cache_node_attrs_ws {DiagrammeR} | R Documentation |
From a graph object of class
dgr_graph
, get node attribute properties for
nodes available in a selection and cache those
values in the graph for later retrieval using
get_cache
.
Selections of nodes can be performed using
the following select_...
functions:
select_nodes()
,
select_last_node()
,
select_nodes_by_degree()
,
select_nodes_by_id()
, or
select_nodes_in_neighborhood()
.
Selections of nodes can also be performed using
the following traversal functions:
(trav_...
):
trav_out()
, trav_in()
,
trav_both()
, trav_in_node()
,
trav_out_node()
.
cache_node_attrs_ws(graph, node_attr, mode = NULL)
graph |
a graph object of class
|
node_attr |
the node attribute from which to obtain values. |
mode |
a option to recast the returned vector
of node attribute value as |
a graph object of class dgr_graph
.
# Set a seed set.seed(23) # Create a graph with 6 nodes and 5 edges graph <- create_graph() %>% add_path(6) %>% set_node_attrs( "value", rnorm(node_count(.), 5, 2)) # Select all nodes where the node attribute `value` # is less than 5 graph <- graph %>% select_nodes("value < 5.0") # Show the graph's node data frame graph %>% get_node_df #> id type label value #> 1 1 <NA> 1 5.090874 #> 2 2 <NA> 2 8.151559 #> 3 3 <NA> 3 5.436577 #> 4 4 <NA> 4 2.906929 #> 5 5 <NA> 5 4.422623 #> 6 6 <NA> 6 5.963101 # Cache available values from the node attribute # `value` from the nodes that are selected; ensure # that the cached vector is numeric graph <- graph %>% cache_node_attrs_ws("value", "numeric") # Get the cached vector and get its # difference from 5 graph %>% get_cache() %>% {x <- .; 5 - x} #> [1] 2.0930707 0.5773773