【问题标题】:shiny download csv or excel depending on radioButton闪亮的下载 csv 或 excel 取决于 radioButton
【发布时间】:2019-06-14 07:23:59
【问题描述】:

根据用户输入下载为 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打开。

【问题讨论】:

  • 你确定吗?我没有看到任何错误。
  • 不,我没试过。我想我现在明白了。看我的回答。

标签: r shiny shiny-reactivity


【解决方案1】:

您在 filename 参数中使用了反应值。在这种情况下,您必须将filename 设置为函数:

filename = function(){
  stringr::str_c(Sys.Date(), " Palim.", input$dl_data_file_type)
}

【讨论】:

    猜你喜欢
    • 2016-05-02
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多