9

I've provided a small reproducible example below. I would like to generate tabs in quarto for each of the ggplot objects within a named list plots. The below quarto document would render the figures in their own 2nd level heading, but not in tabs as expected.

---
title: "Untitled"
format: html
---

```{r}
library(tidyverse)

data <- iris %>% as_tibble()

plots <- data %>%
  group_nest(Species) %>% 
  deframe() %>% 
  map(., ~ {
    ggplot(.x, aes(x = Sepal.Length, y = Sepal.Width)) + 
      geom_point()
  })

```

# Iris Plots 

::: {.panel-tabset}
```{r}
#| column: screen
#| fig-width: 12
#| fig-height: 8
#| fig-align: center
#| results: asis

iwalk(plots, ~ {
  cat('## ', .y, '\n\n')
  
  print(.x)
  
  cat('\n\n')
  
})

```

:::

The document would correctly render the plots within tabs as expected when the chunk-options (all except results:asis) were removed.

# Iris Plots 

::: {.panel-tabset}
```{r}
#| results: asis

iwalk(plots, ~ {
  cat('## ', .y, '\n\n')
  
  print(.x)
  
  cat('\n\n')
  
})

```

:::
Michael Luu
  • 509
  • 3
  • 10

1 Answers1

10

Updated Answer

Now if you want to generate tabsets which would expand the whole width of the screen, wrap the .panel-tabset div with the .column-screen div.

Note that, we have to use more : for .column-screen than that of .panel-tabset div. We have used three : for .panel-tabset, so we have to use four more of the : to create div for column-screen.


---
title: "Panel tabs"
format: html
---

```{r}
library(tidyverse)

data <- iris %>% as_tibble()

plots <- data %>%
  group_nest(Species) %>% 
  deframe() %>% 
  map(., ~ {
    ggplot(.x, aes(x = Sepal.Length, y = Sepal.Width)) + 
      geom_point() +
      theme_bw(
        base_size = 18 # to increase the size of the plot elements
      )
  }) 

```

# Iris Plots 

:::: {.column-screen}
::: {.panel-tabset}
```{r}
#| results: asis
#| fig-width: 14
#| fig-height: 6

iwalk(plots, ~ {
  cat('## ', .y, '\n\n')
  
  print(.x)
  
  cat('\n\n')
  
})

```

:::
:::: 


Now the panel tabsets are expanded to the width of the screen.

panel_tab_with_screen_width

Also Note that, I have increased the size of elements of the plot (e.g. axis.title, axis.text etc.) with base_size = 18 in plot-theme.


Old Answer

Your first approach would work if you just remove the column: screen and fig-align: center from the chunk option.

Because these two chunk options are preventing .panel-tabset from properly creating the divs for rendering tabsets.

So this works after removing these two chunk option (and you don't need fig-align: center since figures in tabsets are by default centered.


# Iris Plots 

::: {.panel-tabset}
```{r}
#| fig-width: 12
#| fig-height: 8
#| results: asis

iwalk(plots, ~ {
  cat('## ', .y, '\n\n')
  
  print(.x)
  
  cat('\n\n')
  
})

```
shafee
  • 15,566
  • 3
  • 19
  • 47
  • 2
    [R Workflow](https://hbiostat.org/rflow) shows how to use some functions to generate tabs and other Quarto elements. – Frank Harrell Aug 16 '22 at 13:06
  • Thank you all for the suggestions. I would ideally like to be able to adjust the chunk options such as columns: screen, which would produce tabs for the figures that expands the width of the screen. – Michael Luu Aug 16 '22 at 22:44
  • 1
    @MichaelLuu, see my update. – shafee Aug 17 '22 at 08:49
  • Great thank you so much! That did the trick! I've accepted the answer – Michael Luu Aug 17 '22 at 14:43
  • @shafee thanks for this! very interesting. i have another list of tables and would like to put it in the margin next to the plot, but still under the tabset. do you have any suggestions for how to do this? i'm trying to use `#| column: margin` but this has to be in a new chunk. – A. Piong Oct 26 '22 at 11:43
  • I would recommend to create a new question with a reprex stating clearly your expected output if your question is not directly a duplicate of existing ones :) – shafee Oct 26 '22 at 11:52