Display plotly graph produced in a for loop in RMakrdown html

Hi!

I have the following code:

library(plotly)

l <- htmltools::tagList()

for(i in 1:3) {
   l[[i]] <- plot_ly(x = rnorm(10))
}


for(i in 1:3) {

cat(paste0("\n\n#### ", i, "\n"))
  l[[i]]
}

Unfortunately, the plots are not displayed in the html output. Any suggestions? I work with R 4.1.1.
Thank you very much!

I can achieve the result with the following approach, based on using markdown::mark to turn any markdown I want into html, and then I can work entirely with HTML within tagLists.

```
---
title: "R Notebook"
output:
  html_document: default
---

```{r echo=FALSE}
suppressWarnings({
  suppressMessages({
library(plotly)
library(htmltools)
    
    })})

l <- htmltools::tagList()

for(i in 1:3) {
  l[[i]] <- tagList(HTML(markdown::mark(text=paste0("\n\n#### ", i, "\n"))),
         plot_ly(x = rnorm(10), type="histogram"))
}
```

```{r results='asis'}
l
```
1 Like

Thank you very much, this approach works for me! :slight_smile: Thank you!

Little follow-up question. I inserted your code after

"### Plot {.tabset .tabset-fade .tabset-pills}" in a Markdown document,
but it doesn't create tabs in the html output. Is this a result of the "HTML(markdown::mark(text=paste0("\n\n#### ", i, "\n")))" part?

Thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.