I'm trying to use the DiagrammeR
package to create a graph based on two simple data frames created by hand. The resulting graph should simply have two nodes ('a' and 'b') and one edge connecting them ('a' -> 'b').
Based on the documentation, it seems like this should be very easy to do.
From the package documentation as of version 1.0.1, the following are the minimal inputs to the create_graph()
function:
nodes_df
: an optional data frame containing, at minimum, a column (called id) which contains node IDs for the graph. Additional columns (node attributes) can be included with values for the named node attribute.
edges_df
: an optional data frame containing, at minimum, two columns (called from and to) where node IDs are provided. Additional columns (edge attributes) can be included with values for the named edge attribute.
Based on this documentation, it seems like at least one of the following two attempts to define a graph should work:
library(Diagrammer)
options('stringsAsFactors' = FALSE)
# Using integer node IDs
create_graph(nodes_df = data.frame(id = c(1L, 2L)),
edges_df = data.frame(from = 1L, to = 2L))
# Using character node IDs
create_graph(nodes_df = data.frame(id = c('a', 'b')),
edges_df = data.frame(from = 'a', to = 'b'))
However, with both attempts to create a graph, I receive the following error message:
Error in `[.data.frame`(nodes_df, , i) : undefined columns selected
Why do the calls not work as expected? What is the minimal way to create the intended graph by creating node and edge data frames using just the data.frame()
function, similar to the example given here?