DiagrammeR (version 0.9.0)

add_edge: Add an edge between nodes in a graph object

Description

With a graph object of class dgr_graph, add an edge to nodes within the graph.

Usage

add_edge(graph, from, to, rel = NULL, use_labels = FALSE)

Arguments

graph

a graph object of class dgr_graph.

from

the outgoing node from which the edge is connected.

to

the incoming nodes to which each edge is connected.

rel

an optional string specifying the relationship between the connected nodes.

use_labels

an option to use node label values in from and to for defining node connections. Note that this is only possible if all nodes have distinct label values set and none exist as an empty string.

Value

a graph object of class dgr_graph.

Examples

Run this code
# NOT RUN {
# Create a graph with 4 nodes
graph <-
  create_graph() %>%
  add_node(label = "one") %>%
  add_node(label = "two") %>%
  add_node(label = "three") %>%
  add_node(label = "four")

# Add an edge between those nodes and attach a
# relationship to the edge
graph <-
 add_edge(
   graph,
   from = 1,
   to = 2,
   rel = "A")

# Use the `edge_info()` function to verify that
# the edge has been created
edge_info(graph)
#>   id from to rel
#> 1  1    1  2   A

# Add another node and edge to the graph
graph <-
  graph %>%
  add_edge(3, 2, "A")

# Verify that the edge has been created by
# getting a count of graph edges
edge_count(graph)
#> [1] 2

# Add edges by specifying node `label` values
# and setting `use_labels = TRUE`; note
# that all nodes must have unique `label`
# values to use this option
graph <-
  graph %>%
  add_edge(
    "three", "four", "L",
    use_labels = TRUE) %>%
  add_edge(
    "four", "one", "L",
    use_labels = TRUE)

# Use the `get_edges()` function to verify
# that the edges were added
get_edges(graph)
#> [1] "1->2" "3->2" "3->4" "4->1"
# }

Run the code above in your browser using DataLab