Parameterized reporting is for everyone!
Hired as an environmental technician.
Then transitioned to data scientist.
I went from picking leeches off my boots to picking cats off my keyboard!
With a skills test!
Task: generate custom Soil Health Reports for each individual farmer
Timeframe: one week
Challenge: how do I automate custom reports???
YouTube and blog rabbit holes & tutorial hell…
…because LaTeX 😖 🤷
We’re end-of-conference tired 🥱 and our brains are full 😵.
Don’t worry about taking notes or copying code.
Parameterized Quarto reports work with both Knitr
and Jupyter
engines.
Run any line or chunk to add params
to your environment.
Render Button, keyboard shortcut Ctrl + Shift + K
, or by saving.
Uses default parameters in the YAML.
Output file has same name and location as input file.
quarto::quarto_render()
Use in Console
or an iterate.R
script.
Create a data frame with two columns that match quarto_render()
args:
output_file
: filename.extexecute_params
: named list of parametersoutput_file | execute_params |
---|---|
2023_SammySunflower_Report.html | Sammy Sunflower, 2023 |
2022_OscarOrchard_Report.html | Oscar Orchard, 2022 |
2022_TinaTomato_Report.html | Tina Tomato, 2022 |
Map over each row of the reports
dataframe.
year | sampleId | farmer | crop | sand_% | silt_% | clay_% |
---|---|---|---|---|---|---|
2023 | 23-SUN-01 | Sammy Sunflower | Sunflower | 44 | 23 | 3 |
2022 | 22-ORC-02 | Oscar Orchard | Cherry | 69 | 21 | 10 |
2022 | 22-TOM-02 | Tina Tomato | Vegetable | 11 | 79 | 10 |
2022 | 22-HAR-03 | Henry Harvest | Herb | 36 | 51 | 13 |
2023 | 23-GAR-03 | Grace Gardener | Sunflower | 64 | 33 | 3 |
2022 | 22-QUI-02 | Quincy Quinoa | Quinoa | 24 | 62 | 14 |
fake farmer names: ChatGPT 🙏🏻
params
to set up comparisons# Get the farmer's crops
farmer_crop <- data |>
dplyr::filter(farmer == params$farmer) |>
dplyr::pull(crop) |>
unique()
data_long <- data |>
# Tidy data: one observation per row
tidyr::pivot_longer(cols = c("sand_%":"clay_%"),
names_to = "measurement") |>
dplyr::mutate(
# Categorize samples based on if they are from the farmer of
# interest or are of the same crop type
category = dplyr::case_when(
farmer == params$farmer ~ "Your fields",
crop %in% farmer_crop ~ "Same crop",
TRUE ~ "Other fields"
)
)
# Set category factor levels
data_long$category <- factor(
data_long$category,
levels = c("Other fields", "Same crop", "Your fields"),
labels = c("Other fields", "Same crop", "Your fields")
)
data_long |>
dplyr::select(-c(year, sampleId)) |>
dplyr::slice_head(by = category) |>
kableExtra::kable() |>
kableExtra::column_spec(5, background = "#FDCE86")
farmer | crop | measurement | value | category |
---|---|---|---|---|
Sammy Sunflower | Sunflower | sand_% | 44 | Your fields |
Oscar Orchard | Cherry | sand_% | 69 | Other fields |
Grace Gardener | Sunflower | sand_% | 64 | Same crop |
Show how their results compare to other samples in similar contexts.
library(ggplot2)
set.seed(123)
data_long |>
ggplot(aes(
x = measurement,
y = value,
color = category,
shape = category,
size = category,
alpha = category
)) +
geom_jitter(width = 0.2) +
# Define styles for producer's samples versus all samples
scale_alpha_manual(values = c(
"Other fields" = 0.5,
"Same crop" = 1,
"Your fields" = 1
)) +
scale_color_manual(values = c(
"Other fields" = "#CCC29C",
"Same crop" = "#76ADBC",
"Your fields" = "#A60F2D"
)) +
scale_size_manual(values = c(
"Other fields" = 2,
"Same crop" = 4,
"Your fields" = 4
)) +
washi::washi_theme(
legend_position = "bottom",
gridline_x = FALSE,
gridline_y = FALSE,
text_scale = 1.5
) +
theme(legend.title = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_rect(color = "gray", fill = NA))
Provide your reader the context to understand the results.
Evaluate plotly
code only for HTML reports and regular ggplot2
code for anything else.
Use out_type
in eval
chunk option.
Interactive plot for HTML reports:
Static plot for MS Word reports:
Check out an example HTML report.
Web Content Accessibility Guidelines (WCAG) Quick Reference
Inspect
toolMy three fave things: cats, nature, and R/Quarto.