2

I would like to create nested tabs in a Quarto document. I wrote a custom function called nested_tabs which takes the names of a nested list for the tab names and should plot the graphs at each corresponding tab. Unfortunately, output is not expected. Somehow it doesn't show graph but the path of graph. Here is some simple reproducible example code:

---
title: "test"
format: html
---

```{r}
#| echo: false
#| message: false
#| warning: false
library(tidyverse)

# example list
example_list <- list(
  tab1 = list(test1.1 = ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
                geom_point(),
              test1.2 = ggplot(iris, aes(x = Petal.Length, y = Sepal.Width))+
                geom_point()),
  tab2 = list(test2 = ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length))+
                geom_point())
)

# function
nested_tabs <- function(nested_list) {
 
  main_tab <- names(nested_list)
 
  cat(':::: {.panel-tabset} \n') # main tab
  
  for (i in main_tab) {
    id <- which(main_tab == i)
    cat("##", as.character(i), "\n\n")
 
    cat("::: {.panel-tabset} \n\n") # sub tab
 
    for (j in 1:length(names(nested_list[[id]]))) {
      cat("###", names(nested_list[[id]])[j], "\n\n")
      cat("```{r}\n")
      base::print(nested_list[[id]][[j]])
      cat("\n```\n\n")
    }
    cat("\n:::\n\n")
  }
  cat("::::")
}
```


```{r}
#| results: asis
#| echo: false
#| message: false
#| warning: false

nested_tabs(example_list)
```

Output:

enter image description here

As you can see, the tabs are working but not the output. So I was wondering if anyone knows how we can make these nested tabs?

0

1 Answer 1

3

Instead of your inner for loop, you can purrr::iwalk() over your plot_list[[i]] as shown here. Optionally, you may also set the fig-width / fig-height of your as-is-chunk.

out1

Code

---
title: "test"
format: html
---

```{r}
#| echo: false
#| message: false
#| warning: false
library(ggplot2)
library(purrr)

plot_list <- list(
  tab1 = list(test1.1 = ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
                geom_point(),
              test1.2 = ggplot(iris, aes(x = Petal.Length, y = Sepal.Width))+
                geom_point()),
  tab2 = list(test2 = ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length))+
                geom_point())
)

nested_tabs <- \(nested_list) {
  cat(':::: {.panel-tabset} \n') # main tab
  for (i in names(nested_list)) {
    cat("##", as.character(i), "\n\n")
    cat("::: {.panel-tabset} \n\n") # sub tab
    purrr::iwalk(nested_list[[i]], ~ { # source: https://stackoverflow.com/a/73368297/28479453
      cat('###', .y, '\n\n')
      print(.x)
      cat('\n\n')
    })
    cat("\n:::\n\n")
  }
  cat("::::")
}  

```    

```{r}
#| results: asis
#| echo: false
#| message: false
#| warning: false
#| fig-width: 14
#| fig-height: 6    
nested_tabs(plot_list)    
```

Edit 1: Here is how to do it with plotly https://stackoverflow.com/a/79667255/28479453

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.