Search code examples
rshinyshiny-server

downloadHandler not working with observeEvent


I am trying to download a file using downloadHandler with observeEvent shiny but I am not able to download the file,

    library(shiny)

load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata"))


   ui <- fluidPage(
    sidebarLayout(

    # Input
    sidebarPanel(

      # Numeric input for number of rows to show
      numericInput(inputId = "n_rows",
                   label = "How many rows do you want to see?",
                   value = 10),

      # Action button to show
      actionButton(inputId = "button", 
                   label = "Show")

    ),

    # Output:
    mainPanel(
      tableOutput(outputId = "datatable")

    )
  )
)


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

  # creating a reactive expression
  df <- eventReactive(input$button, {
    movies %>% head(input$n_rows)
  })

  # download a csv  everytime when user click on show button
  observeEvent(input$button, {
    output$button <- downloadHandler(
      filename = function() {
        paste("data-", Sys.Date(), ".csv", sep="")
      },
      content = function(file) {
        write.csv(df(), file)
      }
    )
    cat("done downloading file  \n")
  })

  # displays the data on the web in tabular format, data comes from reactive event
  output$datatable <- renderTable({
    df()
  })
}

# Create a Shiny app object
shinyApp(ui = ui, server = server)

I was able to execute above code without any errors, but csv file is not downloading, i want to display the data table and download the displayed data on same button click event , how can i achieve this, Am I missing something, any help would be appreciated


Solution

  • Try this:

    library(shiny)
    library(dplyr)
    load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata"))
    
    
    ui <- fluidPage(
      sidebarLayout(
    
        # Input
        sidebarPanel(
    
          # Numeric input for number of rows to show
          numericInput(inputId = "n_rows",
                       label = "How many rows do you want to see?",
                       value = 10),
    
          # Action button to show
    
          downloadButton('downloadData', 'Download data')
    
        ),
    
        # Output:
        mainPanel(
          tableOutput(outputId = "datatable")
    
        )
      )
    )
    
    
    server <- function(input, output, session) {
    
      # Reactive value for selected dataset ----
      df <- reactive({
        movies %>% head(input$n_rows)
      })
    
    
      output$datatable <- renderTable({
        df()
      })
    
      output$downloadData <- downloadHandler(
        filename = function() { 
          paste("dataset-", ".csv", sep = "")
        },
        content = function(file) {
          write.csv(df(), file, row.names = FALSE)
        })
    }
    
    # Create a Shiny app object
    shinyApp(ui = ui, server = server)