Last active
September 20, 2023 06:22
-
-
Save rich-iannone/8e115b0bf328fb4b97c3 to your computer and use it in GitHub Desktop.
DiagrammeR (https://github.com/rich-iannone/DiagrammeR)— Trigger a script from inside a graph series and modify that graph series. Uses the 'create_series' and 'trigger_script' functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install the latest version of DiagrammeR from GitHub | |
devtools::install_github("rich-iannone/DiagrammeR") | |
# Ensure that the package is loaded | |
library("DiagrammeR") | |
# 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. | |
# And, the script is actually a character object | |
sample_node_script <- | |
' | |
# Provide default graph attributes | |
graph_attrs <- c("layout = twopi", | |
"overlap = FALSE", | |
"outputorder = edgesfirst") | |
# Provide default node attributes | |
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") | |
# Provide default edge attributes | |
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_edges(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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment