rquarto

Dynamically create tabs in quarto


I want to dynamically create tabs in quarto using the iris dataset.

One tab for each species, and print the table for that species in the tab.

Here is my quarto code so far:

---
title: iris tabset
format: 
  html:
    df-print: paged
knitr:
  opts_chunk: 
    echo: false
---

 ```{r}
#| label: setup
#| include: false
# Libraries
library(tidyverse)

    ```

::: {.tabset}

```{r} 
#| output: asis 

res <- purrr::map_chr(unique(iris$Species), \(species) {
    knitr::knit_child(text = c(
      "## `r species`",
      "", 
      "```{r}",
      "#| echo: false",
      "iris %>%",
      "  filter(Species == species) ",
      
      "```",
      "",
      ""
    ), envir = environment(), quiet = TRUE)
  })

cat(res, sep = '\n')


    ```

:::

The code print the tables but they aren't in their own tabs.

Can anyone fix the code so that the tabs are created as described.


Solution

  • Your code runs as is, if you replace

    ::: {.tabset}
    

    with

    ::: {.panel-tabset .nav-pills}
    

    enter image description here