2016-02-04 3 views
5

ループやlapplyを使用してRmarkdown文書に複数のプロット図を作成しようとしています。rmarkdown(knitr chunk)文書でmutiple(R)plotly figureが生成されました

Rスクリプト:

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) 

はうまく動作しますが、knitrチャンクに含まれる、それは失敗します。

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

私はlapplyevalparseの組み合わせでprint(b)を交換しようとしただけ最後の数字が表示されました。

スコープ/環境の問題が疑わしいですが、解決策が見つかりません。

ありがとうございました。

答えて

4

print(b)の代わりにbhtmltools::tagList()に入力します。

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

このバージョンの開発バージョン(> = 2.0.29)はplotly on Githubです。

+0

ご協力ありがとうございます。しかし、それは動作しません。私は警告メッセージが表示されます: – frdbd

+0

## charToRaw(テキスト)の警告:引数は長さ1の文字列でなければなりません。 – frdbd

+0

tagList()関数は生成された数字の代わりにプロットのデータを入力として取りますjsで)。 – frdbd

3

私は、一時ファイルを使用して、それをknitingによる「汚い」解決策が見つかりました:

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

をしかし、それは不十分なのです。

+0

魅力的な作品です! –

関連する問題