代码之家  ›  专栏  ›  技术社区  ›  Hauke

如何用变量contentType实现R闪亮的下载处理程序?

  •  0
  • Hauke  · 技术社区  · 7 年前

    docs ,下载处理程序应接受contentType作为字符串。因为contentType不是常量,所以我试图传递一个变量或reactive。但只要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 回复  |  直到 7 年前
        1
  •  1
  •   DSGym    7 年前
    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