根据用户输入下载为excel或csv格式。该代码仅适用于radioButtons中的预选值。如下所示,它适用于csv,因为selected = "csv"。如果将其更改为xlsx,则只对xlsx有效。用户应该能够选择,这两个选项应该是可能的。
也许值被缓存了,我需要以某种方式强制更新。
library(shiny)
ui <- fluidPage(
h4("Download data"),
wellPanel(
fluidRow(
column(4, radioButtons("dl_data_file_type", "Format",
choices = c(excel = "xlsx",
csv = "csv"),
selected = "csv")),
column(5),
column(3, downloadButton("dl_data_dwnld_bttn"))
)))
server <- function(input, output) {
output$dl_data_dwnld_bttn <- {
downloadHandler(
filename = stringr::str_c(Sys.Date(), " Palim.", input$dl_data_file_type),
content = function(file){
x <- iris
if ( input$dl_data_file_type == "xlsx") {
writexl::write_xlsx(x, file)}
else if ( input$dl_data_file_type == "csv") {
readr::write_csv(x, file)}
})}}
shinyApp(ui = ui, server = server)错误是excel文件仍然以.csv结尾,无法用excel打开。

发布于 2019-06-14 17:38:37
您在filename参数中使用了反应值。在这种情况下,您必须将filename设置为函数:
filename = function(){
stringr::str_c(Sys.Date(), " Palim.", input$dl_data_file_type)
}https://stackoverflow.com/questions/56593372
复制相似问题