Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diagrammer cannot create nodes in R

Tags:

r

diagrammer

I currently have R version 3.2.2 with DiagrammeR R package. I get these two errors when trying to run the following code:

library(DiagrammeR)
nodes <- create_nodes(nodes = seq(uniquenodes), 
                      type = "number", 
                      label = uniquenodes)

Error: could not find function "create_nodes"

edges <- create_edges(from = match(df$col1, uniquenodes), 
                      to = match(df$col2,uniquenodes), 
                      rel = "related")

Error: could not find function "create_edges"

like image 826
Deividas Kiznis Avatar asked Feb 07 '17 20:02

Deividas Kiznis


2 Answers

The code below should be compatible with DiagrammeR 0.9.0. The graph appears to have a different appearance than the one generated in DiagrammeR creates "wrong" diagram in R. I haven't played with render_graph in 0.9.0 very much, so am not yet sure how to get the earlier appearance.

df <- data.frame(col1 = c("Cat", "Dog", "Bird"),
                 col2 = c("Feline", "Canis", "Avis"),
                 stringsAsFactors = FALSE)
uniquenodes <- unique(c(df$col1, df$col2))

uniquenodes

library(DiagrammeR)

nodes <- create_node_df(n=length(uniquenodes), 
                        type="number", 
                        label=uniquenodes)
edges <- create_edge_df(from=match(df$col1, uniquenodes), 
                        to=match(df$col2, uniquenodes), 
                        rel="related")
g <- create_graph(nodes_df=nodes, 
                  edges_df=edges)
render_graph(g)
like image 68
Benjamin Avatar answered Nov 12 '22 07:11

Benjamin


I haven't played with render_graph in 0.9.0 very much, so am not yet sure how to get the earlier appearance.

The different appearance comes from the argument attr_theme in function create_graph, which is set to "default". Setting it to NULL provides brings back the appearance, however, this can be further adjusted by using function set_global_graph_attributes, which for me only worked in combination with magrittr:%>% as described here: https://stackoverflow.com/a/42676248/6816220

like image 26
larnsce Avatar answered Nov 12 '22 07:11

larnsce