Skip to content
R for the Rest of Us Logo

Going Deeper with R

Parameterized Reporting, Part 1

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

View code shown in video
---
title: "Life Expectancy Report"
format: html
execute: 
  echo: false
  warning: false
  message: false
params:
  country: "Afghanistan"
---

```{r}
if (interactive()) {
  params <- rmarkdown::yaml_front_matter(
    rstudioapi::getSourceEditorContext()$path
  )$params
}
```

```{r}
library(tidyverse)
library(gapminder)
```

```{r}
life_expectancy <-
  gapminder |>
  select(country, year, lifeExp) |>
  filter(country == params$country) 
```

## Life Expectancy in `r params$country`

```{r}
ggplot(
  data = life_expectancy,
  aes(
    x = year,
    y = lifeExp
  )
) +
  geom_line() +
  theme_minimal()
```

Your Turn

I’ve created a starter project to help you. To install it:

  1. Download the course project at https:://rfor.us/gdprmp.

  2. Open the course project in Positron.

Make sure to install the {gapminder}, {rmarkdown}, and {quarto} packages in order to be able to run the code in the project.

Then, working in the project:

  1. Open the file called your-turn-report.qmd and render it to see what it looks like.

  2. Add a parameter in your YAML for continent and set its value to Asia.

  3. On line 19, change the code to filter the gapminder data using the continent parameter.

  4. On line 25, use inline R code to replace Asia with your continent parameter.

Make sure you can render your report and have everything work exactly the same as when you first rendered.

Learn More

Example reports that R for the Rest of Us / Clarity Data Studio has done:

Learn more about parameterized reporting:

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Course Content

44 Lessons