1

I'm trying to use ggplotly in a nested tab construction in Quarto from one of my previous questions, Nested tabs in Quarto document. Somehow when we use ggplotly the plot does not show op in the document. If we just print the list it should work. Here is a reproducible example:

--- title: "test" format: html --- ```{r} #| echo: false #| message: false #| warning: false library(ggplot2) library(plotly) # example list example_list <- list( tab1 = list(test1.1 = ggplotly(ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+ geom_point()), test1.2 = ggplotly(ggplot(iris, aes(x = Petal.Length, y = Sepal.Width))+ geom_point())), tab2 = list(test2 = ggplotly(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 purrr::iwalk(example_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(example_list) ``` 

Output:

enter image description here

As you can see the plots are not shown. I feel like this has something to do with the results: asis, but I'm not sure. So I was wondering if anyone knows how we can use ggplotly in these tabs?

1 Answer 1

4

Following my answer on this post which refers to creating some {plotly} figures using a for loop in {rmarkdown}, one option to achieve your desired result would be to

  1. wrap the plotly objects in htmltools::tagList when printing

  2. ensure that the JS dependencies are included in the rendered document

 --- title: "test" format: html --- ```{r, include=FALSE} # Init Step to make sure that the dependencies are loaded htmltools::tagList(plotly::ggplotly(ggplot2::ggplot())) ``` ```{r} #| echo: false #| message: false #| warning: false library(ggplot2) library(plotly) # example list example_list <- list( tab1 = list(test1.1 = ggplotly(ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+ geom_point()), test1.2 = ggplotly(ggplot(iris, aes(x = Petal.Length, y = Sepal.Width))+ geom_point())), tab2 = list(test2 = ggplotly(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 purrr::iwalk(example_list[[i]], ~ { # source: https://stackoverflow.com/a/73368297/28479453 cat('###', .y, '\n\n') print(htmltools::tagList(.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(example_list) ``` 

enter image description here

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.