使用 xaringan 和 plotly 以编程方式在 R 中生成幻灯片 [英] Programmatically generate slides in R with xaringan and plotly
问题描述
我最近开始使用 xaringan,它真的很棒.感谢 Yihui 的大礼包.我想知道的一个问题是,是否可以在 for 循环中以编程方式生成幻灯片,每张幻灯片都包含一个情节图?我知道我可以像这样生成 ggplots 的幻灯片,其中 ggplot_list 是 ggplots 的列表:
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("
---
")
print(p)
}
```
这很好用.
我还可以通过调用 ggplotly(ggplot_list[[1]])
来包含单独的情节图,这也很有效.
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("
---
")
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("
---
")
print(p)
}
```
---
# plotly in loop doesn't work
```{r, message=FALSE, warning=FALSE, results='asis'}
for (p in ggplot_list) {
cat("
---
")
ggplotly(p)
}
```
# print(ggplotly(p)) in loop doesn't work either
```{r, message=FALSE, warning=FALSE, results='asis'}
for (p in ggplot_list) {
cat("
---
")
print(ggplotly(p))
}
```
推荐答案
我最近在 knitr
中尝试做类似的事情时想出了一个解决方案.我将它添加到上面的示例中.请参阅最后一节 - 它会在一个循环中生成 3 张幻灯片.
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("
---
")
print(p)
}
```
---
# plotly in loop doesn't work
```{r, message=FALSE, warning=FALSE, results='asis'}
for (p in ggplot_list) {
cat("
---
")
ggplotly(p)
}
```
# print(ggplotly(p)) in loop doesn't work either
```{r, message=FALSE, warning=FALSE, results='asis'}
for (p in ggplot_list) {
cat("
---
")
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("
---
## Plot: ", p_name, "
```{r results='asis', echo=FALSE, warning=FALSE, message=FALSE}
ggplotly(ggplot_list[['", p_name, "']])
```")
out = c(out, knit_expanded)
}
```
<!--- knit those table chunk statements -->
`r paste(knit(text = out), collapse = '
')`
这篇关于使用 xaringan 和 plotly 以编程方式在 R 中生成幻灯片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!