【问题标题】:R Markdown: visNetWork not displayed in html output when using for loops (or functions)R Markdown:使用for循环(或函数)时,visNetWork未显示在html输出中
【发布时间】:2022-02-04 03:41:17
【问题描述】:

在 for 循环中尝试显示时,我的 visNetwork 根本不显示。在常规的 R 脚本中,我使用 print 函数,它工作得很好,但它在 R Markdown 文档中不起作用。

这是一个(希望是)可重现的例子:

---
title: "I want my beautiful networks back!"
---

# First example that works

```{r}
require(visNetwork, quietly = TRUE)
# minimal example
nodes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
vn <- visNetwork(nodes, edges, width = "100%")
vn # Or print(vn)
```

# When does it not work?

## In a for loop

```{r}
for (i in 1) vn
```

## In a for loop + using print

This will display the network in the Viewer.

```{r}
for (i in 1) print(vn)
```

## And also in functions

Same remark...

```{r}
foo <- function(x) print(x)
foo(vn)
```

我使用的是 Rstudio 版本 1.1.383 和 这是sessionInfo()的结果

R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6

other attached packages:
[1] visNetwork_2.0.2

【问题讨论】:

    标签: r visnetwork


    【解决方案1】:

    我刚刚找到了一个解决方法,包括以下步骤:

    • 将绘图另存为 HTML
    • 在 RMD 文件中包含 HTML 代码

    这里是一个例子:

    ```{r}
    for (i in 1:3){
      visSave(vn, file = paste0("test",i,".html"))
    }
    ```
    
    ```{r, results='asis'}
    for (i in 1:3){
      cat(htmltools::includeHTML(paste0("test",i,".html")))
    }
    ```
    

    【讨论】:

    • 非常感谢,我认为这个技巧在很多情况下都会对我有用!
    • 圣牛... :-)
    • 它是否只在我的 .Rmd 中打印 html 代码而不是网络,还是应该像那样工作?
    【解决方案2】:

    另一种解决方案是使用print(htmltools::tagList())

    这会阻止 !DOCTOOLS 在输出中显示。

    ```{r}
    for (i in 1) print(htmltools::tagList(vn))
    ```
    

    我在使用 Datatables 时遇到了同样的问题/解决方案,@Yihui 在这里提供了解决方案:https://github.com/rstudio/DT/issues/67

    编辑我已经意识到你至少需要在循环之外绘制一个 visnetwork,即在 print(htmltools::taglist()) 之外,否则不会添加要渲染的脚本到 html 文件。

    【讨论】:

      【解决方案3】:

      您可以通过对字符串进行操作来删除 !DOCTYPE。

      cat (as.character(htmltools::includeHTML(paste0("vn",".html"))) %>%
        {gsub("<!DOCTYPE html>","",.,fixed = TRUE)} %>%
        {sub("\n","",.)}
      )
      

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题,但也想从命令行而不是在 RStudio 中编写我的 Rmd,以便轻松生成许多不同参数的报告,例如:

        Rscript -e "rmarkdown::render('graphical-res-vis-reports.Rmd', \
              params = list(tissue = '${tissue}'), \
              output_file = sprintf('graphical-analysis-results_%s.html','${tissue}'))"
        

        htmltools::includeHTML(html_path) 技巧在从 RStudio 中编织时效果很好,但是当我尝试从命令行编织它时它会吐出一个 pandoc 错误,因为它有效地连接了多个独立的 HTML,这不会产生有效的 HTML 格式. (编辑: pandoc 错误也可能是因为命令行中的 R 使用的 pandoc 版本比 RStudio 旧得多,但使用 iframe 仍然更干净。)

        我终于找到了一个使用 iframe 的解决方案,效果很好。这是一个最小的可重现示例:

        ---
        title: "Finally, a way to print visNetworks in a loop!"
        ---
        
        ```{r make networks}
        require(visNetwork, quietly = TRUE)
        # minimal example
        nodes <- data.frame(id = 1:3)
        edges <- data.frame(from = c(1,2), to = c(1,3))
        
        html_list <- c()
        for (i in 1:3){
            vn <- visNetwork(nodes, edges)
            outfile <- sprintf("vn_%s.html", i)
            visSave(vn, file=outfile)
            html_list <- c(html_list, outfile)
        }
        ```
        
        ```{r plot networks, results="asis"}
        for (i in html_list){
            print(htmltools::tags$iframe(title = "My embedded document", 
                  src = f, height = "650", width = "900", frameBorder = "0"))
        }
        ```
        

        编辑:请注意,更改 iframe 的高度和宽度不会从 HTML 文件调整对象的大小。但是,您可以通过简单的文本替换来增加 visNetwork HTML 对象的大小。在此示例中,我删除了空白填充并将大小从 960x500 更改为 960x960:

        visSave(v1, file = out_html)
        
        # adjust browser window size 
        cmd = sprintf('sed -i \'s|"browser":{"width":960,"height":500,"padding":40,"fill":false}|"browser":{"width":960,"height":960,"padding":0,"fill":false}|\' %s', out_html)
        system(cmd)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-10-11
          • 2018-06-17
          • 2020-03-16
          • 2011-01-31
          • 2020-01-30
          • 1970-01-01
          • 2018-10-05
          相关资源
          最近更新 更多