R/Medicine Data Cleaning 2023 Workshop taught by Crystal Lewis, Shannon Pileggi, and Peter Higgins
ASA Traveling Courses on Quarto taught by Mine Çetinkaya-Rundel and Andrew Bray
Opinions expressed are solely my own and do not express the views of my employer or any organizations I am associated with.
This work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA).
Data scientist at WA Dept of Agriculture
The Coding Cats: cat & code themed merch
Login to Posit Cloud workspace: bit.ly/rladies-parameterized-quarto.
If Posit Cloud doesn’t work, download materials locally:
Ask questions in the public Zoom chat or raise hand ✋.
Workshop structure: presentation, demos/exercises, questions to answer in 💬 Chat.
💬 Chat: share your name, where you’re calling from, and one thing you’ve made that you’re proud of.
01:00
2014+ magrittr pipe %>%
2021+ (R \(\geq\) 4.1.0) native R pipe |>
Isabella Velásquez’s blog post Understanding the native R pipe |> (2022)
To change shortcut to the native pipe:
Tools
→ Global Options
→ Code
→ Editing
→ Use Native Pipe Operator
Windows: Ctrl
+ Shift
+ M
Mac: Cmd
+ Shift
+ M
Slide adapted from R/Medicine Data Cleaning 2023 Workshop
package::function()
dplyr::select()
tells R explicitly to use the function select
from the package dplyr
helps avoid name conflicts (e.g., MASS::select()
)
does not require library(dplyr)
Slide adapted from R/Medicine Data Cleaning 2023 Workshop
Tools
→ Global Options
→
Fussy YAML indentation:
Code
→ Display
→ Indentation guides:
→ Rainbow lines
Match parentheses:
Code
→ Display
→ Indentation guides:
→ Check Use rainbow parentheses
Matching divs:
R Markdown
→ Advanced
→ Check Use rainbow fenced divs
Vast R Markdown ecosystem
Dependent on R
Command line interface (CLI)
Expands R Markdown ecosystem
“Batteries included”
Multi-language and multi-engine
If you’re happy with R Markdown and it’s not broken, no need to switch!
R Markdown will still be maintained but likely no new features (Xie 2022).
.Rmd
→ .qmd
.Rmd
→ .qmd
output:
→ format:
)knitr::convert_chunk_header()
No Quarto equivalent to .Rmd Knit with Parameters GUI built with Shiny {miniUI}.
. . .
Workaround: build web app to get input, serialize to YAML, pass to Quarto render.
More info: GitHub discussion
From R Markdown to Quarto workshop taught by Dr. Mine Çetinkaya-Rundel and Dr. Andrew Bray.
Ted Laderas’ talk Quarto/RMarkdown - What’s Different?
Different audiences, different reports
Show code for technical staff and hide code for everyone else (StackOverflow example).
YAML header with params
key-value pairs
Use these params
in your report to create different variations.
Important
Valid parameter values are strings, numbers, or Boolean.
Must serialize a dataframe to pass it as a parameter, then un-serialize it back to a dataframe within the .qmd
content.
See Christophe Dervieux’s answer in Posit Community to understand why.
See John Paul Helveston’s blog post to learn how to use {jsonlite} as a workaround.
Write report template with default values hard-coded, and then render & review.
Set default params
key-value pairs in YAML.
Replace hard-coded values with the params
variables.
Render the single report and review.
Render extreme cases and review.
Render all variations of the report at once.
Explore a report without parameters and see where we could add them.
Open ex-1-swiss-cats.qmd
.
Click the Render button.
Look at the source markdown & code and the rendered report.
💬 Chat: what variables could we set as parameters?
💡 Hint: run the setup
chunk and look at the pets
dataframe to see what variables it has.
05:00
params
in YAML headerImportant
Your default params
key-value pairs must be found in your dataset. Otherwise, code will error or output will be blank.
The variable name for params
can be anything you choose. Often, it’s a column name in your dataset.
params
Run any line or chunk to add params
to your environment.
params
Modify ex-1-swiss-cats.qmd
to add pet_type
and fave_breed
parameters.
Render button in RStudio or Cmd
/Ctrl
+ Shift
+ K
keyboard shortcut
Check Render on Save
and Cmd
/Ctrl
+ S
keyboard shortcut
Quarto CLI
✨ quarto::quarto_render()
Change parameters in the YAML and render using Render button.
Look at the unique pet breeds and pick your favorite.
In ex-2-quarto-render.qmd
Change the default parameters in the YAML to your favorite pet type and breed. Render using the Render button.
💬 Chat: do you have any pets? what kind? 🐈🐕🐹🐍🐠
05:00
Change parameters and render using quarto_render()
.
Render with quarto::quarto_render()
.
💬 Chat: what kinds of variables are you hoping to use as parameters for your reports?
5-min break after this exercise
05:00
05:00
One HTML report for each cat breed and each dog breed.
Change the default params
in the YAML.
Render button or Cmd
/Ctrl
+ Shift
+ K
keyboard shortcut.
Rename the rendered report to include the parameter & prevent overwriting.
Repeat 537 times.
😭
quarto::quarto_render(
input = here::here("ex-2-quarto-render.qmd"),
output_file = "dogs-affenpinscher-report.html",
execute_params = list(
pet_type = "dogs",
fave_breed = "Affenpinscher"))
quarto::quarto_render(
input = here::here("ex-2-quarto-render.qmd"),
output_file = "dogs-afghan-hound-report.html",
execute_params = list(
pet_type = "dogs",
fave_breed = "Afghan Hound"))
quarto::quarto_render(
input = here::here("ex-2-quarto-render.qmd"),
output_file = "dogs-aidi-chien-de-montagne-de-l-atlas-report.html",
execute_params = list(
pet_type = "dogs",
fave_breed = "Aidi Chien De Montagne De L Atlas"))
quarto::quarto_render(
input = here::here("ex-2-quarto-render.qmd"),
output_file = "dogs-akita-report.html",
execute_params = list(
pet_type = "dogs",
fave_breed = "Akita"))
# + 534 more times...
# 😭
Create a dataframe with three columns that match quarto_render()
args:
output_format
: file type (html, revealjs, pdf, docx, etc.)
output_file
: file name with extension
execute_params
: named list of parameters
Map over each row:
purrr::pwalk(dataframe, quarto_render, quarto_render_args)
😎
pet_reports <- pets |>
dplyr::distinct(pet_type, breed) |> # Get distinct pet/breed combos
dplyr::mutate(
output_format = "html", # Make output_format column
output_file = paste( # Make output_file column:
tolower(pet_type), # cats-abyssiniane-report.html
tolower(gsub(" ", "-", breed)),
"report.html",
sep = "-"
),
execute_params = purrr::map2( # Make execute_params column
pet_type,
breed,
\(pet_type, breed) list(pet_type = pet_type, breed = breed)))
pet_reports_subset <- pet_reports |>
dplyr::slice_head(n = 2, by = pet_type) |>
dplyr::select(output_file, execute_params)
pet_reports_subset
output_file | execute_params |
---|---|
cats-abyssiniane-report.html | cats , Abyssiniane |
cats-aegean-cat-report.html | cats , Aegean Cat |
dogs-affenpinscher-report.html | dogs , Affenpinscher |
dogs-afghan-hound-report.html | dogs , Afghan Hound |
purrr::pwalk()
iterates over multiple arguments simultaneously.
First .l
argument is a list of vectors.
.l
that iterates over rows.Note
index
is the only named argument of quarto_render()
included in pwalk()
.
output_format
, output_file
, and execute_params
are already passed in through the dataframe.
Add to the format:
YAML option to render additional output formats from the same .qmd
file.
---
format:
html:
embed-resources: true # Makes the report self-contained
pdf: default # If not using any additional format options,
docx: default # set value to `default`
---
Note: the Render button now has a dropdown!
Links to download the other formats will automatically appear in HTML documents.
Choose which format links to include:
Demo programmatically rendering all reports in all formats in ex-3-render-reports.R
.
Explore the programmatic way of rendering multiple reports at once.
Look through ex-3-render-reports.qmd
and ex-3-render-reports.R
.
Source
the R script (or run line by line).
💬 Chat: were any of these functions from the .qmd
or .R
files new to you?
Ask questions or take a break. 😊
10:00
Can’t render reports to another directory.
output-dir
YAML option only works for Quarto projects that contain a _quarto.yml
config file.
Workaround: use {fs}
to move files after rendering.
See ex-3-render-reports.R
for example.
More info: GitHub discussion and GitHub issue.
If using embed-resources: true
YAML option, .qmd
can’t be in subfolder, otherwise:
[WARNING] Could not fetch resource …
More info: GitHub discussion and GitHub issue.
Special .content-visible
and .content-hidden
classes with when-format="___"
and unless-format="___"
attributes can be applied to:
Examples from Conditional Content Quarto docs
Pairs well with {{< include >}}
shortcodes to re-use content without copying/pasting.
Use an underscore prefix for included files so they are automatically ignored by a Quarto render of a project (Includes Quarto Docs).
Use conditional content divs to control when tabsets are shown.
ex-4-conditional-content.qmd
so that the panel-tabset
is visible for HTML reports and hidden for all other formats.Explore the other options. There are multiple ways to get the same result.
{.content-visible when-format=“html”} == {.content-hidden unless-format=“html”}
05:00
More efficient to not execute code that generates interactive outputs for static reports.
Useful for executing interactive plot code for HTML reports and static ggplot2
code for all other formats.
Not currently a feature of Quarto v1.4. Follow along with this GitHub discussion.
Include in the setup chunk of your .qmd
file.
Get the format of the Pandoc output:
eval: !expr
chunk optionConditionally execute ggplot2
code for static reports & plotly
code for interactive reports.
Open ex-5-conditional-code.qmd
.
Fill in the blanks for the eval:
option for ggplot
code chunks and plotly
code chunks.
05:00
Understand what parameterized reporting is and when it is useful.
Like very fancy custom functions:
Function → .qmd
template
Input → parameters
Output → rendered reports
Useful for creating variations of the same report:
country, state, county, or city
time periods
breeds, species, diseases, trials, etc.
Note
We only covered reports, but you can also parameterize revealjs
presentations! See this Jumping Rivers blog post about it.
Learn how to convert a report into a parameterized template.
Render all variations of the report at once using {quarto} and {purrr}.
Generate multiple format outputs from the same template with conditional content and code.
{.content-visible when-format=“___“}
{.content-visible unless-format=“___“}
{.content-hidden unless-format=“___“}
{.content-hidden unless-format=“___“}
🙏🏼 Please let me know how I did in this short survey
🏡 Home for all workshop materials: jadeyryan.quarto.pub/rladies-abuja-quarto-params/
Parameterized Reporting with Quarto // jadeyryan.quarto.pub/rladies-abuja-quarto-params/