Based on your comment:
yes data processing depends on the user input. USer will upload some files and click anAction button to start the processing. The download button is in a tab set.
Let's say the action button is named input$start_proc.
In server.R:
shinyServer(function(input, output, session) {
#... other code
observe({
if (input$start_proc > 0) {
# crunch data...
# when data is ready:
session$sendCustomMessage("download_ready", list(...))
# you can put extra information you want to send to the client
# in the ... part.
}
})
#... other code
})
Then in ui.R, you can write some javascript to handler the custom message event.
A full example is:
server.R
library(shiny)
fakeDataProcessing <- function(duration) {
# does nothing but sleep for "duration" seconds while
# pretending some background task is going on...
Sys.sleep(duration)
}
shinyServer(function(input, output, session) {
observe({
if (input$start_proc > 0) {
fakeDataProcessing(5)
# notify the browser that the data is ready to download
session$sendCustomMessage("download_ready", list(fileSize=floor(runif(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)
}
)
})
ui.R
library(shiny)
shinyUI(fluidPage(
singleton(tags$head(HTML(
'
<script type="text/javascript">
$(document).ready(function() {
// disable download at startup. data_file is the id of the downloadButton
$("#data_file").attr("disabled", "true").attr("onclick", "return false;");
Shiny.addCustomMessageHandler("download_ready", function(message) {
$("#data_file").removeAttr("disabled").removeAttr("onclick").html(
"<i class=\\"fa fa-download\\"></i>Download (file size: " + message.fileSize + ")");
});
})
</script>
'
))),
tabsetPanel(
tabPanel('Data download example',
actionButton("start_proc", h5("Click to start processing data")),
hr(),
downloadButton("data_file"),
helpText("Download will be available once the processing is completed.")
)
)
))
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 binds click event on it. Therefore, in order to fully disable it, in addition to add a disabled attribute to make it appear to be disabled, you also need to override its click event by adding an inline onclick attribute. Otherwise the user can still accidentally click the (seemingly disabled) download button and triggers the download.
ui.R, you can try to wrap thedownloadButtonwith aconditionalPanel. You can hide it should the condition do not satisfy.outputDataa reactive value?conditionalPanelis the easiest way to solve this problem, and I admit that I tend to use this shortcut sometimes. With bad feelings, because removing button that just cannot be pressed because of some special condition not met violates all rules of good interface design. I wish there were aconditionalDisablePanelin Shiny. As a workaround, I leave the button enabled and display an error message explaining why.conditionalDisablePanel. You can enhance your Shiny App siginificantly with Javascript actually.conditionalDisabledPanelidea, here is one R snippet that does exactly that :) gist:condDisabledPanel.R