Ted Mosby
Ted Mosby

Reputation: 1456

Parameterization of Rmarkdown Files

I'm trying to loop through a list of employee IDs to create a report for each id.

I know I have to declare the parameter in the YAML but I'm getting a scanner error.

---
title: "Employee Record"
params:
  MASTER_ID: !r uniqueID
output: pdf_document
---
**Employee ID:** `r params$MASTER_ID`

The field of employee ids in the dataset is called MASTER_ID and uniqueID is just a list of each unique employee id (length = 880)

The error I'm getting is:

Error in yaml::yaml.load(string, ...) : 
  Scanner error: while scanning a simple key at line 3, column 1 could not find expected ':' at line 4, column 1

I don't have any extra white spaces or anything so i'm not sure what I'm missing

Upvotes: 1

Views: 118

Answers (1)

joshpk
joshpk

Reputation: 769

To follow up on what others have already said, it is best to call your Rmd from a separate R file using the rmarkdown::render function. This also allows you to easily control the file naming and output location.

employees <- 1:10

for (i in employees) {
  rmarkdown::render("test_pdf.Rmd",
                    params = list(MASTER_ID = i),
                    output_file = paste0('employee-', i, ".pdf"),
                    output_dir = '/reports')
}

With test_pdf.Rmd containing:

title: "Employee Record"
params:
  MASTER_ID: 
    value: 1
output: pdf_document
---
**Employee ID:** `r params$MASTER_ID`

Upvotes: 1

Related Questions