2017-12-20 165 views
6

मैंने हाल ही में xaringan का उपयोग शुरू किया और यह वास्तव में साफ है। महान पैकेज के लिए धन्यवाद Yihui। एक सवाल मैं सोच रहा था, क्या लूप के लिए प्रोग्रामेटिक रूप से स्लाइड उत्पन्न करना संभव है, प्रत्येक में प्लॉटली प्लॉट है?प्रोग्रार्मिक रूप से एक्स में xaringan के साथ स्लाइड उत्पन्न करें और साजिश

```{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')` 
संबंधित मुद्दे