dynamic.Rmd
You also add nodes and edges to static graphs. Below we graph the nodes then add (sg_add_edges
) at random intervals (0.5 to 2 seconds between each edge added). We also stop the layout algorithm after all edges have been aded (+ 100 milliseconds to make sure it stops when all edges are plotted).
# generate data using convenience functions
nodes <- sg_make_nodes(50)
edges <- sg_make_edges(nodes)
edges$delay <- runif(50, 500, 750) # between .5 and .75 seconds between each edge
last_edge <- sum(edges$delay) + 100
sigmajs() %>%
sg_force_start() %>%
sg_nodes(nodes, id, size, color) %>%
sg_add_edges(edges, delay, id, source, target, refresh = TRUE) %>% # read delay documentation
sg_force_stop(last_edge) %>%
sg_button(
"add_edges", # event
class = "btn btn-primary", # button class
tag = tags$a,
"Add edges" # label
)
You can also show helpful text as elements appear on the graph. Let’s add somewhat random dates to our example to simulate edges being added over the course of 25 days.
# adding random but ordered dates to edges
dates <- seq.Date(from = Sys.Date(), Sys.Date() + 24, "days")
dates <- sample(dates, nrow(edges), replace = TRUE)
edges$dates <- dates[order(dates)]
sigmajs() %>%
sg_force_start() %>%
sg_nodes(nodes, id, size, color) %>%
sg_add_edges(edges, delay, id, source, target, refresh = TRUE) %>% # read delay documentation
sg_progress(edges, delay, dates, class = "text-warning", position = "bottom") %>% # add progress text
sg_force_stop(last_edge) %>%
sg_button(
"add_edges", # event
class = "btn btn-primary",
tag = tags$a,
"Add edges" # label
)
You can use the sg_read_*
family of functions to add nodes and edges in bulk, this is much more efficient that the methads above when dealing with large graphs or adding numerous nodes and edges at once.
nodes <- sg_make_nodes(75)
nodes$batch <- c(
rep(1000, 25),
rep(3000, 25),
rep(5000, 25)
)
edges <- data.frame(
id = 1:120,
source = c(
sample(1:25, 40, replace = TRUE),
sample(1:50, 40, replace = TRUE),
sample(1:75, 40, replace = TRUE)
),
target = c(
sample(1:25, 40, replace = TRUE),
sample(1:50, 40, replace = TRUE),
sample(1:75, 40, replace = TRUE)
),
batch = c(
rep(1000, 40),
rep(3000, 40),
rep(5000, 40)
)
) %>%
dplyr::mutate_all(as.character)
sigmajs() %>%
sg_force_start() %>%
sg_read_nodes(nodes, id, color, size, delay = batch) %>%
sg_read_edges(edges, id, source, target, delay = batch) %>%
sg_force_stop(6000) %>%
sg_read_exec() %>%
sg_button(
c("read_exec", "force_stop"),
"Add nodes & edges",
class = "btn btn-primary",
tag = tags$a
)
You can also drop nodes or edges with sg_drop_edges
and sg_drop_nodes
, same principles, I’ll let you explore the examples.