4

I am trying to use a GraphViz graph in DiagrammeR. How can I do this?

myGraph = 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
}
")

and then I want to use it in DiagrammeR, but it will not allow it.

render_graph(myGraph)

Gives:

Error: class(graph) == "dgr_graph" are not all TRUE

Is there a way I need to convert or input the GraphViz graph into the DiagrammeR environment?

ollama
  • 157
  • 2
  • 5
  • grViz returns an object of class htmlwidget and not a dgr_graph. You can visalize directly myGraph. use create_graph function from DiagrammeR to be able to use render_graph. – tokiloutok Aug 24 '16 at 22:28
  • Unfortunately, `gd = create_graph(myGraph)` gives the following error `Error: "nodes" %in% colnames(nodes_df) is not TRUE` -- I don't understand how to specify DiagrammeR graphs using GraphViz. If this isn't possible, then why does the DiagrammeR literature spend so much time talking about GraphViz? I mean, it says "Graphviz support is an integral part of the DiagrammeR package." -- If that's true, how do they connect? – ollama Aug 24 '16 at 23:49

2 Answers2

4

grViz takes a string describing the graph (vis.js style) : it is the interpreted by vis.js. Its return value is then an htmlwidget object.

render_graph takes a dgr_graph object, created using the create_graph function.

you can see in the DiagrammeR doc

library(DiagrammeR)

# Create a simple NDF
nodes <-
  create_nodes(
    nodes = 1:4,
    type = "number")

# Create a simple EDF
edges <-
  create_edges(
    from = c(1, 1, 3, 1),
    to = c(2, 3, 4, 4),
    rel = "related")

# Create the graph object,
# incorporating the NDF and
# the EDF, and, providing
# some global attributes
graph <-
  create_graph(
    nodes_df = nodes,
    edges_df = edges,
    graph_attrs = "layout = neato",
    node_attrs = "fontname = Helvetica",
    edge_attrs = "color = gray20")

# View the graph
render_graph(graph)

DiagrammeR can produce Graphviz code : From the doc mentioned below : "If you'd like to return the Graphviz DOT code (to, perhaps, share it or use it directly with the Graphviz command-line utility), just use output = "DOT" in the render_graph()"

So

  1. you can use create_graph to produce graphviz dot code
  2. you can use graphviz dot code directly with grViz in DiagrammeR
tokiloutok
  • 467
  • 5
  • 14
  • good answer, and just to make sure that it is clear, there is no function in `DiagrammeR` that converts `DOT` to a `dgr_graph`. `DiagrammeR` is designed to go the other way and focused on helping to build the network and render output. – timelyportfolio Aug 25 '16 at 10:38
  • Got it, so DOT / GraphViz is used primarily for visualization from DiagrammeR but not for graph creation – ollama Aug 25 '16 at 15:25
  • Somehow the documentation makes it hard to knowhow to render a graph. – NelsonGon Dec 03 '18 at 17:16
0

Here the problem is with render_graph(myGraph), using just myGraph works like charm.

library(DiagrammeR)

myGraph = 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
}
") 

myGraph

render_graph(myGraph) Does not work in R.

Just myGraph works fine.