🏁 Summary

Jadey Ryan // June 21, 2024
Intermediate Quarto // Cascadia R Conf

Learning objective 1

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:

  • Spatial: country, state, county, or city

  • Temporal: year, month or other time period

  • Anything you can filter by: breeds, species, diseases, water bodies, customers, trials, etc.

Note

We only covered reports, but you can also parameterize revealjs presentations! See this Jumping Rivers blog post about it.

Learning objective 2a

Convert a Quarto document into a parameterized template.

  • Include default params: in YAML

  • Replace hard-coded values with params$pet_type

    • YAML:

      ---
      title: "Report about `r params$pet_type`"
      params:
        pet_type: "cats"
      ---
    • Inline R code:

      I like **`r params$pet_type`**.
    • Code chunks:

      pets |> 
          dplyr::filter(pet_type == params$pet_type)

Learning objective 2b

Render all variations of the report at once using {quarto} and {purrr}.

  1. Get all unique parameter combinations into a dataframe:
head(pet_reports, 2)
pet_type breed output_format output_file execute_params
cats Abyssiniane html cats-abyssiniane-report.html cats , Abyssiniane
cats Aegean Cat html cats-aegean-cat-report.html cats , Aegean Cat
  1. Use dataframe in pwalk() with quarto_render():
purrr::pwalk(
  pet_reports,
  quarto::quarto_render,
  input = here::here("pet_template.qmd"),
  .progress = TRUE
)

Learning objective 3a

Generate multiple format outputs from the same .qmd using conditional content.

Create a div, span, or non-executable code block with one option from each of the below columns:

Class

  • .content-visible
  • .content-hidden

Attribute

  • when-format="___"
  • unless-format="___"

Format

  • latex or pdf
  • epub
  • html or revealjs
  • markdown

Example to show tabset only for HTML reports:

:::: {.content-visible when-format="html"}
::: panel-tabset
{{< include _4-report-content.qmd >}}
:::
::::

Learning objective 3b

Generate multiple format outputs from the same .qmd using conditional code execution.

  1. Get the Pandoc output format.
#| label: setup

format <- knitr::opts_knit$get("rmarkdown.pandoc.to")
  1. Use format in the eval: !expr chunk option.
#| label: interactive-plot
#| eval: !expr format == "html"

# plotly code

Use params in the eval: !expr chunk option.

#| eval: !expr params$fave_breed == "Snowshoe"

# Code for a special plot for my favorite cat breed.

Learning objective 3c

Generate multiple format outputs from the same .qmd using custom styling.

  1. Pick a Bootswatch theme.

  2. Customize with a .scss file.

  3. Use browser developer tools to find/test more styling.

format:
  html:
    theme: [flatly, theme.scss]
  1. Create the reference doc.
Terminal
quarto pandoc -o word-template.docx --print-default-data-file reference.docx
  1. Open word-template.docx and modify the styles.

  2. Set this template in the YAML under the reference-doc: key:

format:
  docx:
    reference-doc: word-template.docx

Thank you!

🏑 Home for all workshop materials: jadeyryan.quarto.pub/cascadia-quarto/


πŸŽ₯ Recordings from previous workshops & talks:
links in GitHub repo or my YouTube playlist

From left to right, Mai, Tai, and Skye. Three snowshoe cats cuddling in their warming beds.