reactive text in download handler filename argument

1,199 views
Skip to first unread message

Ian Kyle

unread,
Jan 28, 2014, 3:03:59 PM1/28/14
to shiny-...@googlegroups.com
I've written a shiny app that allows a user to download data.frame, either in .csv or .xls (using the WriteXLS package). When I set the default file type choice (input$format) to '.xls', the file extension and file are .xls. If I then select '.csv', a .csv file is downloaded but the file extension is still .xls. Same if I set selected='.csv' (the file always downloads with .csv extension, but the file may actually be a .xls or .csv depending on what format is selected. 

Why wont the filename register that input$format has changed within the download handler? If I print the value of input$format in the UI, the value changes when I change radiobutton selections. 

# Server
  output$download_data <- downloadHandler(    
                filename = paste0("longdataset_", Sys.time(), input$format),
                        content = function(file){
                          if(input$format=='.csv'){
                        write.csv(data(), file, row.names=FALSE,
                                  na='')}
                          if(input$format=='.xls'){
                            sdat <- data()
                            WriteXLS('sdat',
                                     file)
                          }}
                          )

# UI
radioButtons('format', 'File type:', 
                   choices=c('.xls',
                            '.csv'),
                    selected='.xls'),
      downloadButton('download_data', 'Download long dataset')



Vincent Nijs

unread,
Jan 28, 2014, 3:19:22 PM1/28/14
to shiny-...@googlegroups.com
I have something similar in my app (see below) and it works fine (i.e., file extensions are changed). Your 'filename' line is missing 'function() { ... }'

output$downloadData <- downloadHandler(
  filename = function() { paste(input$datasets,'.',input$saveAs, sep='') },
  content = function(file) { ...

Ian Kyle

unread,
Jan 28, 2014, 3:22:40 PM1/28/14
to shiny-...@googlegroups.com
perfect. thanks!
Reply all
Reply to author
Forward
0 new messages