1 min read

diagrammeR 使用技巧

保存成图片的方式

对于 github_document等方式,需要静态图片,这里 参考cran 提供解决方案

library(vtree)
FakeDataFig <- vtree(FakeData,"Severity Sex",horiz=FALSE)
grVizToPNG(FakeDataFig)

生成时间有点长。

knitr::include_graphics('FakeDataFig.png')

vtree uses the DiagrammeR package (which in turn is built on the open source graph visualization software, Graphviz).

grVizToPNG

The name of the graphics file (example1.png) is automatically derived from the name of the object (example1).

实现方式 js\(\to\)svg\(\to\)png

这是解决方式

function (g, width = 3000, folder = ".") 
{
    argname <- sapply(as.list(substitute({
        g
    })[-1]), deparse)
    message <- utils::capture.output(svg <- DiagrammeRsvg::export_svg(g))
    result <- rsvg::rsvg_png(charToRaw(svg), paste0(folder, 
        "/", argname, ".png"), width = width)
    invisible(NULL)
}
## function (g, width = 3000, folder = ".") 
## {
##     argname <- sapply(as.list(substitute({
##         g
##     })[-1]), deparse)
##     message <- utils::capture.output(svg <- DiagrammeRsvg::export_svg(g))
##     result <- rsvg::rsvg_png(charToRaw(svg), paste0(folder, 
##         "/", argname, ".png"), width = width)
##     invisible(NULL)
## }

双向箭头

a -> b [dir=both] 参考Github Issue 252