简体   繁体   中英

how to download quarto pdf reports from R Shiny application

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.

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)
      }
    )
  }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM