Automatic Report Generation: Quarto Markdown

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


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

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


title: "Report for `r params$Name`"
format: html
  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.


  • 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:

                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"), 
                    envir = parent.frame())