Why is filename not persisting through?

Hi, I'm running into an issue where the filename in shiny::downloadHandler is not being persisted through in the content argument. Here is an example:

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
  shiny::downloadButton("download_submission", "Download Submission")
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  .SubmissionFilename <- function(dir, loss_analysis) {
    return("test.xlsx")
  }

  output$download_submission <- shiny::downloadHandler(
    filename=function() {
      .SubmissionFilename(dir="", loss_analysis="")
    },
    content=function(file) {
      browser()
      # Due to bug in Rstudio
      stopifnot(file == "test.xlsx")
    }
    , contentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  )
}

# Run the application
shinyApp(ui = ui, server = server)

in content=function(file) {
the file will be provided by the shiny runtime; it will be a random file location in temporary storage that can be written to.
the code in this content handler must write a file with the file name for the handler to work.

The final name of the file is decided by filename=function() { and in your case is test.xlsx and this would work if you produced a file and observed what the filename in the browser showed itself to be.

i.e. there is a renaming step, where the arbitrary file you gave gets the name you describe at the point of being deliverable.

1 Like

Oh okay thank you for the explanation! That makes sense for this toy example.

I'm guessing I've found a case where that renaming step fails at the end in an Ubuntu Server, and not when running the app locally in Windows. On our hosted shiny app we get an error *.htm file wasn't available on site. Locally, in Windows, it runs fine. Unfortunately this is difficult to replicate due to not all cases running into this issue in Linux. So Linux isn't the only prerequisite to getting this error.

Currently we have a workaround by just overwriting the file argument in the content=function(file) part. I'll see if I can find a better replica of the error we're seeing.