DiagrammeR – How to use PDF Diagrams Instead of PNG

I can create this nice diagram using DiagrammeR:

DiagrammeR::grViz("digraph {
  graph [layout = dot, rankdir = TB]
  
  node [shape = rectangle]        
  rec1 [label = 'Step 1. Wake up']
  rec2 [label = 'Step 2. Write code']
  rec3 [label =  'Step 3. ???']
  rec4 [label = 'Step 4. PROFIT']
  
  # edge definitions with the node IDs
  rec1 -> rec2 -> rec3 -> rec4
  }",
  height = 200)

But I noticed that the diagram's resolution is really bad. This is because it exports to a PNG image insteaad of a PDF. Most diagrams I do export to PDF so their quality is really good, but DiagrammeR exports to PNG for some reason. How can I make it export to PDF?

Hi, have you found a solution for this?

No, unfortunately not. If I do find a solution, I will update this post.

I found a solution on SO that may fit your needs. Does this work?

library(DiagrammeR)
library(DiagrammeRsvg)
library(rsvg)
DiagrammeR::grViz("digraph {
  graph [layout = dot, rankdir = TB]
  
  node [shape = rectangle]        
  rec1 [label = 'Step 1. Wake up']
  rec2 [label = 'Step 2. Write code']
  rec3 [label =  'Step 3. ???']
  rec4 [label = 'Step 4. PROFIT']
  
  # edge definitions with the node IDs
  rec1 -> rec2 -> rec3 -> rec4
  }",
                  height = 200) %>% 
  DiagrammeRsvg::export_svg() %>% 
  charToRaw() %>% 
  rsvg::rsvg_pdf("svg_graph.pdf")

This worked, thanks. But there is one problem. It only exports the PDF file, it doesn't include it in my PDF output. So I have to do it manually right after the code block:

![How to profit](svg_graph.pdf)

Is there a way to

  1. Automate it exporting to PDF so I don't have to do it every time and choose the file name every time
  2. Show it in my output

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.