DownloadHandler downloading pdf & html issue

Hi,

I have been trying to resolve this issue for a while now. I have several drop-down lists and a download button. One of the drop-down lists selects between two reports. One of them is a HTML report and the other is pdf.

This is how I handled the downloadhandler for the download button:
(part of the server.R code)

reportname <- reactive({ 
  reportname<- paste0(input$selectreport,".Rmd")
})


ending <- reactive({ 
  if (input$selectreport =="Report1") ending<-".html"
  else{ending <-".pdf"}
  })

parameters <- reactive({ 
  if (input$selectreport =="report1") {
    list(n = input$file1$datapath,
         obs=input$obs)}else{
    list(n = input$file1$datapath,
         variable=input$inSelect)}
})

filename <- reactive({ 
  filename<-paste0(input$selectreport , ending())
})

observe({
  print(filename())
})


output$report <- downloadHandler(
  filename =filename(),
  content = function(file) {
   
    tempReport <- file.path(tempdir(), reportname())
    file.copy(reportname(), tempReport, overwrite = TRUE)
    params <- parameters()

    
    waitress$start()
    
    rmarkdown::render(tempReport, output_file = file,
                      params = params,
                      envir = new.env(parent = globalenv())
    )

    waitress$close() # hide when done
    
  }
)

While the download works fine and the document generated is the right document, the name is always set as report1.html.
No matter what I do the file ending will always be .HTML unless I change the filename() to "report2.pdf".
I use observe to confirm that the filename() contains "report2.pdf" and yet when I download the report I receive "report1.html" every time.

I would be grateful for any guidance on how to fix the issue or how to use a different approach to switch between pdf. and HTML reports being downloaded from two different r markdown documents.

I have created a reprex example.

R markdown report.rmd file:

---
title: "report"
date: "5/9/2022"
output: html_document
params:
  n: NA
---


```{r pressure, echo=FALSE}
plot(rnorm(params$n))```

App code:

shinyApp(
  ui = fluidPage(
    sliderInput("slider", "Slider", 1, 100, 50),
    pickerInput("selectreport", "Select Report",
                c("Report1","Report2"),multiple = F),
    downloadButton("report", "Generate report")
  ),
  server = function(input, output) {
    
    
    ending <- reactive({ 
      if (input$selectreport =="Report1") ending<-".html"
      else{ending <-".pdf"}
    })
    
    parameters <- reactive({ 
      if (input$selectreport =="Report1") {
        list(n = input$file1$datapath,
             obs=input$obs)}else{
               list(n = input$file1$datapath,
                    course=input$inSelect)}
    })
    
    filename <- reactive({ 
      filename<-paste0(input$selectreport , ending())
    })
    
    
    
    observe({
      print(filename())
    })
    
    output$report <- downloadHandler(
      # For PDF output, change this to "report.pdf"
      filename = filename(),
      content = function(file) {
        # Copy the report file to a temporary directory before processing it, in
        # case we don't have write permissions to the current working dir (which
        # can happen when deployed).
        tempReport <- file.path(tempdir(), "report.Rmd")
        file.copy("report.Rmd", tempReport, overwrite = TRUE)
        
        # Set up parameters to pass to Rmd document
        params <- list(n = input$slider)
        
        # Knit the document, passing in the `params` list, and eval it in a
        # child of the global environment (this isolates the code in the document
        # from the code in this app).
        rmarkdown::render(tempReport, output_file = file,
                          params = params,
                          envir = new.env(parent = globalenv())
        )
      }
    )
  }
)

change this part to this :

filename = function() filename(),
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.