0

It looks like DiagrammeR has changed their create_nodes and create_edges functions than it looks on the doc. Also the $dot_code attribute is not there anymore. I can't find the replacement for this.

Here is the example code that they had on the doc, but it is not working. The following example is found in their official DiagrammeR web.

###
# Create a graph with both nodes and edges
# defined, and, add some default attributes
# for nodes and edges
###

library(DiagrammeR)

# Create a node data frame
nodes <-
  create_nodes(nodes = c("a", "b", "c", "d"),
               label = FALSE,
               type = "lower",
               style = "filled",
               color = "aqua",
               shape = c("circle", "circle",
                         "rectangle", "rectangle"),
               data = c(3.5, 2.6, 9.4, 2.7))

edges <-
  create_edges(from = c("a", "b", "c"),
               to = c("d", "c", "a"),
               rel = "leading_to")


graph <-
  create_graph(nodes_df = nodes,
               edges_df = edges,
               node_attrs = "fontname = Helvetica",
               edge_attrs = c("color = blue",
                              "arrowsize = 2"))

graph
#> $nodes_df
#>   nodes label  type  style color     shape data
#> 1     a       lower filled  aqua    circle  3.5
#> 2     b       lower filled  aqua    circle  2.6
#> 3     c       lower filled  aqua rectangle  9.4
#> 4     d       lower filled  aqua rectangle  2.7
#>
#> $edges_df
#>   from to        rel
#> 1    a  d leading_to
#> 2    b  c leading_to
#> 3    c  a leading_to
#>
#> $graph_attrs
#> [1] NULL
#>
#> $node_attrs
#> [1] "fontname = Helvetica"
#>
#> $edge_attrs
#> [1] "color = blue"  "arrowsize = 2"
#>
#> $directed
#> [1] TRUE
#>
#> $dot_code
#> [1] "digraph {\n\ngraph [rankdir = LR]\n\nnode [fontnam...
#>
#> attr(,"class")
#> [1] "dgr_graph"

# View the graph in the RStudio Viewer
render_graph(graph)

2 Answers2

0

look into the help of the DiagrammeR package, the website is indeed not up-to-date. Look at the function create_graph() in the help that shows how the syntax changed.

something like this should do the same:

    ###

library(DiagrammeR)

# Create a node data frame (ndf) where the labels
# are equivalent to the node ID values (this is not
# recommended); the `label` and `type` node
# attributes will always be a `character` class
# whereas `id` will always be an `integer`
nodes <-
  create_node_df(
    n         = 4,
    type      = "lower", 
    label     = c("a", "b", "c", "d"),
    style     = "filled",
    fillcolor = "blue",
    shape     = c("circle", "circle",
                  "rectangle", "rectangle"),
    data      = c(3.5, 2.6, 9.4, 2.7))

# Create an edf with additional edge
# attributes (where their classes will
# be inferred from the input vectors)
edges <-
  create_edge_df(
    from = c(1, 2, 3),
    to   = c(4, 3, 1),
    rel  = "leading_to")

# With `create_graph()` we can
# simply create an empty graph (and
# add in nodes and edges later
# with other functions)
graph <-
  create_graph(
    nodes_df  = nodes,
    edges_df  = edges)     %>%
  set_edge_attrs(
    edge_attr = color,
    values    = "green")   %>%
  set_edge_attrs(
    edge_attr = arrowsize,
    values    = 2)         %>%
  set_node_attrs(
    node_attr = fontname,
    values    = "Helvetica")

graph

render_graph(graph)
Floris Padt
  • 796
  • 5
  • 10
0

The Bad News

For anyone viewing this zombie thread, as of this posting, the information posted on Rich Iannone's documentation page for DiagrammeR is still out of date: http://rich-iannone.github.io/DiagrammeR/graph_creation.html.

The Good News

However, the vignettes for the Diagrammer package contain updated information. As shown in the package vignette named Creating Simple Graphs from NDFs/EDFs, you can view and export GraphViz dot code with a function named generate_dot().

Chris
  • 1,647
  • 1
  • 18
  • 25
  • Welcome to Stackoverflow and thanks for the close read of `DiagrammeR` vignette, and how it applies to this question. – Chris May 24 '21 at 16:57