# NOT RUN {
# Create an empty graph
graph <- create_graph()
# Create a data frame from which several
# columns have values designated as graph nodes
df <-
data.frame(
col_1 = c("f", "p", "q"),
col_2 = c("q", "x", "f"),
col_3 = c(1, 5, 3),
col_4 = c("a", "v", "h"),
stringsAsFactors = FALSE)
# Add nodes from columns `col_1` and `col_2`
# from the data frame to the graph object
graph <-
graph %>%
add_nodes_from_df_cols(
df = df,
columns = c("col_1", "col_2"))
# Show the graph's node data frame; duplicate
# labels are prevented with `keep_duplicates =
# FALSE`)
graph %>%
get_node_df()
#> id type label
#> 1 1 <NA> f
#> 2 2 <NA> p
#> 3 3 <NA> q
#> 4 4 <NA> x
# Add new nodes from columns 3 and 4;
# We can specify the columns by their
# numbers as well
graph <-
graph %>%
add_nodes_from_df_cols(
df = df,
columns = 3:4)
# Show the graph's node data frame; note
# that a nodes didn't get made with columns
# that are not character class columns
graph %>%
get_node_df()
#> id type label
#> 1 1 <NA> f
#> 2 2 <NA> p
#> 3 3 <NA> q
#> 4 4 <NA> x
#> 5 5 <NA> a
#> 6 6 <NA> v
#> 7 7 <NA> h
# }
Run the code above in your browser using DataLab