DiagrammeR icon indicating copy to clipboard operation
DiagrammeR copied to clipboard

Save grviz to pdf, png, or similar

Open adeldaoud opened this issue 6 years ago • 4 comments

I would like to save grviz objects to to pdf, png, or similar formats. However, I have searched the web and DiagrammeR's issues (e.g. #66 , #70 ) but could not judge whether there is an implementation ready.

Tried a version of the DiagrammerRsvg package code (#133 ) that worked, but it does not seem to be generalized and natively packaged into DiagrammeR to support export of grViz. Are there plans for this?

thanks

adeldaoud avatar Jan 23 '19 22:01 adeldaoud

FWIW, the way I do this at the moment is as follows:

library(tidyverse)  # for %>% pipes
library(DiagrammeR)
library(DiagrammeRsvg)  # for conversion to svg
library(rsvg)  # for saving svg

grViz_object %>%
    export_svg() %>%
    charToRaw %>% 
    rsvg_pdf("./path_to_svg_file.svg")

jsavinc avatar Aug 05 '20 16:08 jsavinc

Just for those of us in the "Ceci n'est pas une pipe' crew:

# 1. Make a play graph
tmp = DiagrammeR::grViz('digraph{a->b; c->a; c->b; c->d;}')

# 2. Convert to SVG, then save as png
tmp = DiagrammeRsvg::export_svg(tmp)
tmp = charToRaw(tmp) # flatten
rsvg::rsvg_png(tmp, "g.png") # saved graph as png in current working directory

tbates avatar Jan 23 '21 14:01 tbates

Just for those of us in the "Ceci n'est pas une pipe' crew:

# 1. Make a play graph
tmp = DiagrammeR::grViz('digraph{a->b; c->a; c->b; c->d;}')

# 2. Convert to SVG, then save as png
tmp = DiagrammeRsvg::export_svg(tmp)
tmp = charToRaw(tmp) # flatten
rsvg::rsvg_png(tmp, "g.png") # saved graph as png in current working directory

does anybody know why might this be affecting the final graph output? precisely, I have text in my graph in "Times New Roman" font, which is correctly displayed in R own viewer, but the .png file after I save it displays another font

gasparsuliansky avatar Mar 22 '23 19:03 gasparsuliansky

@gasparsuliansky You may need to load the fonts into your graphic devices. Check out the extrafont package for more.

jrgant avatar Aug 23 '23 20:08 jrgant