【问题标题】:Create downloadButton that can output multiple file types R Shiny创建可以输出多种文件类型的downloadButton R Shiny
【发布时间】:2018-03-02 09:31:27
【问题描述】:

我为输出 CSV 的 R Shiny 应用程序创建了一个下载按钮。我想在 UI 中添加复选框,以选择输出 json、xls 和 TSV 文件的选项,然后是服务器函数中的相应函数。有什么见解吗?下面是一些与此相关的最小代码:

library(shiny)
set.seed(123)


N<- 500
M<-56

EF<- matrix( rnorm(N*M,mean=23,sd=3), N, M)
WM<- matrix( rnorm(N*M,mean=20,sd=3), N, M)
DP<- matrix( rnorm(N*M,mean=25,sd=3), N, M)

Date <- seq(as.Date("2018-01-01"), as.Date("2018-02-25"), by="days")
Date <- as.POSIXct(Date, format = "%Y-%m-%d %H:%M")

ui <- fluidPage(
  titlePanel(code(strong("Measures"), style = "color:black")),
  sidebarLayout(
    sidebarPanel(
      strong("Tools:"),
      selectInput("Test", 
                  label = "Choose a measure to display",
                  choices = c("EF", 
                              "WM",
                              "DP"
                  ),
                  selected = "EF"),

      downloadButton("downloadData", "Download")),
    mainPanel(
      code(strong("Output Data"))
    ))
)


server <- function(input, output) {


  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$dataset, "Table.csv", sep = ",")
    },
    content = function(file) {
      write.csv(x, file, row.names = FALSE)
    }
  )  
}



# Run that shit ----
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r button shiny


    【解决方案1】:

    不是最优雅的,但这里有一个选项。我创建了一个模拟示例——我无法使用您的代码,因为您的示例中没有定义 x(您正在下载的内容)。

    library(shiny)
    library(RJSONIO)
    library(xlsx)
    
    ui <- fluidPage(
            sidebarLayout(
                    sidebarPanel(
                            selectInput("dataset", 
                                        label = "Choose dataset",
                                        choices = c("iris", "cars")),
                            radioButtons("downloadType", "Download Type", 
                                         choices = c("CSV" = ".csv",
                                                     "JSON" = ".json",
                                                     "XLS" = ".xls",
                                                     "TSV" = ".tsv"),
                                         inline = TRUE),
                            downloadButton("downloadData", "Download")
                            ),
                    mainPanel()
                    )
    )
    
    
    server <- function(input, output) {
            datasetInput <- reactive({
                    switch(input$dataset,
                           "iris" = iris,
                           "cars" = cars)
            })
    
            output$downloadData <- downloadHandler(
                    filename = function() {
                            paste0(input$dataset, "_Table", input$downloadType)
                    },
                    content = function(file) {
                            if(input$downloadType == ".csv") {
                                    write.csv(datasetInput(), file, row.names = FALSE)
                            } else if(input$downloadType == ".json") {
                                    exportJSON <- toJSON(datasetInput())
                                    write(exportJSON, file)
                            } else if(input$downloadType == ".xls") {
                                    write.xlsx(datasetInput(), file, 
                                               sheetName = "Sheet1", row.names = FALSE)
                            } else if(input$downloadType == ".tsv") {
                                    write.table(datasetInput(), file, quote = FALSE, 
                                                sep='\t', row.names = FALSE)
                            }
                    }
            )  
    }
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 太好了!谢谢。抱歉,我正在拆分代码以尽量减少反应性安排。谢谢,这很好用!
    • 非常好!这解决了我一直在努力解决几个小时的问题。
    猜你喜欢
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2018-03-16
    • 2017-01-01
    相关资源
    最近更新 更多