2017-12-21 25 views
0

edgebundleRを使用して一連のコードグラフを作成する自動レポートを作成しようとしています。私はループ内でそれを使用しない場合マークダウンのループ中にedgebundleがプロットされない

plot_chords <- function(x,t,pos) { 
    ... 
    stuff I do with the data 
    ... 
    g <- graph.adjacency(mydata, mode="upper", weighted=TRUE, diag=FALSE) 
    return(edgebundle(g)) 
} 

この機能が正常に動作します。

は私が原料の束を行い、多かれ少なかれ、このフォームを持っている機能を持っています。私は一般的に、これは内部に動作しないことが判明

```{r echo = FALSE,message=FALSE, warning = FALSE,results = "asis"} 
for (c in unique(df$Group)) { 

    cat("\n\n## ",c," - Negative Correlations (min r=",t_neg," - only significative)\n\n") 
    plot_chords(subset(df, Group == c),0.5,0) 

} 
``` 

私はプリントを使用しない限りループ:それは、このようなループに入っている場合ではないん

for (c in unique(df$Group)) { 
    temp=df[df$Group == c,] 
    print(plot_chords(temp,0.5,0)) 
} 

しかし、印刷はしていませんマークダウンで働く

プロットをどのように描画できますか?

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

答えて

2

edgebundle呼び出しは、ループ内にないときには、あなたが指摘したように、htmlwidgetを返し、正常に動作します。状況を解決するには、forループを使用して、一時ファイルに複数の特定のRコードチャンクを生成し、その一時ファイルをプライマリ.Rmdファイルの子ファイルとして評価します。

たとえば、.Rmdファイルでは、これらの2つのチャンクが必要なパッケージをロードし、ランダムエッジバンドルを作成して表示する関数fooを定義します。出力.htmlドキュメントで期待どおりのチャンクで二回fooを呼び出す

```{r} 
set.seed(42) 
library(edgebundleR) 
library(igraph) 
``` 

## test the function 

```{r} 
foo <- function() { 
    adjm <- matrix(sample(0:1, 100, replace = TRUE, prob = c(0.6, 0.4)), nc = 10) 
    g <- graph.adjacency(adjm) 
    edgebundle(g) 
} 
``` 

は動作します。

```{r} 
foo() 
foo() 
``` 

forループでいくつかのエッジブライブを生成するには、これを試してみてください。 forループを作成して、temp.Rmdファイルに必要なRチャンクを設定します。アプリケーションの必要に応じてこれを変更する必要があります。 tmpfile

## test the function in a for loop 

```{r} 
tmpfile <- tempfile(fileext = ".Rmd") 
for(i in 1:3) { 
    cat("### This is edgebundle", i, "of 3.\n```{r}\nfoo()\n```\n", 
     file = tmpfile, append = TRUE) 
} 
``` 

内容は次のようになります。

### This is edgebundle 1 of 3. 
```{r} 
foo() 
``` 
### This is edgebundle 2 of 3. 
```{r} 
foo() 
``` 
### This is edgebundle 3 of 3. 
```{r} 
foo() 
``` 

は、このようなチャンクを使用して、プライマリ出力ファイルのウィジェットを表示するには:

```{r child = tmpfile} 
``` 

フル.Rmdファイルと結果:

example.Rmd:

# edgebundleR and knitr 
Answer to https://stackoverflow.com/questions/47926520/edgebundle-doesnt-render-plot-when-in-loop-in-markdown 

```{r} 
set.seed(42) 
library(edgebundleR) 
library(igraph) 
``` 

## test the function 

```{r} 
foo <- function() { 
    adjm <- matrix(sample(0:1, 100, replace = TRUE, prob = c(0.6, 0.4)), nc = 10) 
    g <- graph.adjacency(adjm) 
    edgebundle(g) 
} 
foo() 
foo() 
``` 

## test the function in a for loop 

```{r} 
tmpfile <- tempfile(fileext = ".Rmd") 
for(i in 1:3) { 
    cat("### This is edgebundle", i, "of 3.\n```{r}\nfoo()\n```\n", 
     file = tmpfile, append = TRUE) 
} 
``` 

```{r child = tmpfile} 
``` 

```{r} 
print(sessionInfo(), local = FALSE) 
``` 

enter image description here

+0

私は簡単に(とよりエレガントな)解決策がない驚いているが、これは仕事をしていません。 – Shark8

関連する問題