Create tabsets panel from R code

About this example

In this example, we are presenting several tables inside a tabset panel. Tabset panels are among built-in component layout for Quarto.

Each HTML table is made using gt and shows a subset of dplyr::starwars characters, based on their homeworld, one per pane. Instead of created each tab one by one, knitr child feature is leveraged to dynamically create content.

Tabset panel should be on the form

# Tables

::: {.panel-tabset}

## homeworld1 

```{r}
# code with gt table on data filtered on homeworld1
```

## homeworld2

```{r}
# same code for gt table but on data filtered on homeworld2
```

... and so on for all homeworld

:::

This can be done using R code directly by having the code for each pane inside a child document, that will be rendered for each homeworld:

  • index.qmd is the main document. It contains a chunk that will call knitr::knit_child() to render from a child document based on changing values

  • _child.qmd is the child document. The _ prefix indicates it is a document not to render on its own (See more about Render Targets for Projecs). It contains the markdown structure to create each pane:

    ## `r hw`
    
    
    ```{r}
    #| echo: false
    starwars %>%
      filter(homeworld == hw) %>%
      count(sex) %>%
      head() %>%
      gt()
    ```

Look at the source of this example for details.

Tables

sex n
female 2
male 6
none 2
sex n
female 3
male 5
none 1
NA 2
sex n
female 1
male 2
sex n
male 1
sex n
male 1
sex n
male 2
sex n
male 2
sex n
male 1
sex n
hermaphroditic 1
sex n
male 1
sex n
sex n
female 1
male 2
sex n
male 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
female 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
female 2
male 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
female 1
male 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
female 2
sex n
male 1
sex n
male 1
sex n
female 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
male 1
sex n
female 1
sex n
male 1
sex n
NA 1
sex n
male 1

Resources

Learn more about this knitr feature in the R Markdown Cookbook (Xie, Dervieux, and Riederer 2020), where some recipies are also applicable to Quarto:

References

Xie, Yihui, Christophe Dervieux, and Emily Riederer. 2020. R Markdown Cookbook. Boca Raton, Florida: Chapman; Hall/CRC. https://bookdown.org/yihui/rmarkdown-cookbook.