Get all neighbors of one or more nodes

Description

With one or more nodes, get the set of all neighboring nodes.

Usage

1
get_nbrs(graph, nodes)

Arguments

graph

a graph object of class dgr_graph that is created using create_graph.

nodes

a vector of node ID values.

Value

a vector of node ID values.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
library(magrittr)

# Create a random, directed graph with 18 nodes
# and 22 edges
random_graph <-
  create_random_graph(
    n = 18,
    m = 22,
    directed = TRUE,
    fully_connected = TRUE,
    set_seed = 20) %>%
  set_global_graph_attrs(
    'graph', 'layout', 'sfdp') %>%
  set_global_graph_attrs(
    'graph', 'overlap', 'false')

# Find all neighbor nodes for node `5`
random_graph %>% get_nbrs(5)
#> [1] "1"  "2"  "12" "18"

# Find all neighbor nodes for nodes `5`, `7`,
# and `15`
random_graph %>% get_nbrs(c(5, 7, 15))
#> [1] "1"  "2"  "6"  "11" "12" "18"

# Get neighbors for node `11` and add a node
# attribute to color the nodes green, then, color
# all other nodes light gray
random_graph %<>%
  select_nodes_by_id(get_nbrs(., 11)) %>%
  set_node_attrs_ws('color', 'green') %>%
  invert_selection %>%
  set_node_attrs_ws('color', 'gray85') %>%
  clear_selection

# Render the graph to see the change
random_graph %>% render_graph

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.