iterate {tidygraph} | R Documentation |
Repeatedly modify a graph by a function
Description
The iterate family of functions allow you to call the same modification function on a graph until some condition is met. This can be used to create simple simulations in a tidygraph friendly API
Usage
iterate_n(.data, n, .f, ...)
iterate_while(.data, cnd, .f, ..., max_n = NULL)
Arguments
.data |
A |
n |
The number of times to iterate |
.f |
A function taking in a |
... |
Further arguments passed on to |
cnd |
A condition that must evaluate to |
max_n |
The maximum number of iterations in case |
Value
A tbl_graph
object
Examples
# Gradually remove edges from the least connected nodes while avoiding
# isolates
create_notable('zachary') |>
iterate_n(20, function(gr) {
gr |>
activate(nodes) |>
mutate(deg = centrality_degree(), rank = order(deg)) |>
activate(edges) |>
slice(
-which(edge_is_incident(.N()$rank == sum(.N()$deg == 1) + 1))[1]
)
})
# Remove a random edge until the graph is split in two
create_notable('zachary') |>
iterate_while(graph_component_count() == 1, function(gr) {
gr |>
activate(edges) |>
slice(-sample(graph_size(), 1))
})
[Package tidygraph version 1.3.1 Index]