Ayan Saraf
Ayan Saraf

Reputation: 113

Error with the download button in SHINY

My download button is showing an error when I use it to retrieve my report. The output id for the button is downloadData. What seems to be the error?

output$downloadData <- downloadHandler(
filename = function() {
  paste('Final Report', '.csv', sep='')
  },
content = function(file){
       write.csv(csv_write,row.names=FALSE, na="")
})

Upvotes: 1

Views: 2570

Answers (2)

Mal_a
Mal_a

Reputation: 3760

Here is the code that should work for You assuming that csv_write is the dataset which You wanna download:

output$downloadData <- downloadHandler(
        "Final_Report.csv", content = function(file) {
         write.csv(csv_write, file, row.names=FALSE, na="")
        }
      )

and first of all what kind of Error do You get?

Upvotes: 0

Florian
Florian

Reputation: 25375

See the documentation:

content = function(file) {
  write.csv(data, file)
}

So you also have to pass the file parameter to the write.csv function. Assuming the data you want to write is called csv_write, you should do:

write.csv(csv_write, file, row.names=FALSE, na="")

Working example

library(shiny)

csv_write = data.frame(a=c(1,2,3),b=c(1,2,3))

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

  output$downloadData <- downloadHandler(
    filename = function() {
      paste('Final Report', '.csv', sep='')
    },
    content = function(file){
      write.csv(csv_write,file,row.names=FALSE, na="")
    })


})

ui <-shinyUI(fluidPage(

      downloadButton('downloadData', 'Download data')

))

shinyApp(ui,server)

Note that is csv_write is a reactive or reactiveValue, you should call csv_write() instead of csv_write.

Upvotes: 1

Related Questions