library(magrittr)
# Create a graph with a tree structure that's
# 3 levels deep (begins with node `1`, branching
# by 3 nodes at each level); the resulting graph
# contains 13 nodes, numbered `1` through `13`
graph <-
create_graph(graph_attrs = 'layout = twopi') %>%
add_node("A") %>%
select_nodes %>%
add_n_nodes_ws(3, "from", "B") %>%
clear_selection %>%
select_nodes("type", "B") %>%
add_n_nodes_ws(3, "from", "C") %>%
clear_selection
# Create a graph selection by selecting edges
# associated with nodes `1` and `2`
graph %<>%
select_edges_by_node_id(
nodes = 1:2)
# Get the selection of edges
graph %>% get_selection
#> $edges
#> $edges$from
#> [1] "1" "1" "1" "2" "2" "2"
#>
#> $edges$to
#> [1] "2" "3" "4" "5" "6" "7"
# Perform another selection of nodes, this time
# with a neighborhood spanning 2 nodes from node `1`
graph %<>%
clear_selection %>%
select_edges_by_node_id(
nodes = c(1, 2, 4))
# Get the selection of edges
graph %>% get_selection
#> $edges
#> $edges$from
#> [1] "1" "1" "1" "2" "2" "2" "4" "4" "4"
#>
#> $edges$to
#> [1] "2" "3" "4" "5" "6" "7" "11" "12" "13"
# Get a fraction of the edges selected over all
# the edges in the graph
graph %>%
{
l <- get_selection(.) %>%
unlist(.) %>%
length(.) %>%
divide_by_int(2)
e <- edge_count(.)
l/e
}
#> [1] 0.75
Run the code above in your browser using DataLab