2016-02-04 9 views
5

Ich versuche, mehrere Ploty Figuren in einem Rmarkdown Dokument mit Hilfe von Loop oder Lapply erstellen.Vielfache (R) plotly Zahlen in einem Rmarkdown (Knitr Chunk) Dokument generiert

Der R-Skript:

require(plotly) 
data(iris) 
b <- lapply(setdiff(names(iris), c("Sepal.Length","Species")), 
      function(x) { 
       plot_ly(iris, 
         x = iris[["Sepal.Length"]], 
         y = iris[[x]], 
         mode = "markers") 
      }) 
print(b) 

funktioniert gut, aber es funktioniert nicht, wenn sie in einem knitr Brocken enthält:

--- 
output: html_document 
--- 

```{r,results='asis'} 
require(plotly) 
data(iris) 
b <- lapply(setdiff(names(iris), c("Sepal.Length","Species")), 
      function(x) { 
       plot_ly(iris, 
         x = iris[["Sepal.Length"]], 
         y = iris[[x]], 
         mode = "markers") 
      }) 
print(b) 
``` 

Ich versuchte print(b) mit einer Kombination aus lapplyeval und parse sondern nur Ersatz der letzte Figur wurde angezeigt.

Ich vermute ein Scoping/Umgebung Problem, aber ich finde keine Lösung.

Vielen Dank für Ihre Hilfe.

Antwort

4

Anstelle von print(b), setzen Sie b in htmltools::tagList(), z.B.

```{r} 
library(plotly) 
b <- lapply(
    setdiff(names(iris), c("Sepal.Length","Species")), 
    function(x) { 
    as.widget(plot_ly(iris, 
      x = iris[["Sepal.Length"]], 
      y = iris[[x]], 
      mode = "markers")) 
    } 
) 
htmltools::tagList(b) 
``` 

Hinweis dies erfordert derzeit die Entwicklungsversion (> = 2.0.29) von plotly on Github.

+0

Danke für Ihre Hilfe. Aber es funktioniert nicht. Ich sehe Warnmeldungen: – frdbd

+0

## Warnung in charToRaw (Text): Argument sollte Zeichenfolge mit Länge 1 sein (ca. transl.) – frdbd

+0

Es sieht aus wie die Funktion tagList() Daten der Plots als Eingabe anstelle der generierten Zahlen (in js). – frdbd

3

fand ich eine "dirty" Lösung, die durch temporäre Datei mit und kniting es:

```{r,echo=FALSE} 
mytempfile<-tempfile() 
write("```{r graphlist,echo=FALSE}\n",file=mytempfile) 
write(paste("p[[",1:length(p),"]]"),file=mytempfile,append = TRUE) 
write("\n```",file=mytempfile,append = TRUE) 
``` 

`r knit_child(mytempfile, quiet=T)` 

Aber es ist nicht zufriedenstellend.

+0

Funktioniert wie ein Charme! –

Verwandte Themen