Build Parameterized Report
Create reports that accept parameters to generate multiple customized variations from a single template.
When to Use
- •Generating the same report for different departments, regions, or time periods
- •Creating client-specific reports from a template
- •Building dashboards that filter to specific subsets
- •Automating recurring reports with different inputs
Inputs
- •Required: Report template (Quarto or R Markdown)
- •Required: Parameter definitions (names, types, defaults)
- •Optional: List of parameter values for batch generation
- •Optional: Output directory for generated reports
Procedure
Step 1: Define Parameters in YAML
For Quarto (report.qmd):
yaml
---
title: "Sales Report: `r params$region`"
params:
region: "North America"
year: 2025
include_forecast: true
format:
html:
toc: true
---
For R Markdown (report.Rmd):
yaml
--- title: "Sales Report" params: region: "North America" year: 2025 include_forecast: true output: html_document ---
Step 2: Use Parameters in Code
markdown
```{r}
#| label: filter-data
data <- full_dataset |>
filter(region == params$region, year == params$year)
nrow(data)
```
## Overview for `r params$region`
This report covers the `r params$region` region for `r params$year`.
```{r}
#| label: forecast
#| eval: !expr params$include_forecast
# This chunk only runs when include_forecast is TRUE
forecast_model <- forecast::auto.arima(data$sales)
forecast::autoplot(forecast_model)
```
Step 3: Render with Custom Parameters
Single render:
r
# Quarto quarto::quarto_render( "report.qmd", execute_params = list(region = "Europe", year = 2025) ) # R Markdown rmarkdown::render( "report.Rmd", params = list(region = "Europe", year = 2025), output_file = "report-europe-2025.html" )
Step 4: Batch Render Multiple Reports
r
regions <- c("North America", "Europe", "Asia Pacific", "Latin America")
years <- c(2024, 2025)
# Generate all combinations
combinations <- expand.grid(region = regions, year = years, stringsAsFactors = FALSE)
# Render each
purrr::pwalk(combinations, function(region, year) {
output_name <- sprintf("report-%s-%d.html",
tolower(gsub(" ", "-", region)), year)
quarto::quarto_render(
"report.qmd",
execute_params = list(region = region, year = year),
output_file = output_name
)
})
Expected: One HTML file per region-year combination.
On failure: Check that parameter names match exactly between YAML and code. Ensure all parameter values are valid.
Step 5: Add Parameter Validation
r
#| label: validate-params stopifnot( "Region must be a valid region" = params$region %in% valid_regions, "Year must be numeric" = is.numeric(params$year), "Year must be reasonable" = params$year >= 2020 && params$year <= 2030 )
Step 6: Organize Output
r
# Create output directory
output_dir <- file.path("reports", format(Sys.Date(), "%Y-%m"))
dir.create(output_dir, recursive = TRUE, showWarnings = FALSE)
# Render with output path
quarto::quarto_render(
"report.qmd",
execute_params = list(region = region),
output_file = file.path(output_dir, paste0("report-", region, ".html"))
)
Validation
- • Report renders with default parameters
- • Report renders with each set of custom parameters
- • Parameters are validated before processing
- • Output files are named descriptively
- • Conditional sections render correctly based on parameters
- • Batch generation completes for all combinations
Common Pitfalls
- •Parameter name mismatch: YAML names must exactly match
params$namereferences in code - •Type coercion: YAML may parse
year: 2025as integer but code expects character. Be explicit. - •Conditional evaluation: Use
#| eval: !expr params$flagnoteval = params$flagin Quarto - •File overwriting: Without unique output names, each render overwrites the previous
- •Memory in batch mode: Long batch runs may accumulate memory. Consider using
callr::r()for isolation.
Related Skills
- •
create-quarto-report- base Quarto document setup - •
generate-statistical-tables- tables that adapt to parameters - •
format-apa-report- parameterized academic reports