How to use the download button in Shiny?

11,017

I think the problem with your code is that you are trying to download two CSV's from one downloadbutton. You have two variables called csv_write, and two write.csv calls. A minimal working example would look like:

library(shiny)

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

  output$downloadData <- downloadHandler(
    filename = function() { 
      paste("dataset-", Sys.Date(), ".csv", sep="")
    },
    content = function(file) {
      write.csv(mtcars, file)
    })
})

ui <- shinyUI(fluidPage(
    downloadButton('downloadData', 'Download data')
))

shinyApp(ui=ui,server=server)
Share:
11,017
Ayan Saraf
Author by

Ayan Saraf

Updated on June 14, 2022

Comments

  • Ayan Saraf
    Ayan Saraf almost 2 years

    I want to download a csv file using the download button in Shiny. The file would be created using the parameters from a secondary r script.

        ###SERVER
        output$downloadData <- downloadHandler({
        filename = function() {
            paste('data-', Sys.Date(), '.csv', sep='')
          }
          content = function(file) {
            csv_write<-array(0,dim=c(length(GHI_D),15))
            csv_write<-cbind(GHI_Data$timeStamp,GHI_D,POA_OBS_T,POA_model_T,POA_model_FT,POA_OBS,DNI_model,DHI,tracking_angle,incidence_angel_T,Backtracking_angle,SunAz,SunEl,Kt,DNI,DDNI,incidence_angel,DHI_model,DHI_model_T,Eb,Eb_T)
            write.csv(csv_write,row.names=FALSE, na="")
            write.csv(csv_write,row.names=FALSE, na="")
          }
      })
        ### UI
              downloadButton('downloadData', 'Download CSV Report', style="display: block; margin: 0 auto; width: 230px;color: black;")
    
    • Ferdi
      Ferdi almost 7 years
      "write_csv" from the "readr" library is faster than "write.csv" from base r