trav_out_edge {DiagrammeR} | R Documentation |
From a graph object of class
dgr_graph
move to outgoing edges from a
selection of one or more selected nodes, thereby
creating a selection of edges. An optional filter
by edge attribute can limit the set of edges
traversed to.
trav_out_edge(graph, conditions = NULL, copy_attrs_from = NULL)
graph |
a graph object of class
|
conditions |
an option to use filtering conditions for the traversal. |
copy_attrs_from |
providing a node attribute name will copy those node attribute values to the traversed edges. If the edge attribute already exists, the values will be merged to the traversed edges; otherwise, a new edge attribute will be created. |
a graph object of class dgr_graph
.
# Set a seed set.seed(23) # Create a simple graph graph <- create_graph() %>% add_n_nodes( 2, type = "a", label = c("asd", "iekd")) %>% add_n_nodes( 3, type = "b", label = c("idj", "edl", "ohd")) %>% add_edges_w_string( "1->2 1->3 2->4 2->5 3->5", rel = c(NA, "A", "B", "C", "D")) # Create a data frame with node ID values # representing the graph edges (with `from` # and `to` columns), and, a set of numeric values df <- data.frame( from = c(1, 1, 2, 2, 3), to = c(2, 3, 4, 5, 5), values = round(rnorm(5, 5), 2)) # Join the data frame to the graph's internal # edge data frame (edf) graph <- graph %>% join_edge_attrs(df) get_node_df(graph) #> id type label #> 1 1 a asd #> 2 2 a iekd #> 3 3 b idj #> 4 4 b edl #> 5 5 b ohd get_edge_df(graph) #> id from to rel values #> 1 1 1 2 <NA> 6.00 #> 2 2 1 3 A 6.11 #> 3 3 2 4 B 4.72 #> 4 4 2 5 C 6.02 #> 5 5 3 5 D 5.05 # Perform a simple traversal from nodes to # outbound edges with no conditions on the # nodes traversed to graph %>% select_nodes_by_id(1) %>% trav_out_edge() %>% get_selection() #> [1] 1 2 # Traverse from node `1` to any outbound # edges, filtering to those edges that have # NA values for the `rel` edge attribute graph %>% select_nodes_by_id(1) %>% trav_out_edge( conditions = "is.na(rel)") %>% get_selection() #> [1] 1 # Traverse from node `3` to any outbound # edges, filtering to those edges that have # numeric values greater than `5.0` for # the `rel` edge attribute graph %>% select_nodes_by_id(3) %>% trav_out_edge( conditions = "values > 5.0") %>% get_selection() #> [1] 5 # Traverse from node `1` to any outbound # edges, filtering to those edges that # have values equal to `A` for the `rel` # edge attribute graph %>% select_nodes_by_id(1) %>% trav_out_edge( conditions = "rel == 'A'") %>% get_selection() #> [1] 2 # Traverse from node `2` to any outbound # edges, filtering to those edges that # have values in the set `B` and `C` for # the `rel` edge attribute graph %>% select_nodes_by_id(2) %>% trav_out_edge( conditions = "rel %in% c('B', 'C')") %>% get_selection() #> [1] 3 4 # Traverse from node `2` to any outbound # edges, and use multiple conditions for the # traversal (using a vector in `conditions` # creates a set of `AND` conditions) graph %>% select_nodes_by_id(2) %>% trav_out_edge( conditions = c( "rel %in% c('B', 'C')", "values >= 5.0")) %>% get_selection() #> [1] 4 # Traverse from node `2` to any outbound # edges, and use multiple conditions with # a single-length vector (here, using a # `|` to create a set of `OR` conditions) graph %>% select_nodes_by_id(2) %>% trav_out_edge( conditions = c( "rel %in% c('B', 'C') | values > 6.0")) %>% get_selection() #> [1] 3 4 # Traverse from node `2` to any outbound # edges, and use a regular expression as # a filtering condition graph %>% select_nodes_by_id(2) %>% trav_out_edge( conditions = "grepl('B|C', rel)") %>% get_selection() #> [1] 3 4