View source: R/set_node_attr_w_fcn.R
| set_node_attr_w_fcn | R Documentation | 
From a graph object of class dgr_graph or a node data frame, set node
attribute properties for all nodes in the graph using one of several
whole-graph functions.
set_node_attr_w_fcn(graph, node_attr_fcn, ..., column_name = NULL)
graph | 
 A graph object of class   | 
node_attr_fcn | 
 The name of the function to use for creating a column of
node attribute values. Valid functions are:   | 
... | 
 Arguments and values to pass to the named function in
  | 
column_name | 
 An option to supply a column name for the new node
attribute column. If   | 
A graph object of class dgr_graph.
Other node creation and removal: 
add_n_node_clones(),
add_n_nodes(),
add_n_nodes_ws(),
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_attrs(),
set_node_attrs_ws(),
set_node_position()
# Create a random graph using the
# `add_gnm_graph()` function
graph <-
  create_graph() %>%
  add_gnm_graph(
    n = 10,
    m = 22,
    set_seed = 23) %>%
  set_node_attrs(
    node_attr = value,
    values = rnorm(
      n = count_nodes(.),
      mean = 5,
      sd = 1) %>% round(1))
# Get the betweenness values for
# each of the graph's nodes as a
# node attribute
graph_1 <-
  graph %>%
  set_node_attr_w_fcn(
    node_attr_fcn = "get_betweenness")
# Inspect the graph's internal
# node data frame
graph_1 %>% get_node_df()
# If a specified function takes argument
# values, these can be supplied as well
graph_2 <-
  graph %>%
  set_node_attr_w_fcn(
    node_attr_fcn = "get_alpha_centrality",
    alpha = 2,
    exo = 2)
# Inspect the graph's internal
# node data frame
graph_2 %>% get_node_df()
# The new column name can be provided
graph_3 <-
  graph %>%
  set_node_attr_w_fcn(
    node_attr_fcn = "get_pagerank",
    column_name = "pagerank")
# Inspect the graph's internal
# node data frame
graph_3 %>% get_node_df()
# If `graph_3` is modified by
# adding a new node then the column
# `pagerank` will have stale data; we
# can run the function again and re-use
# the existing column name to provide
# updated values
graph_3 <-
  graph_3 %>%
  add_node(
    from = 1,
    to = 3) %>%
  set_node_attr_w_fcn(
    node_attr_fcn = "get_pagerank",
    column_name = "pagerank")
# Inspect the graph's internal
# node data frame
graph_3 %>% get_node_df()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.