multiple (R) plotly figures generated in a rmarkdown (knitr chunk) document

Instead of print(b), put b in htmltools::tagList(), e.g.

```{r}
library(plotly)

b <- lapply(
  setdiff(names(iris),
          c("Sepal.Length","Species")),
  function(x) {
    plot_ly(iris, 
            x = iris[["Sepal.Length"]],
            y = iris[[x]], 
            mode = "markers")
  }
)

htmltools::tagList(b)
```

Note: Before Plotly v4 it was necessary to convert the Plotly objects to htmlwidgets using Plotly's as.widget() function. As of Plotly v4 they are htmlwiget objects by default.

For people who are interested in the technical background, you may see this blog post of mine. In short, only top-level expressions get printed.


I found a "dirty" solution by using temp file and kniting it :

```{r,echo=FALSE}
mytempfile<-tempfile()
write("```{r graphlist,echo=FALSE}\n",file=mytempfile)
write(paste("p[[",1:length(p),"]]"),file=mytempfile,append = TRUE)
write("\n```",file=mytempfile,append = TRUE)
```

`r knit_child(mytempfile, quiet=T)`

But it's unsatisfactory.