Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create downloadButton that can output multiple file types R Shiny

Tags:

r

button

shiny

I have created a download button for an R Shiny app that outputs a CSV. I would like to add check boxes to the UI for options to output a json, xls, and TSV file and then the corresponding functions in the server function. Any insights? Bellow is some minimal code related to this:

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)
like image 200
Isaac Avatar asked Nov 16 '25 07:11

Isaac


1 Answers

Not the most elegant, but here's an option. I created a mock example -- I couldn't use your code since x (what you're downloading) isn't defined in your example.

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)
like image 68
Hallie Swan Avatar answered Nov 17 '25 22:11

Hallie Swan