Search code examples
rdiagrammer

Error in creating graph from DiagrammeR package


I am trying to create a node-link diagram in R using the DiagrammeR package from the below code:

library(DiagrammeR)

nodes <- create_nodes(nodes = 1:7, type = "number")

edges <- create_edges(from = c(1, 1, 2, 2, 3, 3),
                        to = c(2, 3, 4, 5, 6, 7),
                       rel = "leading to")

graph <- create_graph(nodes_df = nodes, 
                      edges_df = edges, 
                      graph_attrs = "layout = dot", 
                      node_attrs = "fontname = Helvetica", 
                      edge_attrs = "color = gray20") 

# View the graph
render_graph(graph)

But I get the following error:

Error in create_nodes(nodes = 1:7, type = "number") : 
  could not find function "create_nodes"

Are there any alternative ways to create a diagram like this in R:

node-link diagram


Solution

  • The functions create_nodes and create_edges were deprecated some time ago. Here's some code that uses the replacements.

    library(DiagrammeR)
    
    nodes <- create_node_df(n=7, type = "number")
    
    edges <- create_edge_df(from = c(1, 1, 2, 2, 3, 3),
                                            to = c(2, 3, 4, 5, 6, 7),
                                            rel = "leading to")
    
    graph <- create_graph(nodes_df = nodes, edges_df = edges)
    
    render_graph(graph)
    

    enter image description here