Parameterized reports let you create reports that change based on input values. This helps you reuse the same report for different data or questions.
Parameterized reports in 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.
month with a default value. The report prints the month.--- title: "Sales Report" params: month: "2023-01" output: html_document --- ```{r} print(params$month) ```
month parameter set to "2023-02".rmarkdown::render( input = "report.Rmd", params = list(month = "2023-02") )
This report greets the person whose name is passed as a parameter. The R console command runs the report with the name "Alice".
--- 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"))
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.
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.
Practice
parameterized reports in R Markdown?Solution
Step 1: Understand parameterized reports concept
Parameterized reports allow passing different inputs to the same report code.Step 2: Identify the main benefit
This lets you customize the report output without editing the code itself.Final Answer:
To customize report content without changing the code -> Option BQuick Check:
Parameterized reports = customize content without code change [OK]
- Confusing parameterized reports with speeding up knitting
- Thinking it automatically creates plots
- Mixing with dashboard creation
year with default value 2023?Solution
Step 1: Recall YAML parameter syntax in R Markdown
Parameters are defined underparams:with key-value pairs.Step 2: Check correct key and value format
params: year: 2023 usesparams:andyear: 2023correctly without quotes needed for numbers.Final Answer:
params: year: 2023 -> Option DQuick Check:
YAML params use 'params:' and colon syntax [OK]
- Using 'parameters:' instead of 'params:'
- Using equals sign '=' instead of colon ':'
- Putting numbers in quotes unnecessarily
params$region is set to "West"?
```{r}
if (params$region == "West") {
print("Sales are high in the West")
} else {
print("Sales data not available")
}
```Solution
Step 1: Check the condition with given parameter
The condition checks ifparams$regionequals "West". It is set to "West".Step 2: Determine which print statement runs
Since condition is true, it prints "Sales are high in the West".Final Answer:
[1] "Sales are high in the West" -> Option AQuick Check:
params$region == "West" -> print message for West [OK]
- Confusing the else branch output
- Assuming params is undefined causing error
- Mixing region names in output
--- title: "Sales Report" params: month = "January" output: html_document ---
Solution
Step 1: Review YAML parameter syntax
Parameters use colon ':' to assign values, not equals '='.Step 2: Identify the syntax error
Here,month = "January"uses '=' which is invalid YAML syntax.Final Answer:
Using '=' instead of ':' to assign parameter value -> Option CQuick Check:
YAML params need ':' not '=' [OK]
- Using '=' instead of ':' in YAML
- Thinking quotes are mandatory for strings
- Confusing output format with params syntax
category and year. Which approach correctly uses parameters in the R code chunk to filter df?Solution
Step 1: Understand filtering syntax in R
Thesubset()function uses logical conditions with '==' to filter rows.Step 2: Check parameter usage in filtering
filtered_df <- subset(df, category == params$category & year == params$year) correctly usesparams$categoryandparams$yearwith '==' insidesubset().Final Answer:
filtered_df <- subset(df, category == params$category & year == params$year) -> Option AQuick Check:
Use '==' for comparison and subset() for filtering [OK]
- Using '=' instead of '==' for comparison
- Incorrect subsetting syntax without dataframe reference
- Confusing filter() syntax without library
Ready to prove it?
Pass the test and this pattern moves unknown
