After reading about parameterized reports in R markdown cookbook, I tried to implement them myself. Creating a simple Rmarkdown example named "work_formatting.Rmd", I added some parameters.
---
title: "`r params$doc_title`"
author: "Paul M. Hargarten"
---
Date Generated: `r params$date`
# Header 1
Blah blah blah text
```{r cars}
cat("For site ", params$site_number, "\n")
summary(cars)
```
And then tried to render it
number <- c(100, 200, 300)
for(i in 1:3){
rmarkdown::render(
input = "work_formatting.Rmd",
output_file = paste0("word_formatting_", number[i], ".docx"),
output_format = my_word(),
params =list(
doc_title = paste("Some_title"),
date = lubridate::today(),
site_number = number[i]
),
clean = TRUE,
quiet = FALSE
)
}
but I received this error
processing file: work_formatting.Rmd
|.................. | 12%
inline R code fragments
Quitting from lines 2-8 (work_formatting.Rmd)
Error in eval(parse_only(code), envir = envir) :
object 'params' not found
If I define params first (i.e., see below), the word documents are created as expected. I think that this solution works because the default environment used by render() is parent.frame() and thus can find the global object params. However, I am at a loss as this solution differs from just adding them as the argument of render() as indicated in the reference in R Markdown Cookbook. Does anyone else get these errors? Would anyone know if there is a way to avoid creating a global params object?
number <- c(100, 200, 300)
for(i in 1:3){
params = list(
doc_title = paste("Some_title"),
date = lubridate::today(),
site_number = number[i]
)
print(params)
rmarkdown::render(
input = "work_formatting.Rmd",
output_file = paste0("word_formatting_", number[i], ".docx"),
output_format = my_word(),
params = params,
clean = TRUE,
quiet = FALSE
)
}
