For loop over dygraph does not work in R

For anyone struggling with a loop, here's what worked for me.

p=list()
for (n in 1:3){
  p[[n]] <- plot_ly(x = 1:100, y = rnorm(100)+n, mode = 'lines', type="scatter")
}
htmltools::tagList(p)

I.e. it doesn't matter if the list p is created in a loop or lapply, etc. as long as you call htmltools::tagList outside the loop.

Thanks to Yihui for helping me get there and for immense work developing and helping with these tools.


Just wrap the list output from lapply() in htmltools::tagList(), e.g.

```{r}
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
res <- lapply(1:2, function(i) dygraph(lungDeaths[, i]))
htmltools::tagList(res)
```