根据docs,downloadHandler应该接受contentType作为字符串。由于contentType不是常量,因此我尝试传递变量或反应式。但是,只要contentType严格不是字符串,应用程序就会失败。如何传递变量值?
我已经尝试过function(){}
,reactive
,eventReactive
。令我感到困惑的是,它对于filename = function(){}
来说还不错。
ui <- fluidPage(
downloadLink("downloadData", "Download")
)
server <- function(input, output) {
# Our dataset
data <- mtcars
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(data, file)
},
contentType = function() {
"text/csv"
}
)
}
shinyApp(ui, server)
在这种情况下,错误消息是
is.na() applied to non-(list or vector) of type 'closure'
和文件未正确下载(server error
)。
答案 0 :(得分:1)
ui <- fluidPage(
radioButtons("csvs", label = "Type to download", choices = c(".txt", ".csv"), selected = ".csv"),
downloadLink("downloadData", "Download")
)
server <- function(input, output) {
# Our dataset
data <- mtcars
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), input$csvs, sep="")
},
content = function(file) {
write.csv(data, file)
}
)
}
shinyApp(ui, server)
一种更简单有效的方法是让用户使用radioButton