title: Programmatically generate slides in R with xaringan and plotly
body: <p>I recently started using xaringan and it's really neat. Thanks Yihui for the great package. One question I was wondering, is it possible to programmatically generate slides, each containing a plotly plot, in a for loop? I know I can generate slides of ggplots like this, where ggplot_list is a list of ggplots:</p>
  
  <pre><code>```{r, message=FALSE, warning=FALSE, results='asis'}
  for (p in ggplot_list) {
    cat("\n\n---\n")
    print(p)
  }
  ```
  </code></pre>
  
  <p>This works perfectly.</p>
  
  <p>I can also include individual plotly plots by calling <code>ggplotly(ggplot_list[[1]])</code>, which also works perfectly.</p>
  
  <p>But I can't seem to get the combination of the two to work, naively doing the following generates empty slides for me.</p>
  
  <pre><code>```{r, message=FALSE, warning=FALSE, results='asis'}
  for (p in ggplot_list) {
    cat("\n\n---\n")
    ggplotly(p)
  }
  ```
  </code></pre>
  
  <p>Update: here I include a minimal example of things I've tried so far.</p>
  
  <pre><code>---
  title: "xaringan + plotly + loop?"
  subtitle: "Does it work?"
  author: "Fenfen Kan"
  date: "2017/13/32"
  output:
    xaringan::moon_reader:
      lib_dir: libs
      nature:
        highlightStyle: github
        highlightLines: true
        countIncrementalSlides: false
  ---
  
  ```{r setup, include=FALSE}
  options(htmltools.dir.version = FALSE)
  ```
  
  # Several boring ggplots
  
  ```{r, message=FALSE, warning=FALSE}
  library(ggplot2)
  library(plotly)
  
  p1 &lt;- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
    geom_point(aes(color=Species))
  
  p2 &lt;- ggplot(iris, aes(Petal.Length, Petal.Width)) +
    geom_point(aes(color=Species))
  
  p3 &lt;- ggplot(iris, aes(Sepal.Length, Petal.Length)) +
    geom_point(aes(color=Species))
  
  ggplot_list &lt;- list(p1, p2, p3)
  ```
  
  
  ---
  # Invididual plotly works
  
  ```{r}
  ggplotly(p1)
  ```
  
  ---
  # ggplot slides in loop also works
  
  ```{r, message=FALSE, warning=FALSE, results='asis'}
  for (p in ggplot_list) {
    cat("\n\n---\n")
    print(p)
  }
  ```
  
  ---
  # plotly in loop doesn't work
  
  ```{r, message=FALSE, warning=FALSE, results='asis'}
  for (p in ggplot_list) {
    cat("\n\n---\n")
    ggplotly(p)
  }
  ```
  
  # print(ggplotly(p)) in loop doesn't work either
  ```{r, message=FALSE, warning=FALSE, results='asis'}
  for (p in ggplot_list) {
    cat("\n\n---\n")
    print(ggplotly(p))
  }
  ```
  </code></pre>