library(tidyverse)
library(gapminder)
# Filter the gapminder dataset to only include data for the specified country and year
gapminder_filtered <- gapminder |>
filter(country == params$country)
# Create a plot
ggplot(gapminder_filtered, aes(x = year, y = lifeExp)) +
geom_line() +
labs(title = paste("Life Expectancy in", params$country)) +
theme_minimal()
Parameterized Reporting
Overview
For the last topic of the workshop, we are going to discuss parameterized reporting. This is a powerful feature of Quarto that allows you to create a single document that can be used to generate multiple reports. This is useful when you have a report that needs to be generated for different groups of people, different countries or for different time periods.
Parameters
In Quarto, parameters are defined in the YAML header of the document. You can define as many parameters as you need. Parameters can be used to control the content of the document, the formatting of the document, and the output format of the document. To define a parameter, you use the params
key in the YAML header. Here is an example of a parameter definition:
params:
country: "Afghanistan"
Here our we have defined country
as a parameter. Right now, the country
parameter is set to “Afghanistan” but we could change it to any country that might appear in our dataset or that we might want to talk about in our report. You can set your parameters to any value that you want and then call the parameters later on in your report with params$parameter
where parameter
is the name of the parameter.
For example, you could use the country
parameter to filter a dataset to only include data for a specific country. You could also use the country
to create a title for a plot that includes the country name.. For example, we could produce a line chart of the country we specified in the YAML header:
Parameterized Reports
So now that you can define parameters and use them in your report, ou are ready to learn about how you can use parameters to automate a your report-writing process. For a cool illustration, check out this video by Jadey Ryan at posit::conf(2023)
: