Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to download quarto pdf reports from R Shiny application

Tags:

r

pdf

shiny

quarto

I am trying to download pdf/docx/html reports based on the inputs provided by user. To do that I created a Shiny app and a qmd file with parameters. qmd file is working fine when i render it separately. But if i render the same document and try to download it from Shiny it is not working. Here is my downloadHandler chunk

output$report <- downloadHandler(
filename = function() {
  paste("report", input$report_file_type, sep = ".")
},

content = function(file) {
  output <- quarto::quarto_render(
    input = 'report_template.qmd',
    output_format = input$report_file_type,
    execute_params = list(name = input$author_name,
                          report_title = input$report_title)
  )
  file.copy(output, file)
}

)

Edit: here is the qmd report template

---
title: "`r params$report_title`"
author: "`r params$name`"
date: "`r format(Sys.time(), '%d %B, %Y')`"
format: html
embed-resources: true
params:
  report_title: "report title"
  name: "author name"
  p_list: list("A", "B")
---

Printing each element of the list...

```{r}
#| echo: true
for(p in eval(parse(text = params$p_list))){
  print(p)
}
```

Printing the list...
```{r}
#| echo: true
print(eval(parse(text = params$p_list)))
```

I am not getting any error but at the same nothing is being written to downloads folder. It seems like rendered document is not being assigned to output. Perhaps that's why the coping line is not working. what could be the problem and how to solve it? thanks in advance.

like image 711
Satya Pamidi Avatar asked Sep 20 '25 13:09

Satya Pamidi


1 Answers

quarto::quarto_render does not return the generated file or anything and so output in your code is NULL and hence you are not getting any output.

Now to get this code working, we need to copy the file generated by quarto::quarto_render to the file parameter of the function passed to content argument of downloadHandler.

shinyApp(
  
  # UI -----------------------------------------------------------
  ui = fluidPage(
    textInput("author_name", "Author Name", "John Doe"),
    textInput("report_title", "Report Title", "Quarto From Shiny"),
    radioButtons(
      inputId = "report_file_type",
      label = "Report Type",
      choices = c("HTML" = "html", "PDF" = "pdf")
    ),
    downloadButton("report", "Generate report")
  ),
  
  # SERVER --------------------------------------------------------
  server = function(input, output) {
    
    output$report <- downloadHandler(
      filename = function() {
        paste("report", input$report_file_type, sep = ".")
      },
      content = function(file) {
        quarto::quarto_render(
          input = "report_template.qmd",
          output_format = input$report_file_type,
          execute_params = list(
            name = input$author_name,
            report_title = input$report_title
          )
        )
        # copy the quarto generated file to `file` argument.
        generated_file_name <- paste("report_template", input$report_file_type, 
                                     sep = ".")
        file.copy(generated_file_name, file)
      }
    )
  }
)
like image 173
Shafee Avatar answered Sep 22 '25 08:09

Shafee