Search code examples
rr-markdownquarto

Automatic Report Generation: Quarto Markdown


I'm trying to generate a report. Here's a bareboned file.

"Reference.qmd"

name <- c("abc", "def")

for (i in name) {
  rmarkdown::render(input = "example_report.qmd",
                    output_file = paste0("report_", i, "_", Sys.Date(), ".pdf"), 
                    rmarkdown::pdf_document(),
                    params = list(Name = i))
}

"example_report.qmd"

---
title: "Report for `r params$Name`"
format: html
params:
  Name: "default Name"
---

## Quarto

This is a report for `r params$Name`. This person is fabulous. 

Output: object 'Name' not found

I've tried doing this in Rmarkdown file and I have got the same error. I have also executed this code from the terminal with the same issue. I've looked at previous solutions but they don't apply to me. Not sure what's incorrect. Need help.


Solution

  • There are two solutions to your questions. One is using the variables from the outer scope of the rmd file. The other is using the params argument to pass the variables to the rmd-file. Only for the first one, I found a solution for quarto, too.

    Parameters given to a rmd document through the params argument are available inside the document with the list named params. for quarto_render it looks like this:

    quarto_render("example_report.qmd",
                output_format = "html",
                output_file = paste0("report_", i, "_", Sys.Date(), ".html"), 
                execute_params=list(Name = i))
    

    the qmd or rmd-files has to be changed in the last line:

    This is a report for `r params$Name`. This person is fabulous. 
    

    A second way for me was to use the envir argument. Here you can simply use the variable name defined as in the outer scope. Here the slighly changed script. You have to rename variables so that the naming matches your rmd-file.

    x <- c("abc", "def")
    
    for (Name in x) {
        rmarkdown::render(input = "example_report.qmd",
                    output_file = paste0("report_", Name, "_", 
        Sys.Date(), "html"), 
                    rmarkdown::html_document(),
                    envir = parent.frame())
    }