有没有一种简单的方法可以从R Markdown文档中的循环中打印DiagrammeR对象?我有一个文档,其中为多个模型分别生成了一个回归表、一个图和一个图,并一起打印。使用for循环很容易做到这一点,但是DiagrammeR对象不会在循环内打印(即使使用像print(graph)这样的显式调用。
这在其他地方已经有了文档(参见:https://github.com/rich-iannone/DiagrammeR/issues/39 ),但2015年的建议解决方案现在似乎不起作用(并且仅限于html输出)。
参见下面的Rmd示例代码。
---
title: "DiagrammeR loop test"
author: ""
date: "11/20/2020"
output: pdf_document
---
# Some header
Printing a single flowchart works fine in knitr using the DiagrammeR package.
```{r figure_1, out.width = "15%", echo = FALSE, fig.align = 'center', fig.cap = "Flowchart 1: This prints fine."}DiagrammeR::grViz(“
digraph test { node [shape = circle] A; B A -> B } ")Flowchart 2 which is inside a loop, however, does not print
```{r figure_2, out.width = "15%", echo = FALSE, fig.align = 'center', fig.cap = "Flowchart 2: These two diagrams do not print even with an explicit print command."}对于(1:2中的i){
graph <- DiagrammeR::grViz(“
digraph test { node [shape = circle] A; B A -> B } ")打印(图形)
}
发布于 2021-02-08 01:54:14
下面是如何修复它:
# Create an empty list to save the htmlwidgets
plot_list <- htmltools::tagList()
for (i in 1:2){
graph <- DiagrammeR::grViz("
digraph test {
node [shape = circle]
A; B
A -> B
}
")
# Store the htmlwidget inside this list
plot_list[[i]] <- graph
}
#Print this list
plot_listhttps://stackoverflow.com/questions/64948562
复制相似问题