1

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.

Rb99
  • 61
  • 5
  • Do you really want to plot 50 charts after each other like that anyway? – warnbergg May 22 '20 at 05:29
  • 1
    In the actual report, I would try to print the name of the group for each plot as well so you could ctrl+f and locate the relevant plot when necessary. This would just make them all available should the need to assess a particular plot arise. – Rb99 May 22 '20 at 05:36

0 Answers0