【问题标题】:edgebundle doesn't render plot when in loop in markdown在 Markdown 中循环时,edgebundle 不渲染绘图
【发布时间】:2017-12-21 13:58:08
【问题描述】:

我正在尝试创建一个自动报告,其中我使用 edgebundleR 创建了一系列和弦图。

我有一个功能可以做很多事情并且或多或少有这种形式:

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)

}
```

我发现一般来说,除非我使用 print,否则这在循环内不起作用:

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

但是打印在markdown中不起作用。

如何渲染情节?

谢谢。

【问题讨论】:

    标签: r knitr r-markdown data-visualization igraph


    【解决方案1】:

    edgebundle 调用返回一个htmlwidget 并且正如您所指出的,在不在循环中时也可以正常工作。针对您的情况的解决方案是使用 for 循环在临时文件中生成几个特定的​​ R 代码块,然后将该临时文件作为主 .Rmd 文件中的子文件进行评估。

    例如,在 .Rmd 文件中,这两个块将加载所需的包并定义一个函数 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)
    }
    ```
    

    在一个块中调用foo 两次将在输出.html 文档中按预期工作。

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

    要在for 循环中生成多个边缘预算,请尝试此操作。编写一个 for 循环以使用所需的 R 块填充 temp.Rmd 文件。您需要根据您的应用程序的需要对其进行修改。

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

    tmpfile 的内容如下所示:

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

    【讨论】:

    • 我很惊讶没有更简单(更优雅)的解决方案,但这个解决方案可以完成工作。
    猜你喜欢
    • 2018-03-10
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多