Download data button to appear only when data is fetched?

I'm building a Shiny app that makes an API call that's pretty slow in returning data. I'm using a submit button in the app. The following code will prevent the "Download data" button from appearing until after the user hits submit:

conditionalPanel(
                                condition = "input.submit1 == 1",
                                downloadButton("downloadData1", "Download Data")
                              )

However, because of the long wait for actual data to be returned, I'd prefer the download button not to appear until after my reactive data set mydata() is generated. Any suggestions on how I should do that? Thank you.

Another way I'd suggest is to use shinyjs and have the download button disabled until the condition is met. The nice thing about that is that most of the code is on the server side, which makes it easier to set the condition you need to go from disabled to enabled.

This demo app by Dean Attali (the author of shinyjs), has a feature in which the "submit" button is disabled, and becomes enabled when text is inputted.

The only part you'll need to work out is the logic that indicates that mydata() is ready for download.

2 Likes

You could render the downloadButton on the server side instead, hiding it until both the submit button has been pressed and mydata() is ready. It might look something like

renderUI({
  req(input$submit, mydata())
  downloadButton("downloadData")
})
3 Likes

Thanks! That's what I'm trying to do. It turned out to be

uiOutput("downloadData")

on the ui side and

output$downloadData <- renderUI({
    req(input$submit1, mydata())
    downloadButton("downloadData01")
  })

on the server side, with the download button server definition:

output$downloadData01 <- downloadHandler(
    filename = function() {
      paste(input$myquery, ".csv", sep = "")
    },
    content = function(file) {
      write.csv(mydata(), file, row.names = FALSE)
    }
  )
3 Likes

Further to this question, after download button is clicked and data is saved by the user, how to hide the button?
Thanks.

You might consider not doing that--there's no way to be sure the browser actually successfully completed the download, the user may want to try again if there was a network failure or something.

1 Like