add_cycle: Add a cycle of nodes to the graph

Description Usage Arguments Value Examples

View source: R/add_cycle.R

Description

With a graph object of class dgr_graph, add a node cycle to the graph.

Usage

1
add_cycle(graph, n, type = NULL, label = TRUE, rel = NULL, ...)

Arguments

graph

a graph object of class dgr_graph.

n

the number of nodes comprising the cycle.

type

an optional string that describes the entity type for the nodes to be added.

label

either a vector object of length n that provides optional labels for the new nodes, or, a boolean value where setting to TRUE ascribes node IDs to the label and FALSE yields a blank label.

rel

an optional string for providing a relationship label to all new edges created in the node cycle.

...

optional node attributes supplied as vectors.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Create a new graph and
# add a cycle of nodes to it
graph <-
  create_graph() %>%
  add_cycle(n = 6)

# Get node information
# from this graph
node_info(graph)
#>   id type label deg indeg outdeg loops
#> 1  1 <NA>     1   2     1      1     0
#> 2  2 <NA>     2   2     1      1     0
#> 3  3 <NA>     3   2     1      1     0
#> 4  4 <NA>     4   2     1      1     0
#> 5  5 <NA>     5   2     1      1     0
#> 6  6 <NA>     6   2     1      1     0

# Attributes can be specified
# in extra arguments and these
# are applied in order; Usually
# these attributes are applied
# to nodes (e.g., `type` is a
# node attribute) but the `rel`
# attribute will apply to the
# edges
graph_w_attrs <-
  create_graph() %>%
  add_cycle(
    n = 6,
    label = c("one", "two",
              "three", "four",
              "five", "six"),
    type = c("a", "a",
             "b", "b",
             "c", "c"),
    value = c(1.2, 8.4,
              3.4, 5.2,
              6.1, 2.6),
    rel = "cycle")

# Get the graph's node data frame
get_node_df(graph_w_attrs)
#>   id type label value
#> 1  1    a   one   1.2
#> 2  2    a   two   8.4
#> 3  3    b three   3.4
#> 4  4    b  four   5.2
#> 5  5    c  five   6.1
#> 6  6    c   six   2.6

# Get the graph's edge data frame
get_edge_df(graph_w_attrs)
#>   id from to   rel
#> 1  1    1  2 cycle
#> 2  2    2  3 cycle
#> 3  3    3  4 cycle
#> 4  4    4  5 cycle
#> 5  5    5  6 cycle
#> 6  6    6  1 cycle

Example output

Warning messages:
1: In file(con, "rb") :
  cannot open file '/etc/timezone': Permission denied
2: In file(con, "rb") :
  cannot open file '/etc/timezone': Permission denied
  id type label deg indeg outdeg loops
1  1 <NA>     1   2     1      1     0
2  2 <NA>     2   2     1      1     0
3  3 <NA>     3   2     1      1     0
4  4 <NA>     4   2     1      1     0
5  5 <NA>     5   2     1      1     0
6  6 <NA>     6   2     1      1     0
Warning messages:
1: In file(con, "rb") :
  cannot open file '/etc/timezone': Permission denied
2: In file(con, "rb") :
  cannot open file '/etc/timezone': Permission denied
  id type label value
1  1    a   one   1.2
2  2    a   two   8.4
3  3    b three   3.4
4  4    b  four   5.2
5  5    c  five   6.1
6  6    c   six   2.6
  id from to   rel
1  1    1  2 cycle
2  2    2  3 cycle
3  3    3  4 cycle
4  4    4  5 cycle
5  5    5  6 cycle
6  6    6  1 cycle

DiagrammeR documentation built on Sept. 7, 2017, 1:06 a.m.