Trigger a script embedded in a graph series object

Description

Run an R script located inside or referenced from the graph series object in order to migrate the state of one or more contained graphs.

Usage

1
trigger_script(graph_series, script = 1)

Arguments

graph_series

a graph series object of type dgr_graph_1D.

script

the index of the script character string or path reference held in in the graph series.

Value

a graph series object of type dgr_graph_1D.

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
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
## Not run: 
# So, here's a script that essentially takes an empty graph series, and
# creates a new graph on each new day it is triggered. It will create
# random nodes each time it's triggered and add those nodes to the graph
# belonging to the current day. Throughout the script, '_SELF_' refers
# to the graph series in which the script is contained.
sample_node_script <-
'
graph_attrs <-
  c("layout = twopi",
    "overlap = FALSE",
    "outputorder = edgesfirst")

node_attrs <-
  c("shape = circle",
    "fixedsize = TRUE",
    "width = 1",
    "penwidth = 1",
    "color = DodgerBlue",
    "style = filled",
    "fillcolor = Aqua",
    "alpha_fillcolor = 0.5",
    "fontname = Helvetica",
    "fontcolor = Grey25")

edge_attrs <-
  c("arrowhead = dot",
    "minlen = 1.5",
    "color = Green",
    "penwidth = 2")

# If there is no graph available in the series, then, make one!
if (graph_count(graph_series = _SELF_) == 0){

_SELF_ <-
  add_to_series(graph = create_graph(graph_attrs = graph_attrs,
                node_attrs = node_attrs,
                edge_attrs = edge_attrs,
                graph_name = paste0("data_", Sys.Date()),
                graph_time = as.character(Sys.Date()),
                graph_tz = Sys.timezone()),
                graph_series = _SELF_)

}

# Determine the index of the last graph in the series
last_graph_in_series <- graph_count(graph_series = _SELF_)

# If it is a new day, create a new graph in the series to populate with data
if (Sys.Date() > as.Date(_SELF_$graphs[[last_graph_in_series]]$graph_time,
                         tz = _SELF_$graphs[[last_graph_in_series]]$graph_tz)){

_SELF_ <-
  add_to_series(graph = create_graph(graph_attrs = graph_attrs,
                node_attrs = node_attrs,
                edge_attrs = edge_attrs,
                graph_name = paste0("data_", Sys.Date()),
                graph_time = as.character(Sys.Date()),
                graph_tz = Sys.timezone()),
                graph_series = _SELF_)

last_graph_in_series <-
  graph_count(graph_series = _SELF_)
}

# Create a node to place into the graph
letters <- paste(sample(LETTERS, 5), collapse = "")

# Add node to the most recent graph and attach it to
# another randomly picked node available in the graph.
# Note that adding an edge only works in the case that
# there is at least one node available in the graph.
# For convenience, the relevant graph is extracted from
# the series, then placed back in the series.
if (!is.na(sample(get_nodes(_SELF_$graphs[[last_graph_in_series]]), 1))){

graph <- _SELF_$graphs[[last_graph_in_series]]

graph <- add_node(graph = graph,
                  node = letters)

graph <- add_edge(graph = graph,
                  from = letters,
                  to = sample(get_nodes(graph = graph), 1))

} else {

graph <- _SELF_$graphs[[last_graph_in_series]]

graph <- add_node(graph = graph,
                  node = letters)
}

# Remove old graph from series
_SELF_ <- remove_from_series(graph_series = _SELF_,
                             index = "last")

# Add new graph to correct position in series
# The "add_to_series" function always adds a graph to the
# end of the graph series.
_SELF_ <- add_to_series(graph = graph,
                        graph_series = _SELF_)

return(_SELF_)
'

# Create an empty graph series of the 'temporal' type and add
# that script as one of the graph series' 'series scripts'
series_temporal <- create_series(series_type = "temporal",
                                 series_scripts = sample_node_script)

# Call the function 60 times, this will generate 60 random nodes
# with 59 edges
for (i in seq(1, 60)){

  series_temporal <-
    trigger_script(graph_series = series_temporal,
                   script = 1)
  if (i == 60) break
}

# Display the results in the RStudio Viewer
render_graph_from_series(graph_series = series_temporal,
                         graph_no = graph_count(series_temporal))

# Get some basic information about the graphs in the graph series object
series_info(series_temporal)

# Write the script to a file
cat(sample_node_script, file = "~/Desktop/sample_node_script.R")

# Create a reference to the file instead of including text directly
# in the 'series_temporal' object
series_temporal <-
  create_series(series_type = "temporal",
                series_scripts = "~/Desktop/sample_node_script.R")

# Call the function 60 times, this will generate 60 random nodes
# with 59 edges
for (i in seq(1, 60)){

  series_temporal <-
    trigger_script(graph_series = series_temporal,
                   script = 1)
  if (i == 60) break
}

# Display the results in the RStudio Viewer
render_graph_from_series(graph_series = series_temporal,
                         graph_no = graph_count(series_temporal))

## End(Not run)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.