0

I want to make several data sets available for download from the Shiny app. I don't wan to display them or making any other calculations. Just make them downloadable. I've created a subdirectory called data, where I placed several csv files. My current code writes just an empty csv file, and ignores the datasets.

ui <- fluidPage( 
 selectInput(inputId = "dataset", label = "Select a dataset",
                             choices = c("",
                                         "a",
                                         "b",
                                         "c")
                             ),
br(),
                 
                 downloadButton("downloadData", "Download") )

and the server side

server <- function( input, output, session ) {

output$sample <- renderTable( sampleData() )
  
sampleData <- reactive({
     switch(input$dataset, 
            "a"               = read.csv("/data/a.csv"),
            "b"               = read.csv("/data/a.csv"),
            "c"                = read.csv("/data/a.csv")   )

output$sample <- renderTable( sampleData() )
  
output$downloaData <- downloadHandler(
      filename = function(){
      paste("sample_dataset.csv", sep = "")
    },
    
    content = function(file) {
    write.csv(sampleData(), file, row.names = FALSE)
    })
}

Thank you!


EDIT


Here is the second version of the code:

ui <- fluidPage( 
fluidRow(
                 column(6,

                 selectInput(inputId = "dataset",
                             label = "Select a sample dataset",
                             choices = c("",
                                         "a",
                                         "b",
                                         "c",
                                         "d",
                                         "e"
                             )),
                 br(),

                 downloadButton("downloadData", "Download")),

                 tableOutput('sample'),
                 )

SERVER:

server< - function( input, output, session ) { 
 output$sample <- renderTable( sampleData() )
  
  sampleData <- reactive({
        switch(input$dataset,
               "a" = read.csv("data/a.csv"),
               "b" = read.csv("data/b.csv"),
               "c" = read.csv("data/c.csv"),
               "d" = read.csv("data/d.csv"),
               "e" = read.csv("data/e.csv")
        )})
  
  output$sample <- renderTable( sampleData() )
  
  output$downloadData <- downloadHandler( 
    filename = function(){
      paste("sample_dataset.csv", sep = "")
    },
    
    content = function(file) {
      write.csv(sampleData(), file, row.names = FALSE)
    }) 
}
Student
  • 35
  • 6
  • Are you sure the `/data/a.csv` path is correct? Are you using a relative or an absolute path name? – MrFlick Nov 30 '20 at 00:40

1 Answers1

1

You are missing a few things in your code:

1 - you are missing )} closing sampleData <- reactive

2 - You have an extra /before data in read.csv

3 - DownloadData is missing a lower-case d in Render

server <- function( input, output, session ) {

output$sample <- renderTable( sampleData() )



sampleData <- reactive({
     switch(input$dataset, 
           
            "a"               = read.csv("data/a.csv"), # you had an extra / before data
            "b"               = read.csv("data/a.csv"),
            "c"                = read.csv("data/a.csv") )}) # you were missing `)}` here

output$sample <- renderTable( sampleData() )
  
output$downloadData <- downloadHandler( #Here was DownloadData misspelled
      filename = function(){
      paste("sample_dataset.csv", sep = "")
    },
    
    content = function(file) {
    write.csv(sampleData(), file, row.names = FALSE)
    })
 }
ViviG
  • 1,613
  • 1
  • 9
  • 23
  • Thank you! However, it writes an empty csv instead of downloading my files... – Student Dec 18 '20 at 02:21
  • There is probably another small mistake in your code. You can copy it here and I look into it. – ViviG Dec 18 '20 at 11:32
  • Thank you! I've added the new code. It contains much more than the presented chunks, however the rest of the code is unrelated to this download button. – Student Dec 18 '20 at 16:23
  • The code works fine for me. I get a file with data. There were a couple of mistakes (like missing a parenthesis), that I had to get in place in order to render the app, but this should not be the issue in your real code since you managed to download an empty .csv file. Maybe this will help you [Shiny app: downloadHandler does not produce a file](https://stackoverflow.com/questions/25984138/shiny-app-downloadhandler-does-not-produce-a-file). There are a few good answers there. Let me know when you get it working! – ViviG Dec 18 '20 at 23:09