Skip to contents

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

Usage

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

Arguments

data

Input data as returned by read_chunk_toml(). A dataframe where column names correspond to chunk options supported by fig_chunk() or 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).

Value

data, invisibly.

Details

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

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 fig_chunk()/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 code chunk functions: fig_chunk(), read_chunk_toml(), tbl_chunk()

Examples

# data row without iterations
tibble::tibble(label = "fig-cars",
               fig_cap = "Today's ({clock::date_today(zone = Sys.timezone())}) top figure",
               fig_column = "page",
               fig_height = 8L,
               body = "plot(cars)") |>
  quappo::chunks()
#> $`fig-cars`
#> ```{r}
#> #| label: fig-cars
#> #| fig-cap:
#> #|   - Today's (2024-10-06) top figure
#> #| fig-column: page
#> #| fig-height: 8
#> #| fig-pos: H
#> 
#> plot(cars)
#> ```
#> 
#> 

# 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})") |>
  quappo::chunks()
#> $`fig-mpg-by-cyl`
#> ```{r}
#> #| label: fig-mpg-by-cyl
#> #| fig-cap:
#> #|   - Motor Trend Car Road Tests: mpg per cyl
#> #| fig-column: screen
#> #| fig-height: 8
#> #| fig-pos: H
#> 
#> plot(x = mtcars$mpg, y = mtcars$cyl)
#> ```
#> 
#> 
#> $`fig-mpg-by-disp`
#> ```{r}
#> #| label: fig-mpg-by-disp
#> #| fig-cap:
#> #|   - Motor Trend Car Road Tests: mpg per disp
#> #| fig-column: screen
#> #| fig-height: 8
#> #| fig-pos: H
#> 
#> plot(x = mtcars$mpg, y = mtcars$disp)
#> ```
#> 
#> 
#> $`fig-mpg-by-hp`
#> ```{r}
#> #| label: fig-mpg-by-hp
#> #| fig-cap:
#> #|   - Motor Trend Car Road Tests: mpg per hp
#> #| fig-column: screen
#> #| fig-height: 8
#> #| fig-pos: H
#> 
#> plot(x = mtcars$mpg, y = mtcars$hp)
#> ```
#> 
#> 

# 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})") |>
  quappo::chunks()
#> $`fig-mpg-by-drat`
#> ```{r}
#> #| label: fig-mpg-by-drat
#> #| fig-cap:
#> #|   - Motor Trend Car Road Tests: mpg per drat
#> #| fig-column: screen
#> #| fig-height: 8
#> #| fig-pos: H
#> 
#> plot(x = mtcars$mpg, y = mtcars$drat)
#> ```
#> 
#> 
#> $`fig-mpg-by-wt`
#> ```{r}
#> #| label: fig-mpg-by-wt
#> #| fig-cap:
#> #|   - Motor Trend Car Road Tests: mpg per wt
#> #| fig-column: screen
#> #| fig-height: 8
#> #| fig-pos: H
#> 
#> plot(x = mtcars$mpg, y = mtcars$wt)
#> ```
#> 
#> 
#> $`fig-mpg-by-qsec`
#> ```{r}
#> #| label: fig-mpg-by-qsec
#> #| fig-cap:
#> #|   - Motor Trend Car Road Tests: mpg per qsec
#> #| fig-column: screen
#> #| fig-height: 8
#> #| fig-pos: H
#> 
#> plot(x = mtcars$mpg, y = mtcars$qsec)
#> ```
#> 
#>