0

I'm creating several process maps based on the BupaR package in R, and I've like to export a PDF of all graphs, but it won't allow me to export the GraphViz object if it's grouped. These two example code snippets work indepentently, but the combined version at the end does now.

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")

What can I do to export all of them together?

Erik Rasmussen
  • 321
  • 2
  • 9
  • 22

1 Answers1

0

I have been using the solutions bellow:

One functional example:

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

To export as PDF, PNG, SVG or html (I am not sure if the html is working)

# 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)

Source

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

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

In Linux you may install:

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