![](/img/trans.png)
[英]Multiple variables to facet_wrap from selectInput: R Shiny
[英]Problem using selectInput to download multiple variables from data in R Shiny
我有一个 Shiny 应用程序,允许用户在传单中查看多边形(叶绿体)地图。 我想让用户还可以下载地图中用于他们选择的变量的数据的 csv。
我已经能够使用 downloadHandler 下载单个变量或所有数据。 我无法下载多个选定的变量。 目前,当我使用 selectInput(multiple=TRUE) 时,我收到一个错误。
我在网上搜索过,发现很多人在按变量中的值过滤时遇到问题,但我无法找到按多个变量过滤数据框的方法。
这是我的工作(我已经删除了所有传单代码,因为它在这里并不真正相关,因为该方面运行良好)。
全局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 = 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")
)
)
)
服务器
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
,但我不确定用什么来代替。
任何建议将是最受欢迎的。
我建议您在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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.