Log (un)successful file download in Shiny for R

This is a cross-post from Stackoverflow (where I didn't get any response so far :frowning: )

Let's say we have the following minimal shiny app:

library(shiny)

app <- list(

  ui = fluidPage(

    downloadButton("downloadData", label = "Download")
  ),

  server = function(input, output) {

    output$downloadData <- downloadHandler(
      filename = "data.csv",

      content <- function(file) {

        print("Download started") # Function call log

        write.csv(mtcars, file)
      }
    )
  }
)

shiny::runApp(app)

As you can see, I can call print() to "log" to the standard output when the download of "data.csv" has been started.

However, the download can fail for many different reasons: e.g. the user cancels the download, they choose a directory without write permission, or try to save to a drive that has no available space.

Is there a way I could log when the download has finished successfully (or not)? The help page for shiny::downloadHandler() doesn't provide info about this, and I haven't been able to find anything of the sort in StackOverflow or elsewhere.

Thanks!

I believe what you are asking for is not possible under current web standards. The downloads are clientside, the browser would not (want to) leak that info back to the js runtime. I believe that browser extensions that users can opt in to can enable "download api"'s for js communication on that level but not otherwise.

Im not expert in this area, and I may well be wrong but this is my current understanding

1 Like

Thanks @nirgrahamuk , you are much more of an expert than I am, that's for sure.

Something like that is what I was expecting, but I don't have a clear understanding of all the client-server communications. I also don't fully understand the JS runtime's role in the Shiny app, so I'll rely on your response and assume there is no way of achieving this.

After reading your response though, I'm wondering if it would be possible to achieve it somehow using cookies (but maybe I'm misunderstanding something else as well).