R
Beginner
2 min read
Parameterised Reports and Output Formats
Example
# --- Parameterised report YAML ---
# ---
# title: "Regional Sales Report: `r params$region`"
# author: "Analytics Team"
# date: "`r Sys.Date()`"
# output: html_document
# params:
# region:
# label: "Select region"
# value: "North"
# input: select
# choices: [North, South, East, West]
# year:
# label: "Fiscal year"
# value: 2024
# input: slider
# min: 2020
# max: 2025
# step: 1
# ---
# --- Accessing params inside the document ---
# ```{r filter-data}
# sales_region <- sales |>
# filter(region == params$region,
# year == params$year)
# cat("Rows for", params$region, "in", params$year, ":", nrow(sales_region))
# ```
# --- Batch rendering (in a separate R script) ---
# library(purrr)
# regions <- c("North", "South", "East", "West")
# walk(regions, function(reg) {
# rmarkdown::render(
# input = "regional_report.Rmd",
# output_file = paste0("report_", reg, ".html"),
# params = list(region = reg, year = 2024)
# )
# })
# --- Quarto equivalent (.qmd) YAML ---
# ---
# title: "My Quarto Report"
# format: html
# params:
# region: "North"
# execute:
# echo: true
# warning: false
# ---
#
# ```{r}
# #| label: fig-scatter
# #| fig-cap: "MPG vs Weight"
# library(ggplot2)
# ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
# ```
Related Resources
This is the last lesson in this section.
Create a free account to earn a certificate