2017-12-21 38 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

edgebundlehtmlwidget을 반환하며, 루프에없는 경우에도 잘 작동합니다. 상황에 대한 해결책은 for 루프를 사용하여 임시 파일에 여러 개의 특정 R 코드 청크를 생성 한 다음 기본 파일을 기본 .Rmd 파일의 하위 파일로 평가하는 것입니다.

예를 들어 .Rmd 파일에서이 두 청크는 필요한 패키지를로드하고 무작위로 edgebundle을 생성하고 표시하는 함수 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) 
} 
``` 

출력 .html 문서에서 예상대로 덩어리가 작동 두 번 foo를 호출.

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

이 시도 for 루프에서 여러 edgebudles을 생성합니다. 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