Learn R Programming

DiagrammeR (version 0.8.4)

select_edges: Select edges in a graph

Description

Select edges from a graph object of class dgr_graph.

Usage

select_edges(graph, edge_attr = NULL, search = NULL, set_op = "union", from = NULL, to = NULL)

Arguments

graph
a graph object of class dgr_graph that is created using create_graph.
edge_attr
an optional character vector of edge attribute values for filtering the edges returned.
search
an option to provide a logical expression with a comparison operator (>, <, ==, or !=) followed by a number for numerical filtering, or, a regular expression for filtering the nodes returned through string matching.
set_op
the set operation to perform upon consecutive selections of graph nodes. This can either be as a union (the default), as an intersection of selections with intersect, or, as a difference on the previous selection, if it exists.
from
an optional vector of node IDs from which the edge is outgoing for filtering the list of edges present in the graph.
to
an optional vector of node IDs to which the edge is incoming for filtering the list of edges present in the graph.

Value

a graph object of class dgr_graph.

Examples

Run this code
library(magrittr)

# Create a node data frame (ndf)
nodes <-
  create_nodes(
    nodes = c("a", "b", "c", "d"),
    type = "letter",
    label = TRUE,
    value = c(3.5, 2.6, 9.4, 2.7))

# Create an edge data frame (edf)
edges <-
  create_edges(
    from = c("a", "b", "c"),
    to = c("d", "c", "a"),
    rel = c("A", "Z", "A"),
    value = c(6.4, 2.9, 5.0))

# Create a graph with the ndf and edf
graph <-
  create_graph(nodes_df = nodes,
               edges_df = edges)

# Explicitly select the edge `a` -> `d`
graph <-
  graph %>%
  select_edges(
    from = "a",
    to = "d")

# Verify that an edge selection has been made
# using the `get_selection()` function
get_selection(graph)
#> $edges
#> $edges$from
#> [1] "b"
#>
#> $edges$to
#> [1] "c"

# Select edges based on the relationship label
# being `Z`
graph <-
  graph %>%
  clear_selection %>%
  select_edges(
    edge_attr = "rel",
    search = "Z")

# Verify that an edge selection has been made, and
# recall that the `b` -> `c` edge uniquely has the
# `Z` relationship label
get_selection(graph)
#> $edges
#> $edges$from
#> [1] "b"
#>
#> $edges$to
#> [1] "c"

# Select edges based on the edge value attribute
# being greater than 3.0 (first clearing the current
# selection of edges)
graph <-
  graph %>%
  clear_selection %>%
  select_edges(
    edge_attr = "value",
    search = ">3.0")

# Verify that the correct edge selection has been
# made; in this case, edges `a` -> `d` and
# `c` -> `a` have values for `value` greater than
# 3.0
get_selection(graph)
#> $edges
#> $edges$from
#> [1] "a" "c"
#>
#> $edges$to
#> [1] "d" "a"

Run the code above in your browser using DataLab