I'm trying to create an HTML document using Knitr. I noticed you can get it to print regular plots created in a loop without any problem. However, plotly charts aren't as simple. For example, how can I get the following to work?
---
title: "Test"
output: html_document
---
```{r charts, echo=FALSE}
library("plotly")
charts<-list()
for(i in 1:length(unique(iris$Species))){
iris2<-iris[iris$Species==unique(iris$Species)[i],]
charts[[i]]<-plot_ly(iris2, x = ~Sepal.Length, y = ~Sepal.Width, type = 'bar')
}
print(charts)
```
The above does not work; the plots don't appear in the HTML document. However, the following does:
---
title: "Test"
output: html_document
---
```{r charts, echo=FALSE}
library("plotly")
charts<-list()
for(i in 1:length(unique(iris$Species))){
iris2<-iris[iris$Species==unique(iris$Species)[i],]
charts[[i]]<-plot_ly(iris2, x = ~Sepal.Length, y = ~Sepal.Width, type = 'bar')
}
charts[[1]]
charts[[2]]
charts[[3]]
```
I would just use to second option, but I don't know in advance how many charts will be created in the list which may be 50+, so it's not efficient to tell it to specifically print the first in the list, then the second, etc. like you see on the see above.
Again, these plotly/interactive type of HTML plots don't appear work exactly the same way as regular plots, at least when printing them.