Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically generate slides in R with xaringan and plotly

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:

```{r, message=FALSE, warning=FALSE, results='asis'}
for (p in ggplot_list) {
  cat("\n\n---\n")
  print(p)
}
```

This works perfectly.

I can also include individual plotly plots by calling ggplotly(ggplot_list[[1]]), which also works perfectly.

But I can't seem to get the combination of the two to work, naively doing the following generates empty slides for me.

```{r, message=FALSE, warning=FALSE, results='asis'}
for (p in ggplot_list) {
  cat("\n\n---\n")
  ggplotly(p)
}
```

Update: here I include a minimal example of things I've tried so far.

---
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 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point(aes(color=Species))

p2 <- ggplot(iris, aes(Petal.Length, Petal.Width)) +
  geom_point(aes(color=Species))

p3 <- ggplot(iris, aes(Sepal.Length, Petal.Length)) +
  geom_point(aes(color=Species))

ggplot_list <- 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))
}
```
like image 969
Yue Jiang Avatar asked Dec 20 '17 18:12

Yue Jiang


1 Answers

I figured out a solution when trying to do a similar thing in knitr recently. I added it to the above example. See the last section - It generates 3 plotly slides in a loop.

---
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)
library(knitr)

p1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point(aes(color=Species))

p2 <- ggplot(iris, aes(Petal.Length, Petal.Width)) +
  geom_point(aes(color=Species))

p3 <- ggplot(iris, aes(Sepal.Length, Petal.Length)) +
  geom_point(aes(color=Species))

ggplot_list <- list("p1"=p1, "p2"=p2, "p3"=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))
}
```

# generate chunks, then explicitly calling `knit` works! 

```{r create-markdown-chunks-dynamically, include=FALSE}

out = NULL
for (p_name in names(ggplot_list)) {
  knit_expanded <- paste0("\n\n---\n## Plot: ", p_name, "\n\n```{r results='asis', echo=FALSE, warning=FALSE, message=FALSE}\n\nggplotly(ggplot_list[['", p_name, "']])\n\n```")
  out = c(out, knit_expanded)
}

```

<!--- knit those table chunk statements --> 
`r paste(knit(text = out), collapse = '\n')`
like image 178
Yue Jiang Avatar answered Oct 23 '22 21:10

Yue Jiang