shiny app : disable downloadbutton

后端 未结 2 1848
花落未央
花落未央 2020-12-31 16:37

My shiny app produces some files that user can download. I have put downloadbutton in the ui for this purpose. However, when the page launches and before any calculation is

2条回答
  •  梦谈多话
    2020-12-31 16:51

    Just adding another answer that works in a similar fashion to the one by Xin, but using a package (shinyjs) that natively supports enabling/disabling buttons, rather than having to deal with the messy javascript yourself. Using this package, you can simply call disable("download") or enable("download").

    Here's a full example replicating the answer by Xin but with this package

    library(shiny)
    library(shinyjs)
    
    runApp(shinyApp(
      ui = fluidPage(
        # need to make a call to useShinyjs() in order to use its functions in server
        shinyjs::useShinyjs(),  
        actionButton("start_proc", "Click to start processing data"),
        downloadButton("data_file")
      ),
      server = function(input, output) {
        observe({
          if (input$start_proc > 0) {
            Sys.sleep(1)
            # enable the download button
            shinyjs::enable("data_file")
            # change the html of the download button
            shinyjs::html("data_file",
                          sprintf("
                                  Download (file size: %s)",
                                  round(runif(1, 1, 10000))
                          )
            )
          }
        })
    
        output$data_file <- downloadHandler(
          filename = function() {
            paste('data-', Sys.Date(), '.csv', sep='')
          },
          content = function(file) {
            write.csv(data.frame(x=runif(5), y=rnorm(5)), file)
          }
        )
    
        # disable the downdload button on page load
        shinyjs::disable("data_file")
      }
    ))
    

提交回复
热议问题