add_edges_w_string: Add one or more edges using a text string

Description Usage Arguments Value Examples

View source: R/add_edges_w_string.R

Description

With a graph object of class dgr_graph, add one or more edges to the graph using a text string.

Usage

1
add_edges_w_string(graph, edges, rel = NULL, use_labels = FALSE)

Arguments

graph

a graph object of class dgr_graph.

edges

a single-length vector with a character string specifying the edges. For a directed graph, the string object should be formatted as a series of node ID values as [node_ID_1]->[node_ID_2] separated by a one or more space characters. For undirected graphs, -- should replace ->. Linebreaks in the vector won't cause an error.

rel

an optional vector specifying the relationship between the connected nodes.

use_labels

an option to use node label values in the edges string to define 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

 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
38
39
40
41
42
43
# 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 edges between nodes using a
# character string with node ID values
graph_node_id <-
  graph %>%
  add_edges_w_string(
    "1->2 1->3 2->4 2->3")

# Show the graph's internal edge data frame
get_edge_df(graph_node_id)
#>   id from to  rel
#> 1  1    1  2 <NA>
#> 2  2    1  3 <NA>
#> 3  3    2  4 <NA>
#> 4  4    2  3 <NA>

# Add edges between nodes using a
# character string with node label values
# and setting `use_labels = TRUE`; note
# that all nodes must have unique `label`
# values to use this option
graph_node_label <-
  graph %>%
  add_edges_w_string(
    "one->two one->three
     two->four two->three",
    use_labels = TRUE)

# Show the graph's internal edge data frame
# (it's the same as before)
get_edge_df(graph_node_label)
#>   id from to  rel
#> 1  1    1  2 <NA>
#> 2  2    1  3 <NA>
#> 3  3    2  4 <NA>
#> 4  4    2  3 <NA>

DiagrammeR documentation built on May 19, 2017, 11:29 p.m.

Search within the DiagrammeR package
Search all R packages, documentation and source code