在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
答案 0 :(得分:3)
我刚刚找到了一些解决方法,包括以下步骤:
这是一个例子:
```{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")))
}
```
答案 1 :(得分:1)
另一种解决方案是使用class Departmental_Goals(models.Model):
manager = models.ForeignKey(Profile, blank = False, null = True, on_delete=models.SET_NULL)
department = models.ForeignKey(Departments, null=True, on_delete=models.CASCADE)
summary = models.CharField(max_length=70, blank=False, null = False)
goal_category = models.ManyToManyField(goal_category)
def __str__(self):
return self.summary
这可防止在输出中显示!DOCTOOLS。
print(htmltools::tagList())
在使用数据表时遇到了相同的问题/解决方案,该解决方案由@Yihui在这里提供:https://github.com/rstudio/DT/issues/67
编辑从那以后,我意识到您至少需要在循环外部(即在print(htmltools :: taglist())外部)绘制一个visnetwork,否则就不会添加要渲染的脚本到html文件。
答案 2 :(得分:0)
您可以通过对字符串进行操作来删除!DOCTYPE。
cat (as.character(htmltools::includeHTML(paste0("vn",".html"))) %>%
{gsub("<!DOCTYPE html>","",.,fixed = TRUE)} %>%
{sub("\n","",.)}
)