2017-12-20 117 views
6

Gần đây tôi đã bắt đầu sử dụng xaringan và nó thực sự gọn gàng. Cảm ơn Yihui vì gói tuyệt vời. Một câu hỏi tôi đã tự hỏi, là nó có thể lập trình tạo ra các slide, mỗi có chứa một âm mưu cốt truyện, trong một vòng lặp for? Tôi biết tôi có thể tạo các trang trình bày của ggplots như thế này, trong đó ggplot_list là một danh sách các ggplots:Lập trình tạo trang trình bày trong R với xaringan và sơ đồ

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

Điều này hoạt động hoàn hảo.

Tôi cũng có thể bao gồm các ô lô lẻ riêng lẻ bằng cách gọi ggplotly(ggplot_list[[1]]), cũng hoạt động hoàn hảo.

Nhưng tôi dường như không thể kết hợp cả hai làm việc, ngây thơ làm như sau tạo ra các trang trình bày trống cho tôi.

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

Cập nhật: ở đây tôi bao gồm một ví dụ tối thiểu về những điều tôi đã thử cho đến nay.

--- 
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)) 
} 
``` 
+0

Bạn đã thử với 'in (ggplotly (p))'? –

+0

hmm không, điều đó cũng không hiệu quả với tôi. Tôi sẽ thêm nó vào một ví dụ tối thiểu. –

Trả lời

2

Tôi đã tìm ra giải pháp khi cố gắng thực hiện một điều tương tự trong knitr gần đây. Tôi đã thêm nó vào ví dụ trên. Xem phần cuối cùng - Nó tạo ra 3 slide rõ ràng trong một vòng lặp.

--- 
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')` 
Các vấn đề liên quan