2017-12-20 13 views
6

最近xaringanを使い始めました。本当にきれいです。素晴らしいパッケージのおかげでYihui。私が疑問に思っていた1つの質問は、forループにプロットされたプロットを含むスライドをプログラムで生成することが可能なのでしょうか?プログラムでRを使ってxaringanとpllyでスライドを生成

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

これは完璧に動作します:私はggplot_listがggplotsのリストである。このようなggplotsのスライドを、生成することができます知っています。

ggplotly(ggplot_list[[1]])を呼び出すことで、プロットプロットを個別に含めることもできますが、これも完全に機能します。

しかし、私は2つの組み合わせがうまくいかないように思えます。次のようにすると、空のスライドが生成されます。

```{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')` 
関連する問題