Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

r - Save DiagrammeR object to PNG on disc

I am using the lavaanPlot-Package to plot my Path Model created with lavaan. Works like a charm. Just to make it easier here is a reproducible example

Reproducible Example

library(pacman)
p_unload()
p_load(dplyr,lavaan,webshot,lavaanPlot)
model <- 'mpg ~ cyl + disp + hp
          qsec ~ disp + hp + wt'

fit <- sem(model, data = mtcars)
summary(fit)

plot <- lavaanPlot(model = fit, 
                   node_options = list(shape = "box", fontname = "Helvetica"), 
                   edge_options = list(color = "grey"), 
                   coefs = F)

plot

However I would like to save the result to a png file on my disc (rather than utilizing some form of screenshot). Digging in the docs I found out that lavaanPlot returns a Diagrammer Object. I tried to get a png file with three different approaches

Approaches

1. Webshot
Gives me the error webshot.js returned failure value: 1
2. export_graph
I found some suggestion how to fix it this link on SO, suggesting to use DiagrammeR export_graph. However this gives me the following error:

Fehler: `export_graph()` REASON:
* The graph object is not valid 

3. export_svg
Additionally I found this suggested solution on github, which I couldnt get to work either. It produces a SVG file for me which I cant open properly

# Try 1 using webshot
webshot("Plot_test_webshot.png")

# Try 2 export_graph
plot %>%
  export_graph(file_name = "pic.png",
               file_type = "png")

export_graph(plot,
             file_name = "pic.png",
             file_type = "png")

# Try 3 export_svg
plot %>%
  export_svg() %>%
  charToRaw %>% 
  rsvg_pdf("./path_to_svg_file.svg")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...