Data Download problem in Shiny

23 views
Skip to first unread message

Nada Abdalla

unread,
Jan 21, 2018, 9:50:44 PM1/21/18
to Shiny - Web Framework for R

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)

  })

}

Ger Inberg

unread,
Feb 9, 2018, 2:42:53 AM2/9/18
to Shiny - Web Framework for R
Hi Nada,

your problem is not reproducible for me because of the datasets referenced, but a similar example (see below) for me is working
Could the problem be related to your datasets?

ui <- fluidPage(
        tabPanel("Downloading Data",
          titlePanel("Download Data"),
          sidebarLayout(
            sidebarPanel(
              selectInput("dataset",label="Choose a dataset:",c("iris","mtcars")),
              downloadButton("downloadData","Download")
            ),
            mainPanel(
              tableOutput("tab3")
            )
          )
        )
)

server <- function(input, output) {
  datasetInput<-reactive({
    switch(input$dataset,"iris"=iris,"mtcars"=mtcars)
  })
  output$tab3<-renderTable({
    datasetInput()
  })
  output$downloadData<-downloadHandler(
    filename=function(){
      paste(input$dataset,".csv",sep="")
    },
    content=function(file){
      write.csv(datasetInput(),file,row.names = FALSE)
    })
}
shinyApp(ui = ui, server = server)

Nada Abdalla

unread,
Feb 9, 2018, 9:33:25 AM2/9/18
to Shiny - Web Framework for R
Hi Greg, I’m really not sure what the problem is. I’ve decided to make the data download in separate tabs instead of the drop down menu option, it works in this case.
Thank you
Reply all
Reply to author
Forward
0 new messages