quarto_chunks: Generate Quarto knitr code chunk snippets from structured...

quarto_chunksR Documentation

Generate Quarto knitr code chunk snippets from structured data

Description

Generates Quarto Markdown (.qmd) file snippets with knitr figure or table code chunks from structured data.

Usage

quarto_chunks(data, env = parent.frame())

Arguments

data

Input data as returned by read_quarto_chunk_data(). A dataframe where column names correspond to chunk options supported by quarto_fig_chunk() or quarto_tbl_chunk() plus the optional iteration variable columns itr_vars and itr_vars_r. Columns body, label and fig_cap/tbl_cap are mandatory. See section Input data for details.

env

Environment to evaluate iterations in (see section Iteration variables for details).

Details

The generated .qmd files are intended to be included in other Quarto documents via the built-in include shortcode.

Value

data, invisibly.

Input data

All data columns of type character are cli::pluralize()d, meaning literal curly brackets (⁠{⁠ and ⁠}⁠) need to be escaped by doubling them (to ⁠{{⁠ and ⁠}}⁠).

The column names of data can either correspond to quarto_fig_chunk()/quarto_tbl_chunk()'s argument names (in snake case), to knitr's figure chunk option names (period-separared) or directly to Quarto's figure/table code chunk option names (in kebab case) – they will be converted as needed.

Additional column names are silently ignored, except for the optional iteration variable columns itr_vars (of type list) and itr_vars_r (of type character).

Iteration variables

Specifying iteration variables allows to produce multiple code chunks per data row. The iteration variables can be referred to in the other columns via cli::pluralize()'s string interpolation syntax (i.e. R code in curly brackets).

The itr_vars column must be a list of named lists (or NULL for no iteration on the respective rows). The itr_vars_r column allows to specify itr_vars as a string of R code (which must yield a list of named lists / NULLs when parsed and executed).

See Also

Other Quarto functions: quarto_fig_chunk(), quarto_tbl_chunk(), read_quarto_chunk_data()

Examples

# data row without iterations
tibble::tibble(label = "fig-cars",
               fig_cap = "Today's ({lubridate::today()}) top figure",
               fig_column = "page",
               fig_height = 8L,
               body = "plot(cars)") |>
  salim::quarto_chunks()

# data row with iteration variables specified directly as `itr_vars`
tibble::tibble(itr_vars = list(list(v = "cyl"),
                               list(v = "disp"),
                               list(v = "hp")),
               label = "fig-mpg-by-{v}",
               fig_cap = "Motor Trend Car Road Tests: mpg per {v}",
               fig_column = "screen",
               fig_height = 8L,
               body = "plot(x = mtcars$mpg, y = mtcars${v})") |>
  salim::quarto_chunks()

# data row with iteration variables specified indirectly as `itr_vars_r` code
tibble::tibble(itr_vars_r = "purrr::map(colnames(mtcars)[5:7], \\(x) list(v = x))",
               label = "fig-mpg-by-{v}",
               fig_cap = "Motor Trend Car Road Tests: mpg per {v}",
               fig_column = "screen",
               fig_height = 8L,
               body = "plot(x = mtcars$mpg, y = mtcars${v})") |>
  salim::quarto_chunks()

salim-b/salim documentation built on May 18, 2024, 7:08 a.m.