Hi,
We are currently working on a Shiny application that will be deployed on shinyapps.io. Users of the app can upload an Excel-file. The app processes the file, after which it produces an output. The user can download this file through a download button in the application (based on downloadHandler).
A requirement of our app is that we delete all (processed) data as soon as possible. This also implies that we want to delete the output file after the user download is complete. I have tried to enforce this, but not succesfully yet: the file was deleted before the download was completed. This resulted in an empty HTML-download rather than the desired Excel-file. Therefore, is there a way to enforce this after the download is complete?
The solution I tried before is as follows:
output$download <- downloadHandler(
filename = function() {
ts <- format(Sys.time(), "%Y%m%d-%H%M%S")
paste0("output-", ts, ".xlsx")
},
content = function(file) {
req(results_rv()) # results_rv() is a reactive value in which the output file is stored
res <- results_rv()
writexl::write_xlsx(
x = res,
path = file
)
on.exit({
if (file.exists(file)) unlink(file, force = TRUE)
}, add = TRUE)
}
)
I have read that it is difficult to implement the deletion as there apparently is no feedback from HTML to the app that the download is complete. Also, Shiny itself would clear the output file. However, I would like to validate whether this is true or whether there are different solutions. Perhaps by delaying the deletion?
I have read the following post in which we find out that temporary data can be removed. I have applied this solution for the (intermediate) files, but did not see it fit yet for this problem (I might be wrong).
If more information is necessary to provide an answer I am glad to help. Thank you in advance!