| Title: | Dynamically Generate Tabset Panels in 'Quarto' HTML Documents |
| Version: | 0.2.0 |
| Description: | Dynamically generate tabset panels https://quarto.org/docs/output-formats/html-basics.html#tabsets in 'Quarto' HTML documents using a data frame as input. |
| License: | MIT + file LICENSE |
| URL: | https://sayuks.github.io/quartabs/, https://github.com/sayuks/quartabs |
| BugReports: | https://github.com/sayuks/quartabs/issues |
| Suggests: | altdoc, dplyr (≥ 1.0.0), DT, flextable, gt (≥ 0.9.0), htmltools, knitr, plotly, purrr, quarto, reactable, sessioninfo, spelling, testthat (≥ 3.0.0), tibble, tidyr, tinytable, utils |
| Config/testthat/edition: | 3 |
| Encoding: | UTF-8 |
| Language: | en-US |
| RoxygenNote: | 7.3.2 |
| NeedsCompilation: | no |
| Packaged: | 2026-11-22 08:45:33 UTC; sayuks |
| Author: | Yusuke Sasaki [aut, cre] |
| Maintainer: | Yusuke Sasaki <sayuks.dev@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-11-22 10:20:02 UTC |
Dynamically Generate Tabset Panels in Quarto HTML Documents
Description
render_tabset() takes a data frame as input and outputs the markdown
that generates the tabset
to stdout (console). Only works with Quarto HTML documents.
See Get started for details.
Usage
render_tabset(
data,
tabset_vars,
output_vars,
layout = NULL,
heading_levels = NULL,
active = NULL,
pills = FALSE,
tabset_width = "default"
)
Arguments
data |
A data frame. |
tabset_vars |
Columns to use as tabset labels. Internally passed
to the |
output_vars |
Columns to display in each tabset panel. Internally
passed to the |
layout |
|
heading_levels |
|
active |
Functions are applied before the conversion of factor, Date and POSIXt
columns to character, so numbers are compared as numbers and factors
keep their levels. A character value is compared with the tab labels
(e.g. |
pills |
Logical, use pills or not.
See https://getbootstrap.com/docs/5.2/components/navs-tabs/#pills
for details. If |
tabset_width |
Character, one of "default", "fill" and "justified".
See https://getbootstrap.com/docs/5.2/components/navs-tabs/#fill-and-justify
for details. If |
Details
Write
#| results: asisat the beginning of the chunk orresults='asis'in the chunk options.If multiple
tabset_varsare given, create nested tabsets.For columns specified in
output_vars, columns of type list are output withprint()and normal columns are output withcat().The
datais sorted internally bytabset_vars, so tabs are displayed in ascending order. Factor columns are sorted by their levels (before the conversion to character described below), so the tab order can be controlled with the levels. For example,factor(x, levels = unique(x))keeps the order of first appearance in the data. Since levels apply to the whole column, nested tabs cannot have a different order under each parent tab.If
tabset_varsoroutput_varshave "factor", "Date" and "POSIXt" columns, they are converted internally to character. This is to prevent it being displayed as numeric whencat()is executed. Sorting bytabset_varsis performed before conversion to string.
Value
NULL invisibly. This function outputs the markdown
that generates the tabset
to stdout (console).
Limitations
-
layoutis intended for simplified use cases and complex layouts may not work. When outputting tables or figures that use JavaScript (such as
{plotly},{leaflet},{DT},{reactable}, etc.), it seems JavaScript dependencies need to be resolved. A simple solution is to wrap the output inhtmltools::div()and create a dummy plot in another chunk. See the Get started for details.When
tabset_varsandoutput_varshave the following columns, they may not display well:A column of type list contains a named vector or list (This is for
output_vars.tabset_varsmust not contain list columns).Classes with their own printing methods, such as "difftime", "ts", .etc.
When specifying a list-type column that includes ggplot objects in
output_vars, setting the chunk optionecho: fencedmay cause the plots to not display correctly.If the chunk label starts with
fig-(e.g.#| label: fig-example), Quarto holds all plots until the end of the chunk, so they are displayed below the tabset instead of inside each tab panel. Use a label that does not start withfig-, or no label. To make each plot a cross-referenceable figure, see the Get started for details.
References
As this function is focused on quickly and dynamically
generating tabsets and chunks, it is difficult to customize it on a
chunk-by-chunk basis. The regular way to dynamically create chunks is
to use functions such as knitr::knit(), knitr::knit_child(),
knitr::knit_expand(), etc. For more information on these,
see the following links.
Heiss, Andrew. 2024. “Guide to Generating and Rendering Computational Markdown Content Programmatically with Quarto.” November 4, 2024. doi:10.59350/pa44j-cc302.
-
https://pkg.yihui.org/rmarkdown-cookbook/child-document.html#child-document
Examples
# sample data
df <- data.frame(
group1 = c(rep("A", 3), rep("B", 3)),
group2 = rep(c("X", "Y", "Z"), 2),
value1 = 1:6,
value2 = letters[1:6]
)
# Here are examples of the output before it is converted to tabset.
# If you want it to actually work, in the .qmd file,
# set `results='asis'` in the chunk options or
# write `#| results: asis` at the beginning of the chunk.
# Basic usage
render_tabset(df, group1, value1)
# Nested tabset, two outputs side by side with a width of 1:1
render_tabset(
df,
c(group1, group2),
c(value1, value2),
layout = "::: {layout-ncol=2}"
)
# Use heading instead of tabset
render_tabset(
df,
c(group1, group2),
value1,
heading_levels = c(2, 3)
)
# Show tab "B" and, under each tab, the last nested tab by default
render_tabset(
df,
c(group1, group2),
value1,
active = list(group1 = "B", group2 = max)
)