# NOT RUN {
# Create a graph with 3 nodes
graph <-
create_graph() %>%
add_path(3) %>%
set_node_attrs(
"width", c(3.4, 2.3, 7.2))
# Get the graph's internal ndf to show which
# node attributes are available
get_node_df(graph)
#> id type label width
#> 1 1 <NA> 1 3.4
#> 2 2 <NA> 2 2.3
#> 3 3 <NA> 3 7.2
# Mutate the `width` node attribute, dividing
# each value by 2
graph <-
graph %>%
mutate_node_attrs("width", "~ / 2")
# Get the graph's internal ndf to show that the
# node attribute `width` had its values changed
get_node_df(graph)
#> id type label width
#> 1 1 <NA> 1 1.70
#> 2 2 <NA> 2 1.15
#> 3 3 <NA> 3 3.60
# Create a new node attribute, called `length`,
# that is the log of values in `width` plus 2
# (and round all values to 2 decimal places)
graph <-
graph %>%
mutate_node_attrs(
"width", "round(log(~) + 2, 2)", "length")
# Get the graph's internal ndf to show that
# the node attribute values had been mutated
# and used as the new node attribute `length`
get_node_df(graph)
#> id type label width length
#> 1 1 <NA> 1 1.70 2.53
#> 2 2 <NA> 2 1.15 2.14
#> 3 3 <NA> 3 3.60 3.28
# Create a new node attribute called `area`,
# which is the product of the `width` and
# `length` attributes; note that we can provide
# NA to `node_attr_from` since we are naming
# at least one of the node attributes in the
# `expressions` vector (and providing a new
# node attribute name: `area`)
graph <-
graph %>%
mutate_node_attrs(
NA, "width * length", "area")
# Get the graph's internal ndf to show that
# the node attribute values had been multiplied
# together, creating a new node attribute `area`
get_node_df(graph)
#> id type label width length area
#> 1 1 <NA> 1 1.70 2.53 4.301
#> 2 2 <NA> 2 1.15 2.14 2.461
#> 3 3 <NA> 3 3.60 3.28 11.808
# }
Run the code above in your browser using DataLab