create_graph: Create a graph object

Description Usage Arguments Value Examples

View source: R/create_graph.R

Description

Generates a graph object with the option to use node data frames (ndfs) and/or edge data frames (edfs) to populate the initial graph.

Usage

1
2
create_graph(nodes_df = NULL, edges_df = NULL, directed = TRUE,
  graph_name = NULL, attr_theme = "default", write_backups = FALSE)

Arguments

nodes_df

an optional data frame containing, at minimum, a column (called nodes) which contains node IDs for the graph. Additional columns (node attributes) can be included with values for the named node attribute.

edges_df

an optional data frame containing, at minimum, two columns (called from and to) where node IDs are provided. Additional columns (edge attributes) can be included with values for the named edge attribute.

directed

with TRUE (the default) or FALSE, either directed or undirected edge operations will be generated, respectively.

graph_name

an optional string for labeling the graph object.

attr_theme

the theme (i.e., collection of graph, node, and edge global graph attributes) to use for this graph. The default theme is called default. If this is set to NULL then no global graph attributes will be applied to the graph upon creation.

write_backups

an option to write incremental backups of changing graph states to disk. If TRUE, a subdirectory of the working directory will be used to store RDS files. The default value is FALSE so one has to opt in to use this functionality.

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# With `create_graph()` we can simply create an
# empty graph (and add in nodes and edges later,
# with other functions)
graph <- create_graph()

# A graph can be created with nodes and
# without edges; this is usually done in 2 steps:
# 1. create a node data frame (ndf) using the
#    `create_node_df()` function
ndf <- create_node_df(n = 4)

# 2. create the graph object with `create_graph()`
#    and pass in the ndf to `nodes_df`
graph <- create_graph(nodes_df = ndf)

# Get information on the graph's nodes
node_info(graph)
#>   id type label deg indeg outdeg loops
#> 1  1 <NA>  <NA>   0     0      0     0
#> 2  2 <NA>  <NA>   0     0      0     0
#> 3  3 <NA>  <NA>   0     0      0     0
#> 4  4 <NA>  <NA>   0     0      0     0

# You can create a similar graph with just nodes but
# also provide a range of attributes for the nodes
# (e.g., types, labels, arbitrary 'values')
ndf <-
  create_node_df(
    n = 4,
    label = TRUE,
    type = c("type_1", "type_1",
             "type_5", "type_2"),
    shape = c("circle", "circle",
              "rectangle", "rectangle"),
    values = c(3.5, 2.6, 9.4, 2.7))

graph <- create_graph(nodes_df = ndf)

# Get information on the graph's internal node
# data frame (ndf)
get_node_df(graph)
#>   id   type label     shape values
#> 1  1 type_1     1    circle    3.5
#> 2  2 type_1     2    circle    2.6
#> 3  3 type_5     3 rectangle    9.4
#> 4  4 type_2     4 rectangle    2.7

# A graph can also be created by specifying both
# the nodes and edges; create an edge data frame
# (edf) using the `create_edge_df()` function:
edf <-
  create_edge_df(
    from = c(1, 2, 3),
    to = c(4, 3, 1),
    rel = "leading_to",
    values = c(7.3, 2.6, 8.3))

# 2. create the graph object with `create_graph()`
#    and pass in the ndf and edf
graph <-
  create_graph(
    nodes_df = ndf,
    edges_df = edf)

# Get information on the graph's internal edge
# data frame (edf)
get_edge_df(graph)
#>   id from to        rel values
#> 1  1    1  4 leading_to    7.3
#> 2  2    2  3 leading_to    2.6
#> 3  3    3  1 leading_to    8.3

# Get information on the graph's internal node
# data frame (ndf)
get_node_df(graph)
#>   id   type label     shape values
#> 1  1 type_1     1    circle    3.5
#> 2  2 type_1     2    circle    2.6
#> 3  3 type_5     3 rectangle    9.4
#> 4  4 type_2     4 rectangle    2.7

Example output

Warning message:
In file(con, "rb") : cannot open file '/etc/timezone': Permission denied
Warning message:
In file(con, "rb") : cannot open file '/etc/timezone': Permission denied
  id type label deg indeg outdeg loops
1  1 <NA>  <NA>   0     0      0     0
2  2 <NA>  <NA>   0     0      0     0
3  3 <NA>  <NA>   0     0      0     0
4  4 <NA>  <NA>   0     0      0     0
Warning message:
In file(con, "rb") : cannot open file '/etc/timezone': Permission denied
  id   type label     shape values
1  1 type_1     1    circle    3.5
2  2 type_1     2    circle    2.6
3  3 type_5     3 rectangle    9.4
4  4 type_2     4 rectangle    2.7
Warning message:
In file(con, "rb") : cannot open file '/etc/timezone': Permission denied
  id from to        rel values
1  1    1  4 leading_to    7.3
2  2    2  3 leading_to    2.6
3  3    3  1 leading_to    8.3
  id   type label     shape values
1  1 type_1     1    circle    3.5
2  2 type_1     2    circle    2.6
3  3 type_5     3 rectangle    9.4
4  4 type_2     4 rectangle    2.7

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