SyntaxStudy
Sign Up
R Parameterised Reports and Output Formats
R Beginner 2 min read

Parameterised Reports and Output Formats

Parameterised R Markdown reports allow you to generate multiple versions of the same report for different subsets of data, time periods, or configurations without duplicating code. Parameters are declared in the YAML front matter under the params key, each with a name, default value, and optional input widget type for the Shiny-based parameter UI. Inside the document, parameters are accessed through the read-only list params, e.g., params$region or params$year. When rendering programmatically, you pass a named list to the params argument of rmarkdown::render(), enabling batch report generation with purrr or a simple for loop. R Markdown supports a wide variety of output formats beyond HTML and PDF. Presentation formats include ioslides_presentation, slidy_presentation (HTML slideshows), beamer_presentation (LaTeX PDF slides), and the xaringan package's moon_reader format, which produces elegant HTML5 presentations. The flexdashboard package transforms an R Markdown file into an interactive dashboard with a row/column layout and optional Shiny reactivity. The blogdown and bookdown packages build websites and multi-chapter books respectively from collections of R Markdown files. The Quarto document system is the next-generation successor to R Markdown, developed by Posit (formerly RStudio). Quarto uses .qmd files, natively supports R, Python, Julia, and Observable JS, and provides a unified YAML syntax across languages. The rendering pipeline replaces knitr+Pandoc with a language-agnostic engine. For new projects, Quarto is the recommended choice; R Markdown remains fully supported and the two systems share most concepts.
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()
# ```

This is the last lesson in this section.

Create a free account to earn a certificate