Data download problem in Shiny


#1

I’ve run into a problem with downloadHandler() in Shiny:
I want to choose from three data sets to download any file via this function. The dropdown menu in the app shows the three files to choose from, but when I click download it’s always the first dataset that is downloaded.

The overall code is pretty long but this is the relevant part. I appreciate if someone points out to where I’m making a mistake:

ui <- fluidPage( tabPanel("Downloading Data",

                      titlePanel("Download Data"),

                      sidebarLayout(

                        sidebarPanel(

                          selectInput("dataset",label="Choose a dataset:",c("m.census","f.census","mortality.rates")),

                          downloadButton("downloadData","Download")

                        ),

                        mainPanel(

                          tableOutput("tab3")

                        )

                      )))

server <- function(input, output) {

datasetInput<-reactive({

    switch(input$dataset,

           "m.census"=m.census,

           "f.census"=f.census,

           "mortality.rates"=mortality.rates)

  })

  output$tab3<-renderTable({

    datasetInput()

  })

output$downloadData<-downloadHandler(

    filename=function(){

    paste(input$dataset,".csv",sep="")

  },

  content=function(file){

    write.csv(datasetInput(),file,row.names = FALSE)

  })

}