add_n_nodes_ws: Add a multiple of new nodes with edges to or from one or more...

View source: R/add_n_nodes_ws.R

add_n_nodes_wsR Documentation

Add a multiple of new nodes with edges to or from one or more selected nodes

Description

Add n new nodes to or from one or more nodes available as a selection in a graph object of class dgr_graph. New graph edges will all move either from the nodes in the selection toward the newly created nodes (with the option direction = "from"), or to the selected nodes already in the graph (using direction = "to"). Optionally, set node type and edge rel values for all the new nodes and edges created, respectively.

This function makes use of an active selection of nodes (and the function ending with ⁠_ws⁠ hints at this).

Selections of nodes can be performed using the following node selection (⁠select_*()⁠) functions: select_nodes(), select_last_nodes_created(), select_nodes_by_degree(), select_nodes_by_id(), or select_nodes_in_neighborhood().

Selections of nodes can also be performed using the following traversal (⁠trav_*()⁠) functions: trav_out(), trav_in(), trav_both(), trav_out_node(), trav_in_node(), trav_out_until(), or trav_in_until().

Usage

add_n_nodes_ws(
  graph,
  n,
  direction = NULL,
  type = NULL,
  label = NULL,
  rel = NULL,
  node_aes = NULL,
  edge_aes = NULL,
  node_data = NULL,
  edge_data = NULL
)

Arguments

graph

A graph object of class dgr_graph.

n

The number of new nodes to attach as successor nodes to the nodes in the selection.

direction

Using from will create new edges from existing nodes to the new nodes. The to option will create new edges directed toward the existing nodes.

type

An optional character vector that provides group identifiers for the nodes to be added.

label

An optional character object that describes the nodes to be added.

rel

An optional string to apply a rel attribute to all newly created edges.

node_aes

An optional list of named vectors comprising node aesthetic attributes. The helper function node_aes() is strongly recommended for use here as it contains arguments for each of the accepted node aesthetic attributes (e.g., shape, style, color, fillcolor).

edge_aes

An optional list of named vectors comprising edge aesthetic attributes. The helper function edge_aes() is strongly recommended for use here as it contains arguments for each of the accepted edge aesthetic attributes (e.g., shape, style, penwidth, color).

node_data

An optional list of named vectors comprising node data attributes. The helper function node_data() is strongly recommended for use here as it helps bind data specifically to the created nodes.

edge_data

An optional list of named vectors comprising edge data attributes. The helper function edge_data() is strongly recommended for use here as it helps bind data specifically to the created edges.

Value

A graph object of class dgr_graph.

See Also

Other node creation and removal: add_n_node_clones(), add_n_nodes(), add_node(), add_node_clones_ws(), add_node_df(), add_nodes_from_df_cols(), add_nodes_from_table(), colorize_node_attrs(), copy_node_attrs(), create_node_df(), delete_node(), delete_nodes_ws(), drop_node_attrs(), join_node_attrs(), layout_nodes_w_string(), mutate_node_attrs(), mutate_node_attrs_ws(), node_data(), recode_node_attrs(), rename_node_attrs(), rescale_node_attrs(), set_node_attr_to_display(), set_node_attr_w_fcn(), set_node_attrs(), set_node_attrs_ws(), set_node_position()

Examples

# Create an empty graph, add a node to it, select
# that node, and then add 5 more nodes to the graph
# with edges from the original node to all of the
# new nodes
graph <-
  create_graph() %>%
  add_n_nodes(n = 1) %>%
  select_last_nodes_created() %>%
  add_n_nodes_ws(
    n = 5,
    direction = "from")

# Get the graph's nodes
graph %>% get_node_ids()

# Get the graph's edges
graph %>% get_edges()

# Create an empty graph, add a node to it, select
# that node, and then add 5 more nodes to the graph
# with edges toward the original node from all of
# the new nodes
graph <-
  create_graph() %>%
  add_n_nodes(n = 1) %>%
  select_last_nodes_created() %>%
  add_n_nodes_ws(
    n = 5,
    direction = "to")

# Get the graph's nodes
graph %>% get_node_ids()

# Get the graph's edges
graph %>% get_edges()


rich-iannone/DiagrammeR documentation built on Feb. 5, 2024, 8 a.m.