2017-12-20 36 views
6

최근에 xaringan을 사용하기 시작했으며 정말 멋지다. 위대한 패키지에 대한 감사 Yihui. 내가 궁금해했던 한 가지 질문은, for 루프에서 플롯 플롯을 포함하는 슬라이드를 프로그래밍 방식으로 생성하는 것이 가능한가? 이것은 완벽하게 작동프로그래밍 방식으로 xaringan 및 음모로 R 슬라이드 생성

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

: 나는이 같은 ggplots의 슬라이드를 생성 할 수 있습니다 알고, ggplot_list는 ggplots의 목록입니다.

또한 완벽하게 작동하는 ggplotly(ggplot_list[[1]])을 호출하여 플롯 플롯을 개별적으로 포함 할 수 있습니다.

하지만 두 작품의 조합을 얻지 못해서 순진하게 다음 작업을 수행하면 나에게 빈 슬라이드가 생성됩니다.

```{r, message=FALSE, warning=FALSE, results='asis'} 
for (p in ggplot_list) { 
    cat("\n\n---\n") 
    ggplotly(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) 

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

'print (ggplotly (p)) '를 사용해 보셨습니까? –

+0

흠, 그건 나에게도 도움이 안된다. 나는 그것을 최소한의 예제에 추가 할 것이다. –

답변

2

나는 최근에 knitr에서 비슷한 일을 시도 할 때 해결책을 찾아 냈습니다. 위의 예에 추가했습니다. 마지막 섹션보기 - 루프에 3 개의 플롯 슬라이드를 생성합니다.

--- 
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')`