05:00
Another use case: 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 to create different variations of a report from a single .qmd document.
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 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 header---
title: "Swiss Cats" # Metadata
format: # Set format types
html:
toc: true # Set additional options
docx: default
params: # Set default parameter key-value pairs
fave_breed: "Snowshoe"
---
Report content goes here. # Write narrative and codeImportant
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.
paramsRun any line or chunk to add params to your environment.
paramsCmd/Ctrl + F to find where to replace hard-coded values with params.
paramsUse $ list notation in code for plot/table titles and labels, filtering, etc.
paste() syntax:
Modify 1-swiss-cats-demo.qmd to add pet_type and fave_breed parameters.
This parameterized version of 1-swiss-cats.qmd is the starting point for the next section’s exercises (2-quarto-render.qmd).