【问题标题】:Problem using selectInput to download multiple variables from data in R Shiny使用 selectInput 从 R Shiny 中的数据下载多个变量的问题
【发布时间】:2019-12-19 19:38:22
【问题描述】:

我有一个闪亮的应用程序,允许用户在传单中查看多边形(叶绿素)地图。我希望用户还可以下载地图中用于他们选择的变量的数据的 csv。

我已经能够使用 downloadHandler 下载单个变量或所有数据。我无法下载多个选定的变量。目前,当我使用 selectInput(multiple=TRUE) 时,我收到一个错误。

我在网上搜索过,发现很多人在按变量中的值过滤时遇到问题,但我找不到按多个变量过滤数据框的方法。

这是我的工作的一个mre(我已经删除了所有传单代码,因为它在这里并不相关,因为这方面运作良好)。

global.R

library(dplyr)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

a <- c(1:10)
b <- c(21:30)
c <- runif(10)

data <- data.frame(a, b, c)

ui.R

ui = fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("download", "Download",
                  choices = c("A nice name for variable a",
                              "A nice name for variable b",
                              "A nice name for variable c"),
                  multiple = F),
      downloadButton("downloadData", "Download selected variables")
    ),
    mainPanel(
      tableOutput("mytable")
    )
  )
)

服务器.R

server <- (function(input, output, session) {

  decision <- reactive({
    switch(input$download,
           "A nice name for variable a"= data$a,
           "A nice name for variable b" = data$b,
           "A nice name for variable c" = data$c
           )
  })

  #Create download function for selected data
  output$downloadData <- downloadHandler(
    filename = function() {paste("SelectedVariables", ".csv", sep = "")
    },
    content = function(file) {write.table(decision(), file, sep=",")
    }
  )

})

如果我尝试选择多个变量,则会收到以下错误:

收听http://127.0.0.1:7180 警告:开关错误:EXPR 必须是长度为 1 的向量 [没有可用的堆栈跟踪]

我怀疑是在服务器端使用了switch,但我不确定要改用什么。

欢迎任何建议。

【问题讨论】:

  • 澄清一下,上面的 mre 应该可以工作(就下载单个变量而言)。要重新创建我收到的错误,请将 ui.R 脚本中的行 multiple=F 替换为 multiple=T

标签: r shiny download reactive


【解决方案1】:

我建议您在 selectInput 中使用命名选项,而不是 switch。然后你可以让它像下面的例子一样工作。请注意,这可能需要一些错误处理(例如,未选择任何列)。希望这会有所帮助!

library(shiny)
library(dplyr)

a <- c(1:10)
b <- c(21:30)
c <- runif(10)

data <- data.frame(a, b, c)

ui <-   sidebarLayout(
  sidebarPanel(
    selectInput("download", "Download",
                choices = c("A nice name for variable a" = "a",
                            "A nice name for variable b" = "b",
                            "A nice name for variable c" = "c"),
                multiple = T),
    downloadButton("downloadData", "Download selected variables")
  ),
  mainPanel(
    tableOutput("mytable")
  )
)

server <- (function(input, output, session) {

  decision <- reactive({
    data[,input$download,drop=FALSE]
  })

  #Create download function for selected data
  output$downloadData <- downloadHandler(
    filename = function() {paste("SelectedVariables", ".csv", sep = "")},
    content = function(file) {write.table(decision(), file, sep=",")
    }
  )
})

shinyApp(ui,server)

【讨论】:

  • 非常感谢弗洛里安的快速回答。代码运行良好,现在我可以根据需要下载变量了。
猜你喜欢
  • 2021-08-11
  • 2019-05-22
  • 2019-03-04
  • 2018-06-22
  • 2023-04-02
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多