如何创建“操作按钮”以重置ECharts2Shiny条形图中的选择

时间:2019-02-05 12:31:58

标签: r shiny

我想将选择重置为条形图。这是我的代码:

library(shiny)
library(shinydashboard)
library(ECharts2Shiny)

demo <- data.frame(c(5,0,0,1,20,25,14,1,53),
               c(1,1,1,2,12,22,5,1,25),
               c(6,0,0,3,26,38,19,2,65),
               c(0,0,0,0,5,3,0,0,6),
               c(0,1,1,0,1,6,0,0,7),
               c(6,0,1,3,32,47,19,2,78),
               c(2,0,0,1,8,13,10,1,33),
               c(0,0,0,0,0,9,2,0,4),
               c(1,0,0,1,6,4,6,0,23))
names(demo) <- c("M", "F", "O", "B", "C", "Re", "Vi", "Ac", "Co")
row.names(demo) <- c("Az","Bh", "Bi", "Ch", "Ha", "Ka", "N.G", "Ra", "Su") 

ui <- mainPanel(tabPanel(h5("Block wise"), loadEChartsLibrary(), 
tags$div(id="demo1", style="width:100%;height:500px;"), deliverChart(div_id= 
"demo1"),downloadButton("downloadData", "Download Data")))
server <- shinyServer(function(input,output){
             renderBarChart(div_id = "demo1", grid_left = '1%', direction = 
"vertical", data = demo)})
shinyApp(ui,server)

如何创建重置按钮以取消选择属性选择? 我也需要帮助以excel csv格式下载数据。下载数据按钮会弹出一个窗口,要求保存文件,但不允许我们选择“文件名”输入栏正下方的“另存为类型”。

预先感谢

1 个答案:

答案 0 :(得分:0)

可以使用shinyjs来执行js代码来重置绘图:location.reload();

downloadButton必须具有服务器组件才能以给定的文件格式保存数据。

library(shiny)
library(shinydashboard)
library(ECharts2Shiny)
library(shinyjs)

demo <- data.frame(c(5,0,0,1,20,25,14,1,53),
                   c(1,1,1,2,12,22,5,1,25),
                   c(6,0,0,3,26,38,19,2,65),
                   c(0,0,0,0,5,3,0,0,6),
                   c(0,1,1,0,1,6,0,0,7),
                   c(6,0,1,3,32,47,19,2,78),
                   c(2,0,0,1,8,13,10,1,33),
                   c(0,0,0,0,0,9,2,0,4),
                   c(1,0,0,1,6,4,6,0,23))
names(demo) <- c("M", "F", "O", "B", "C", "Re", "Vi", "Ac", "Co")
row.names(demo) <- c("Az","Bh", "Bi", "Ch", "Ha", "Ka", "N.G", "Ra", "Su") 

ui <- mainPanel( shinyjs::useShinyjs(),
  tabPanel(h5("Block wise"), loadEChartsLibrary(), 
          actionButton("reset","reset plot"),
          tags$div(id="demo1", style="width:100%;height:500px;"), 
          deliverChart(div_id="demo1"),
          downloadButton("downloadData", "Download Data")))
server <- shinyServer(function(input,output){
  observeEvent(input$reset,{
    shinyjs::runjs("location.reload();")
  })

  output$downloadData <- downloadHandler(
  filename = function() {
    paste('data-', Sys.Date(), '.csv', sep='')
  },
  content = function(con) {
    write.csv(demo, con)
  }
)

  renderBarChart(div_id = "demo1", grid_left = '1%', direction = 
                   "vertical", data = demo)})
shinyApp(ui,server)