Conditional content & code

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

Conditional content

Control content visibility

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

{.class attribute="format"}

Class

  • .content-visible
  • .content-hidden

Attribute

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

Format

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

Examples:

::: {.content-visible when-format="html"}

Will only appear in HTML.

:::
Some text
[in HTML.]{.content-visible when-format="html"}
[in PDF.]{.content-visible when-format="pdf"}

Fenced code blocks purely for documentation.

```{.python .content-visible when-format="html"}
# code shown only in HTML
2 + 2
```

Useful for static/interactive features

Pairs well with the {{< include >}} shortcode to re-use content without copying/pasting.

:::: {.content-visible unless-format="html"}

## Cats

{{< include _cats.qmd >}}

## Dogs

{{< include _dogs.qmd >}}

::::
:::: {.content-visible when-format="html"}
::: panel-tabset

## Cats

{{< include _cats.qmd >}}

## Dogs

{{< include _dogs.qmd >}}

:::
::::

Good practice

Use an underscore prefix for included files so they are automatically ignored by a Quarto render of a project (Include shortcode Quarto docs).

πŸ’ͺ🏼 Exercise

Use conditional content divs to control when tabsets are shown.

  1. Modify 3-conditional-content.qmd so that the panel-tabset is visible for HTML reports and hidden for all other formats.


  1. Try another way to get the same result.

    {.content-visible when-format="html"} is essentially the same as {.content-hidden unless-format="html"}.


  1. πŸ’¬ Chat: Besides tabsets, what other kinds of content might you want to make visible for only a certain format?
05:00

Conditional code execution

Conditionally execute a code chunk

  • More efficient to not execute code that generates interactive outputs for static reports.

  • Useful for executing interactive plot code (e.g., plotly or ggiraph) for HTML reports and static ggplot2 code for all other formats.

  • Useful for executing different code based on a parameter value.

  • Not currently a feature of Quarto v1.4. Follow along with this GitHub discussion.

  • Chunk options can use R code for option values with !expr. Learn about the limitations to this YAML β€œtag” literal syntax in the Quarto Chunk Options reference.

Conditional code based on output

Get the format of the Pandoc output by including the following in the setup chunk of your .qmd file:

```{r}
#| label: setup

# Get output format
format <- knitr::opts_knit$get("rmarkdown.pandoc.to")
```

Use eval: !expr chunk option

#| echo: fenced
#| eval: !expr format %in% c("latex", "docx")

# code to create static {ggplot2}

Use latex not pdf

format comes from knitr::opts_knit$get("rmarkdown.pandoc.to"). Pandoc uses LaTeX to create PDFs.

Quarto format aliases won’t work here.

#| echo: fenced
#| eval: !expr format == "html"

# code to create interactive {plotly}

πŸ’ͺ🏼 Exercise

Conditionally execute ggplot2 code for static reports & plotly code for interactive reports.

  1. Open 5-conditional-code.qmd.

  2. In the ggplot2 code chunks and plotly code chunks, fill in the blanks for the eval: option.

# Replace ___ with html, latex, or docx.

#| eval: !expr format == "___"

#| eval: !expr format %in% c("___", "___")
  1. πŸ’¬ Chat: How would you change the eval: option to execute a chunk based on a parameter value rather than the output format?
05:00

Conditional code based on parameter

Use params in !expr:

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

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


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

# Code for a different plot for all other breeds.
# Note the ! in front of params.