My question is closely related to that one.
I need to add a counter for downloads. So I use a reactive value that I want to set inside the 'content' argument of the downloadHandler
function
An example:
library(shiny)
ui <- fluidPage(
downloadButton("downloadData", "Download"),
actionButton("trig", "get number")
)
server <- function(input, output) {
data <- mtcars
rnDownloads <- reactiveValues(n=0)
observe(print(rnDownloads$n))
observeEvent(input$trig, {
print(paste("number of downloads:", rnDownloads$n))
})
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
rnDownloads$n <- rnDownloads$n + 1
write.csv(data, file)
}
)
}
shinyApp(ui, server)
I don't know whether it's a bug, but it does not work as I expected: the only way to update the value of the reactiveValue rnDownloads$n
is to click on the actionButton
.