Skip to contents

Add nodes and their attributes to an existing graph object from data in a CSV file or a data frame.

Usage

add_nodes_from_table(
  graph,
  table,
  label_col = NULL,
  type_col = NULL,
  set_type = NULL,
  drop_cols = NULL
)

Arguments

graph

A graph object of class dgr_graph.

table

Either a path to a CSV file, or, a data frame object.

label_col

An option to apply a column of data in the table as label attribute values.

type_col

An option to apply a column of data in the table as type attribute values.

set_type

An optional string to apply a type attribute to all nodes created from data in the external table.

drop_cols

An optional column selection statement for dropping columns from the external table before inclusion as attributes in the graph's internal node data frame. Several columns can be dropped by name using the syntax col_1 & col_2 & .... Columns can also be dropped using a numeric column range with : (e.g., 5:8), or, by using the : between column names to specify the range (e.g., col_5_name:col_8_name).

Value

A graph object of class dgr_graph.

Examples

# To add nodes from the dataset called
# `currencies` (available as a dataset
# in the package), call the
# `add_nodes_from_table()` function
# after creating an empty graph; new
# node ID values will be created as
# monotonically-increasing values
graph_1 <-
  create_graph() %>%
  add_nodes_from_table(
    table = currencies)

# View part of the graph's internal
# node data frame (ndf)
graph_1 %>%
  get_node_df() %>%
  .[, 1:5] %>%
  head()
#>   id type label iso_4217_code curr_number
#> 1  1 <NA>  <NA>           AED         784
#> 2  2 <NA>  <NA>           AFN         971
#> 3  3 <NA>  <NA>           ALL           8
#> 4  4 <NA>  <NA>           AMD          51
#> 5  5 <NA>  <NA>           ANG         532
#> 6  6 <NA>  <NA>           AOA         973

# If you would like to assign
# any of the table's columns as
# `type` or `label` attributes,
# this can be done with the `type_col`
# and `label_col` arguments; to set
# a static `type` attribute for all
# of the table records, use `set_type`
graph_2 <-
  create_graph() %>%
  add_nodes_from_table(
    table = currencies,
    label_col = iso_4217_code,
    set_type = currency)

# View part of the graph's internal ndf
graph_2 %>%
  get_node_df() %>%
  .[, 1:5] %>%
  head()
#>   id     type label curr_number exponent
#> 1  1 currency   AED         784        2
#> 2  2 currency   AFN         971        2
#> 3  3 currency   ALL           8        2
#> 4  4 currency   AMD          51        2
#> 5  5 currency   ANG         532        2
#> 6  6 currency   AOA         973        2

# Suppose we would like to not
# include certain columns from the
# external table in the resulting
# graph; we can use the `drop_cols`
# argument to choose which columns
# to not include as attributes
graph_3 <-
  create_graph() %>%
  add_nodes_from_table(
    table = currencies,
    label_col = iso_4217_code,
    set_type = currency,
    drop_cols = exponent & currency_name)

# Show the node attribute names
# for the graph; note that the
# `exponent` and `currency_name`
# columns are not attributes in the
# graph's internal node data frame
graph_3 %>%
  get_node_df() %>%
  colnames()
#> [1] "id"          "type"        "label"       "curr_number"