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 done, there is nothing to download. I want to prevent user from downloading empty pages.
For this, I'm thinking to disable the downloadButton before the output is ready. But I don't know how to do that. I have found ways to disable ActionButton (such as ShinyBS package and other JS codes), but nothing for downloadButton.
Right now, I use validate() to throw errors if the output is not ready. However, when the downloadButton is clicked, a new empty web page opens with an error message in it which is ugly.
let me know what you think.
This is my ui code
downloadButton('download', 'Download Lasso component matrix')),
and this is my server code :
output$download_matrix <- downloadHandler(
filename = function() {
validate(
need(is.null(outputData())==FALSE, "No data to download yet")
)
paste('combined_model_matrix', '.txt', sep='') },
content = function(file) {
write.csv(outputData()$combinedAdjMtr, file)
})
Based on your comment:
Let's say the action button is named
input$start_proc
.In server.R:
Then in ui.R, you can write some javascript to handler the custom message event.
A full example is:
server.R
ui.R
In the example the data processing is faked by waiting for 5 seconds. Then the download button will be ready. I also added some "fake"
fileSize
information in the message to demonstrate that how you can send extra information to the user.Note that because Shiny implements
actionButton
as<a>
tag instead of<button>
, and it bindsclick
event on it. Therefore, in order to fully disable it, in addition to add adisabled
attribute to make it appear to be disabled, you also need to override itsclick
event by adding an inlineonclick
attribute. Otherwise the user can still accidentally click the (seemingly disabled) download button and triggers the download.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")
orenable("download")
.Here's a full example replicating the answer by Xin but with this package