#| include: false library(ggplot2) library(jcutils) jcutils::knitr_default_setup(echo = TRUE)
This vignette showcases the ability to output a list to a knitr report (Quarto or R Markdown) in the form of a tabset.
In the case of an unnamed list, the method defaults to numerical labelling.
We make use of the mtcars
dataset to illustrate typical usage.
data("mtcars", package = "datasets") vars <- stats::setNames(nm = c("disp", "hp", "drat")) gg_mtcars <- function(.x, ...) { ggplot2::qplot(mtcars[[.x]], mtcars$mpg, xlab = .x, ...) }
To affect all lists within a document, one can register a method for the S3
generic knit_print
which is explained here.
#| echo: true library(knitr) registerS3method("knit_print", "list", jcutils::printer_tabset)
Affecting a single chunk is achieved through the render
chunk option.
```r`r ''` #| render: !expr jcutils::printer_tabset myList ```
::: {.callout-note}
We don't pass the list to print
but rather make it visible either by calling
its reference name or a function that returns said list.
::::
ggplot2
)plotlist <- lapply(vars, gg_mtcars)
unname(plotlist)
plotlist
Nested lists work in Quarto but not in Rmd format. See #11
gt
)gtlist <- lapply(vars, function(.x) gt::gt(mtcars[1:5, c(.x, "mpg"), drop = FALSE]))
gtlist
kable
)kablelist <- lapply(vars, function(.x) knitr::kable(mtcars[1:5, c(.x, "mpg"), drop = FALSE]))
kablelist
https://cran.r-project.org/web/packages/knitr/vignettes/knit_print.html "Knitr - Custom Print Methods"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.