3

I have R markdown document and I want to dynamically create tabs with ggplotly graphics inside them

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(ggplot2)
library(plotly)
```

```{r}
fig=ggplot(cars)+geom_point(aes(speed, dist))
```

# level 1
## level 2{.tabset .tabset-pills}

```{r echo=FALSE, results='asis'}

for (h in 1:3){
  cat("###", h,'{-}',  '\n\n')
 ggplotly(fig)
  cat( '\n\n')
}
```

I understand that it is different from normal ggplot graph and I looked at the solutions here: enter link description here but It did not work for me

stefan
  • 90,330
  • 6
  • 25
  • 51
yuliaUU
  • 1,581
  • 2
  • 12
  • 33

2 Answers2

4

Following this post this can be achieved like so:

Edit: Following this post I added two functions to pass the fig.width and fig.height to ggplotly.

Edit 2: Added the code to additionally use plotly::subplots.

---
title: test
date: "20 5 2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(ggplot2)
library(plotly)
```

```{r, echo=FALSE}
# Get the current figure size in pixels:
get_w <- function() {
  with(knitr::opts_current$get(c("fig.width", "dpi", "fig.retina")),
       fig.width*dpi/fig.retina)
}

get_h <- function() {
  with(knitr::opts_current$get(c("fig.height", "dpi", "fig.retina")),
       fig.height*dpi/fig.retina)
}
```

```{r}
fig <- ggplot(cars) + 
  geom_point(aes(speed, dist))
```

# level 1

## level 2 {.tabset .tabset-pills}

```{r, include=FALSE}
htmltools::tagList(ggplotly(fig))
```

```{r echo=FALSE, results='asis', fig.width=4, fig.height=4}
fig <- ggplotly(fig, width = get_w(), height = get_h())
for (h in 1:3) {
  cat("###", h, '{-}',  '\n\n')
  print(htmltools::tagList(plotly::subplot(fig, fig, nrows=2, heights = c(0.1, 0.9))))
  cat( '\n\n')
}
```
stefan
  • 90,330
  • 6
  • 25
  • 51
  • Stephan, thanks a lot! is there a way to set up the size of the graphs to make them larger? I tried to do it in:```{r echo=FALSE, results='asis'} adding fig.width and height, but does not work I do not want to do it globally as I have different figure sizes for each plot – yuliaUU May 20 '20 at 16:25
  • 1
    Hi @yuliaUU. I just edited my post. I borrowed the solution for adjusting the size using fig.width and fig.height from https://stackoverflow.com/questions/42532129/passing-fig-width-into-a-taglist – stefan May 20 '20 at 18:34
  • sorry for so many questions, I tried to provide a reproducible example but now when I am trying to adjust it to my own code, it is not working properly. I actually plotting : ff <- plotly::subplot(fig, fig, nrows=2, heights = c(0.1, 0.9)) where f ig<-ggplotly(fig). I added the print(htmltools::tagList(ggplotly(ff, width = get_w(), height = get_h()))) but it prints the width and length as a text under ggpltly – yuliaUU May 20 '20 at 20:37
  • 1
    Hi @yuliaUU. You have to do it the other way around. First ggplotly. Second plotly::subplot. And instead of the ggplotly put the plotly::subplot inside the htmlttolls::taglist. I edited my code accordingly. – stefan May 21 '20 at 07:43
  • I wonder if you can help me with a similar issue here:https://stackoverflow.com/questions/62016121/how-to-display-chorddiag-plot-when-using-dynamically-generated-tabs . I tried to do the same things as you provided here but it does not work – yuliaUU May 26 '20 at 16:30
  • I asked the same question and still could get the answer for seemingly a very simple question (how to `print` ggplotly() from inside `for` loop?). https://stackoverflow.com/questions/60685631/using-ggplotly-and-dt-from-a-for-loop-in-rmarkdown. The answer above does not seem to offer a simple solution to such simple question, or it does? – IVIM Jun 26 '20 at 16:21
-2

found a solution from this link too, this doesn't call for HTML, just markdown.

 ---
 date: "20 5 2020"
 output: html_document
 ---

 ```{r setup, include=FALSE}
 knitr::opts_chunk$set(echo = TRUE)
 ```

 ```{r}
 library(ggplot2)
 library(plotly)
 ```

 ```{r}
 fig <- ggplot(cars) + 
   geom_point(aes(speed, dist))
 ```

 ## Results {.tabset}

 ### 1

 We show a scatter plot in this section.

 ```{r}
 ggplotly(fig)
 ```

 ### 2

 We show the data in this tab.

 ```{r}
 ggplotly(fig)
 ```     
Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27
  • this answer is unrelated to my question. tabs are not a problem to add manually. I was asking about dynamically generating them . ggplotly won't work in that case – yuliaUU May 20 '20 at 16:02