How to loop stuff in tabset - quarto edition

ish
Auteur

Simon Coulombe

Published

May 11, 2024

Objective

Every time I build a new report I want to put something in programatically-generated tabs. Every time, I have to dig old code somewhere because I don’t remember how I did it last time. With the move the quarto (I’m 2 years late), I decided to try and save it all in the same spot.

Here is my guide on how to add the following type of outputs inside tabsets in quarto:
* knitr::kable()
* gt::gt()
* datatable::DT()
* ggplot2
* plotly::ggplotly()
* leaflet::leaflet()
* mapview::mapview()

Do click the </> icon next to the title to jump to the source code on github.

kable

kable needs to be wrapped inside print().

species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Biscoe 37.8 18.3 174 3400 female 2007
Adelie Biscoe 37.7 18.7 180 3600 male 2007
Adelie Biscoe 35.9 19.2 189 3800 female 2007
Adelie Biscoe 38.2 18.1 185 3950 male 2007
Adelie Biscoe 38.8 17.2 180 3800 male 2007
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Dream 39.5 16.7 178 3250 female 2007
Adelie Dream 37.2 18.1 178 3900 male 2007
Adelie Dream 39.5 17.8 188 3300 female 2007
Adelie Dream 40.9 18.9 184 3900 male 2007
Adelie Dream 36.4 17.0 195 3325 female 2007
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Torgersen 39.1 18.7 181 3750 male 2007
Adelie Torgersen 39.5 17.4 186 3800 female 2007
Adelie Torgersen 40.3 18.0 195 3250 female 2007
Adelie Torgersen NA NA NA NA NA 2007
Adelie Torgersen 36.7 19.3 193 3450 female 2007

gt

gt needs to be wrapped inside print(tagList()).

species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Biscoe 37.8 18.3 174 3400 female 2007
Adelie Biscoe 37.7 18.7 180 3600 male 2007
Adelie Biscoe 35.9 19.2 189 3800 female 2007
Adelie Biscoe 38.2 18.1 185 3950 male 2007
Adelie Biscoe 38.8 17.2 180 3800 male 2007
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Dream 39.5 16.7 178 3250 female 2007
Adelie Dream 37.2 18.1 178 3900 male 2007
Adelie Dream 39.5 17.8 188 3300 female 2007
Adelie Dream 40.9 18.9 184 3900 male 2007
Adelie Dream 36.4 17.0 195 3325 female 2007
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Torgersen 39.1 18.7 181 3750 male 2007
Adelie Torgersen 39.5 17.4 186 3800 female 2007
Adelie Torgersen 40.3 18.0 195 3250 female 2007
Adelie Torgersen NA NA NA NA NA 2007
Adelie Torgersen 36.7 19.3 193 3450 female 2007

datatable

datatable needs to be wrapped inside print(tagList()) AND having initialized the datatable library before (really!).
See the answer by Stefan here: https://stackoverflow.com/questions/74707808/programatically-generate-tabset-panels-of-datatables-in-quarto

ggplot

gplot needs to be wrapped inside print().

plotly

plotly needs to be wrapped inside print(tagList()) AND be initialized by running plot_ly() before hand, similar to datatable:

leaflet

mapview

mapview needs to be wrapped inside print(tagList()) AND be initialized by running plot_ly() before hand, similar to datatable. You also need to extract the @map from the mapview object before printing it.

all in one

you cal absolutely put everything together ..

species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Biscoe 37.8 18.3 174 3400 female 2007
Adelie Biscoe 37.7 18.7 180 3600 male 2007
Adelie Biscoe 35.9 19.2 189 3800 female 2007
Adelie Biscoe 38.2 18.1 185 3950 male 2007
Adelie Biscoe 38.8 17.2 180 3800 male 2007
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Biscoe 37.8 18.3 174 3400 female 2007
Adelie Biscoe 37.7 18.7 180 3600 male 2007
Adelie Biscoe 35.9 19.2 189 3800 female 2007
Adelie Biscoe 38.2 18.1 185 3950 male 2007
Adelie Biscoe 38.8 17.2 180 3800 male 2007
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Dream 39.5 16.7 178 3250 female 2007
Adelie Dream 37.2 18.1 178 3900 male 2007
Adelie Dream 39.5 17.8 188 3300 female 2007
Adelie Dream 40.9 18.9 184 3900 male 2007
Adelie Dream 36.4 17.0 195 3325 female 2007
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Dream 39.5 16.7 178 3250 female 2007
Adelie Dream 37.2 18.1 178 3900 male 2007
Adelie Dream 39.5 17.8 188 3300 female 2007
Adelie Dream 40.9 18.9 184 3900 male 2007
Adelie Dream 36.4 17.0 195 3325 female 2007
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Torgersen 39.1 18.7 181 3750 male 2007
Adelie Torgersen 39.5 17.4 186 3800 female 2007
Adelie Torgersen 40.3 18.0 195 3250 female 2007
Adelie Torgersen NA NA NA NA NA 2007
Adelie Torgersen 36.7 19.3 193 3450 female 2007
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Torgersen 39.1 18.7 181 3750 male 2007
Adelie Torgersen 39.5 17.4 186 3800 female 2007
Adelie Torgersen 40.3 18.0 195 3250 female 2007
Adelie Torgersen NA NA NA NA NA 2007
Adelie Torgersen 36.7 19.3 193 3450 female 2007

Reuse

Citation

BibTeX citation:
@online{coulombe2024,
  author = {Coulombe, Simon},
  title = {How to Loop Stuff in Tabset - Quarto Edition},
  date = {2024-05-11},
  url = {https://www.simoncoulombe.com/posts/2024-05-11-how-to-loop-stuff-in-tabset--quarto-edition},
  langid = {en}
}
For attribution, please cite this work as:
Coulombe, Simon. 2024. “How to Loop Stuff in Tabset - Quarto Edition.” May 11, 2024. https://www.simoncoulombe.com/posts/2024-05-11-how-to-loop-stuff-in-tabset--quarto-edition.