r语言 - BupaR DiagrammeR Group & Export Graph



我正在R中基于BupaR包创建几个流程图,我想导出所有图形的PDF,但如果GraphViz对象分组,它将不允许我导出。这两个示例代码片段独立工作,但最后的组合版本现在可以了。

log %>%
group_by(location) %>%
process_map()
log %>%
process_map(render = F) %>%
export_graph("my_process_map.pdf",title = "My title")
log %>%
group_by(location) %>%
process_map(render = F) %>%
export_graph("my_process_map.pdf",title = "My title")

我能做些什么把它们一起出口?

我一直在使用以下解决方案:

一个功能示例:

library(DiagrammeR)
flux_sp_model = grViz("
digraph boxes_and_circles {
# a 'graph' statement
graph [overlap = true, fontsize = 10]
# several 'node' statements
node [shape = box,
fontname = Helvetica]
A; B; C; D; E; F
node [shape = circle,
fixedsize = true,
width = 0.9] // sets as circles
1; 2; 3; 4; 5; 6; 7; 8
# several 'edge' statements
A->1 B->2 B->3 B->4 C->A
1->D E->A 2->4 1->5 1->F
E->6 4->6 5->7 6->7 3->8
}
")

flux_sp_model

导出为PDF、PNG、SVG或html(我不确定html是否为工作(

# devtools::install_github("rich-iannone/DiagrammeR")
# devtools::install_github("rich-iannone/DiagrammeRsvg")
# install.packages("DiagrammeRsvg")
# install.packages("rsvg")
library(DiagrammeR)
library(DiagrammeRsvg)
library(magrittr)
library(rsvg)
library(dplyr)
# grViz(diagram = flux_sp_model[[1]]$diagram) %>% export_svg %>% charToRaw %>% rsvg_png("graph.png", width = 5000, height = 5000) ### too heavy
# grViz(diagram = flux_sp_model[[1]]$diagram) %>% export_svg %>% charToRaw %>% rsvg_svg("graph.svg") ### for svg
grViz(diagram = flux_sp_model[[1]]$diagram) %>% export_svg %>% charToRaw %>% rsvg_pdf("flux_sp_model.pdf")
grViz(diagram = flux_sp_model[[1]]$diagram) %>% export_svg %>% charToRaw %>% rsvg_png("flux_sp_model.png")
class(flux_sp_model)
class(flux_sp_model[[1]]$diagram)
### or: (same result)
# pdf
grViz_pdf <- function(x, filename = "graph.pdf") {
utils::capture.output({
rsvg::rsvg_pdf(svg = charToRaw(DiagrammeRsvg::export_svg(DiagrammeR::grViz(x))), file = filename)})
invisible()}
grViz_pdf(flux_sp_model[[1]]$diagram)
# png
grViz_png <- function(x, filename = "graph.png") {
utils::capture.output({
rsvg::rsvg_png(svg = charToRaw(DiagrammeRsvg::export_svg(DiagrammeR::grViz(x))), file = filename)})
invisible()}
grViz_png(flux_sp_model[[1]]$diagram)
### save as html
# install.packages("plotly")
library(plotly)
htmlwidgets::saveWidget(flux_sp_model, 'flux_sp_model.html')
p <- plot_ly(mtcars, x = ~mpg, y = ~disp, mode = "markers")
htmlwidgets::saveWidget(p, "index.html")
class(p)

来源

https://github.com/rich-iannone/DiagrammeR/issues/70

https://stackoverflow.com/questions/57078753/how-to-save-grviz-object-from-diagrammer-r-package

在Linux中,您可以安装:

sudo apt install libv8-dev ### for package "DiagrammeRsvg"
sudo apt install librsvg2-dev ### for package "rsvg"

相关内容

  • 没有找到相关文章

最新更新