Parameterized reports let you create reports that change based on input values. This helps you reuse the same report for different data or questions.
0
0
Parameterized reports in R Programming
Introduction
You want to generate sales reports for different months without rewriting the report.
You need to create a report that shows data for a specific customer chosen by the user.
You want to filter a report by date range entered by the user.
You want to create a summary report that changes based on a selected product category.
Syntax
R Programming
library(rmarkdown) rmarkdown::render(input = "report.Rmd", params = list(param1 = value1, param2 = value2))
The params argument passes values to the report.
Inside the R Markdown file, you define parameters in the YAML header.
Examples
This example defines a parameter
month with a default value. The report prints the month.R Programming
--- title: "Sales Report" params: month: "2023-01" output: html_document --- ```{r} print(params$month) ```
This runs the report with the
month parameter set to "2023-02".R Programming
rmarkdown::render( input = "report.Rmd", params = list(month = "2023-02") )
Sample Program
This report greets the person whose name is passed as a parameter. The R console command runs the report with the name "Alice".
R Programming
--- title: "Greeting Report" params: name: "Friend" output: html_document --- ```{r} cat(paste0("Hello, ", params$name, "! Welcome to your report.")) ``` # In R console: rmarkdown::render("greeting_report.Rmd", params = list(name = "Alice"))
OutputSuccess
Important Notes
Parameters must be declared in the YAML header of the R Markdown file.
You can pass multiple parameters as a list to rmarkdown::render().
Using parameters makes your reports flexible and reusable.
Summary
Parameterized reports let you customize report content without changing the code.
Define parameters in the YAML header and pass values when rendering.
This helps create dynamic, reusable reports for different inputs.