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 tbl_graph object

n

The number of times to iterate

.f

A function taking in a tbl_graph as the first argument and returning a tbl_graph object

...

Further arguments passed on to .f

cnd

A condition that must evaluate to TRUE or FALSE determining if iteration should continue

max_n

The maximum number of iterations in case cnd never evaluates to FALSE

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]