/
graph_proxies.R
780 lines (625 loc) · 21.6 KB
/
graph_proxies.R
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#' Add node or edge
#'
#' Proxies to dynamically add a node or an edge to an already existing graph.
#'
#' @param proxy An object of class \code{sigmajsProxy} as returned by \code{\link{sigmajsProxy}}.
#' @param data A \code{data.frame} of _one_ node or edge.
#' @param ... any column.
#' @param refresh Whether to refresh the graph after node is dropped, required to take effect, if you are running force the algorithm is killed.
#'
#' @examples
#' \dontrun{
#' demo("add-node", package = "sigmajs")
#' demo("add-edge", package = "sigmajs")
#' demo("add-node-edge", package = "sigmajs")
#' }
#'
#' @note Have the parameters from your initial graph match that of the node you add, i.e.: if you pass \code{size} in your initial chart,
#' make sure you also have it in your proxy.
#'
#' @return The \code{proxy} object.
#'
#' @rdname add_p
#' @export
sg_add_node_p <- function(proxy, data, ..., refresh = TRUE) {
if (missing(proxy))
stop("must pass proxy", call. = FALSE)
.test_proxy(proxy)
# build data
nodes <- .build_data(data, ...) %>%
.check_ids() %>%
.check_x_y() %>%
.as_list()
message <- list(id = proxy$id, data = nodes, refresh = refresh) # create message
proxy$session$sendCustomMessage("sg_add_node_p", message)
return(proxy)
}
#' @rdname add_p
#' @export
sg_add_edge_p <- function(proxy, data, ..., refresh = TRUE) {
if (missing(proxy))
stop("must pass proxy", call. = FALSE)
.test_proxy(proxy)
# build data
edges <- .build_data(data, ...) %>%
.check_ids() %>%
.check_x_y() %>%
.as_list()
message <- list(id = proxy$id, data = edges, refresh = refresh) # create message
proxy$session$sendCustomMessage("sg_add_edge_p", message) # send message
return(proxy)
}
#' Add nodes or edges
#'
#' Proxies to dynamically add *multiple* nodes or edges to an already existing graph.
#'
#' @param proxy An object of class \code{sigmajsProxy} as returned by \code{\link{sigmajsProxy}}.
#' @param data A \code{data.frame} of nodes or edges.
#' @param ... any column.
#' @param refresh Whether to refresh the graph after node is dropped, required to take effect, if you are running force the algorithm is killed and restarted at every iteration..
#' @param rate Refresh rate, either \code{once}, the graph is refreshed after data.frame of nodes is added or at each \code{iteration} (row-wise). Only applies if \code{refresh} is set to \code{TRUE}.
#'
#' @examples
#' \dontrun{
#' demo("add-nodes", package = "sigmajs")
#' demo("add-edges", package = "sigmajs")
#' }
#'
#' @note Have the parameters from your initial graph match that of the node you add, i.e.: if you pass \code{size} in your initial chart,
#' make sure you also have it in your proxy.
#'
#' @return The \code{proxy} object.
#'
#' @rdname adds_p
#' @export
sg_add_nodes_p <- function(proxy, data, ..., refresh = TRUE, rate = "once") {
if (missing(proxy))
stop("must pass proxy", call. = FALSE)
.test_proxy(proxy)
if (missing(data))
stop("must pass data", call. = FALSE)
if (!rate %in% c("once", "iteration"))
stop("incorrect rate", call. = FALSE)
# build data
nodes <- .build_data(data, ...) %>%
.check_ids() %>%
.check_x_y() %>%
.as_list()
message <- list(id = proxy$id, data = nodes, refresh = refresh, rate = rate) # create message
proxy$session$sendCustomMessage("sg_add_nodes_p", message)
return(proxy)
}
#' @rdname adds_p
#' @export
sg_add_edges_p <- function(proxy, data, ..., refresh = TRUE, rate = "once") {
if (missing(proxy))
stop("must pass proxy", call. = FALSE)
.test_proxy(proxy)
if (missing(data))
stop("must pass data", call. = FALSE)
if (!rate %in% c("once", "iteration"))
stop("incorrect rate", call. = FALSE)
# build data
nodes <- .build_data(data, ...) %>%
.check_ids() %>%
.check_x_y() %>%
.as_list()
message <- list(id = proxy$id, data = nodes, refresh = refresh, rate = rate) # create message
proxy$session$sendCustomMessage("sg_add_edges_p", message)
return(proxy)
}
#' Remove node or edge
#'
#' Proxies to dynamically remove a node or an edge to an already existing graph.
#'
#' @param proxy An object of class \code{sigmajsProxy} as returned by \code{\link{sigmajsProxy}}.
#' @param id Id of edge or node to delete.
#' @param refresh Whether to refresh the graph after node is dropped, required to take effect, if you are running force the algorithm is killed and restarted.
#'
#' @return The \code{proxy} object.
#'
#' @rdname drop_p
#' @export
sg_drop_node_p <- function(proxy, id, refresh = TRUE) {
if (missing(proxy))
stop("must pass proxy", call. = FALSE)
.test_proxy(proxy)
if (missing(id))
stop("must pass id")
message <- list(id = proxy$id, data = id, refresh = refresh)
proxy$session$sendCustomMessage("sg_drop_node_p", message)
return(proxy)
}
#' @rdname drop_p
#' @export
sg_drop_edge_p <- function(proxy, id, refresh = TRUE) {
if (missing(proxy))
stop("must pass proxy", call. = FALSE)
.test_proxy(proxy)
if (missing(id))
stop("must pass id")
message <- list(id = proxy$id, data = id, refresh = refresh)
proxy$session$sendCustomMessage("sg_drop_edge_p", message)
return(proxy)
}
#' Clear or kill the graph
#'
#' Clear all nodes and edges from the graph or kills the graph.
#'
#' @param proxy An object of class \code{sigmajsProxy} as returned by \code{\link{sigmajsProxy}}.
#' @param refresh Whether to refresh the graph after node is dropped, required to take effect, if you are running force the algorithm is killed and restarted.
#'
#' @return The \code{proxy} object.
#'
#' @rdname clear-kill
#' @export
sg_clear_p <- function(proxy, refresh = TRUE) {
if (missing(proxy))
stop("must pass proxy", call. = FALSE)
.test_proxy(proxy)
message <- list(id = proxy$id, refresh = refresh)
proxy$session$sendCustomMessage("sg_clear_p", message)
return(proxy)
}
#' @rdname clear-kill
#' @export
sg_kill_p <- function(proxy, refresh = TRUE) {
if (missing(proxy))
stop("must pass proxy", call. = FALSE)
.test_proxy(proxy)
message <- list(id = proxy$id, refresh = refresh)
proxy$session$sendCustomMessage("sg_kill_p", message)
return(proxy)
}
#' Add nodes or edges with a delay
#'
#' Proxies to dynamically add multiple nodes or edges to an already existing graph with a *delay* between each addition.
#'
#' @param proxy An object of class \code{sigmajsProxy} as returned by \code{\link{sigmajsProxy}}.
#' @param data A \code{data.frame} of _one_ node or edge.
#' @param ... any column.
#' @param refresh Whether to refresh the graph after node is dropped, required to take effect, if you are running force the algorithm is killed and restarted at every iteration.
#' @param delay Column name containing delay in milliseconds.
#' @param cumsum Whether to compute the cumulative sum of the delay.
#'
#' @details The delay helps for build dynamic visualisations where nodes and edges do not appear all at the same time.
#' How the delay works depends on the \code{cumsum} parameter. if \code{TRUE} the function computes the cumulative sum
#' of the delay to effectively add each row one after the other: delay is thus applied at each row (number of seconds to wait
#' before the row is added *since the previous row*). If \code{FALSE} this is the number of milliseconds to wait before the node or
#' edge is added to the visualisation; \code{delay} is used as passed to the function.
#'
#' @return The \code{proxy} object.
#'
#' @note Have the parameters from your initial graph match that of the node you add, i.e.: if you pass \code{size} in your initial chart,
#' make sure you also have it in your proxy.
#'
#' @rdname adds_delay_p
#' @export
sg_add_nodes_delay_p <- function(proxy, data, delay, ..., refresh = TRUE, cumsum = TRUE) {
if (missing(proxy))
stop("must pass proxy", call. = FALSE)
.test_proxy(proxy)
if (missing(data) || missing(delay))
stop("must pass data and delay", call. = FALSE)
delay_col <- eval(substitute(delay), data) # subset delay
if (isTRUE(cumsum))
delay_col <- cumsum(delay_col) # cumul for setTimeout
delay_table <- dplyr::tibble(sigmajsdelay = delay_col) # build delay tibble
# build data
nodes <- .build_data(data, ...) %>%
dplyr::bind_cols(delay_table) %>% # bind delay
.check_ids() %>%
.check_x_y() %>%
.as_list()
message <- list(id = proxy$id, data = nodes, refresh = refresh) # create message
proxy$session$sendCustomMessage("sg_add_nodes_delay_p", message)
return(proxy)
}
#' @rdname adds_delay_p
#' @export
sg_add_edges_delay_p <- function(proxy, data, delay, ..., refresh = TRUE, cumsum = TRUE) {
if (missing(proxy))
stop("must pass proxy", call. = FALSE)
.test_proxy(proxy)
if (missing(data) || missing(delay))
stop("must pass data and delay", call. = FALSE)
delay_col <- eval(substitute(delay), data) # subset delay
if (isTRUE(cumsum))
delay_col <- cumsum(delay_col) # cumul for setTimeout
delay_table <- dplyr::tibble(sigmajsdelay = delay_col) # build delay tibble
# build data
nodes <- .build_data(data, ...) %>%
dplyr::bind_cols(delay_table) %>% # bind delay
.check_ids() %>%
.check_x_y() %>%
.as_list()
message <- list(id = proxy$id, data = nodes, refresh = refresh) # create message
proxy$session$sendCustomMessage("sg_add_edges_delay_p", message)
return(proxy)
}
#' Drop nodes or edges
#'
#' Proxies to dynamically drop *multiple* nodes or edges from an already existing graph.
#'
#' @param proxy An object of class \code{sigmajsProxy} as returned by \code{\link{sigmajsProxy}}.
#' @param data A \code{data.frame} of nodes or edges.
#' @param ids Column containing ids to drop from the graph.
#' @param refresh Whether to refresh the graph after node is dropped, required to take effect.
#' @param rate Refresh rate, either \code{once}, the graph is refreshed after data.frame of nodes is added or at each \code{iteration} (row-wise). Only applies if \code{refresh} is set to \code{TRUE}.
#'
#' @return The \code{proxy} object.
#'
#' @note Have the parameters from your initial graph match that of the node you add, i.e.: if you pass \code{size} in your initial chart,
#' make sure you also have it in your proxy.
#'
#' @rdname drops_p
#' @export
sg_drop_nodes_p <- function(proxy, data, ids, refresh = TRUE, rate = "once") {
if (missing(proxy))
stop("must pass proxy", call. = FALSE)
.test_proxy(proxy)
if (missing(data))
stop("must pass data", call. = FALSE)
if (!rate %in% c("once", "iteration"))
stop("incorrect rate", call. = FALSE)
ids <- eval(substitute(ids), data) # subset ids
message <- list(id = proxy$id, data = ids, refresh = refresh, rate = rate) # create message
proxy$session$sendCustomMessage("sg_drop_nodes_p", message)
return(proxy)
}
#' @rdname drops_p
#' @export
sg_drop_edges_p <- function(proxy, data, ids, refresh = TRUE, rate = "once") {
if (missing(proxy))
stop("must pass proxy", call. = FALSE)
.test_proxy(proxy)
if (missing(data))
stop("must pass data", call. = FALSE)
if (!rate %in% c("once", "iteration"))
stop("incorrect rate", call. = FALSE)
ids <- eval(substitute(ids), data) # subset ids
message <- list(id = proxy$id, data = ids, refresh = refresh, rate = rate) # create message
proxy$session$sendCustomMessage("sg_drop_edges_p", message)
return(proxy)
}
#' Drop nodes or edges with a delay
#'
#' Proxies to dynamically drop multiple nodes or edges to an already existing graph with a *delay* between each removal.
#'
#' @param proxy An object of class \code{sigmajsProxy} as returned by \code{\link{sigmajsProxy}}.
#' @param data A \code{data.frame} of _one_ node or edge.
#' @param ids Ids of elements to drop.
#' @param refresh Whether to refresh the graph after node is dropped, required to take effect, if you are running force the algorithm is killed and restarted at every iteration.
#' @param delay Column name containing delay in milliseconds.
#' @param cumsum Whether to compute the cumulative sum of the delay.
#'
#' @details The delay helps for build dynamic visualisations where nodes and edges do not disappear all at the same time.
#' How the delay works depends on the \code{cumsum} parameter. if \code{TRUE} the function computes the cumulative sum
#' of the delay to effectively drop each row one after the other: delay is thus applied at each row (number of seconds to wait
#' before the row is dropped *since the previous row*). If \code{FALSE} this is the number of milliseconds to wait before the node or
#' edge is added to the visualisation; \code{delay} is used as passed to the function.
#'
#' @return The \code{proxy} object.
#'
#' @note Have the parameters from your initial graph match that of the node you add, i.e.: if you pass \code{size} in your initial chart,
#' make sure you also have it in your proxy.
#'
#' @rdname drop_delay_p
#' @export
sg_drop_nodes_delay_p <- function(proxy, data, ids, delay, refresh = TRUE, cumsum = TRUE) {
if (missing(proxy))
stop("must pass proxy", call. = FALSE)
.test_proxy(proxy)
if (missing(data))
stop("must pass data", call. = FALSE)
delay_col <- eval(substitute(delay), data) # subset delay
if (isTRUE(cumsum))
delay_col <- cumsum(delay_col) # cumul for setTimeout
ids <- eval(substitute(ids), data) # subset ids
to_drop <- dplyr::tibble(
id = ids,
sigmajsdelay = delay_col
)
to_drop <- dplyr::tibble(
id = ids,
sigmajsdelay = delay_col
) %>% # bind delay
.as_list()
message <- list(id = proxy$id, data = to_drop, refresh = refresh) # create message
proxy$session$sendCustomMessage("sg_drop_nodes_delay_p", message)
return(proxy)
}
#' @rdname drop_delay_p
#' @export
sg_drop_edges_delay_p <- function(proxy, data, ids, delay, refresh = TRUE, cumsum = TRUE) {
if (missing(proxy))
stop("must pass proxy", call. = FALSE)
.test_proxy(proxy)
if (missing(data))
stop("must pass data", call. = FALSE)
delay_col <- eval(substitute(delay), data) # subset delay
if (isTRUE(cumsum))
delay_col <- cumsum(delay_col) # cumul for setTimeout
ids <- eval(substitute(ids), data) # subset ids
to_drop <- dplyr::tibble(
id = ids,
sigmajsdelay = delay_col
)
to_drop <- dplyr::tibble(
id = ids,
sigmajsdelay = delay_col
) %>% # bind delay
.as_list()
message <- list(id = proxy$id, data = to_drop, refresh = refresh) # create message
proxy$session$sendCustomMessage("sg_drop_edges_delay_p", message)
return(proxy)
}
#' Read
#'
#' Read nodes and edges to add to the graph. Other proxy methods to add data to a graph have to add nodes and edges one by one,
#' thereby draining the browser, this method will add multiple nodes and edges more efficiently.
#'
#' @param proxy An object of class \code{sigmajsProxy} as returned by \code{\link{sigmajsProxy}}.
#' @param data A \code{data.frame} of _one_ node or edge.
#' @param ... any column.
#'
#' @section Functions:
#' \itemize{
#' \item{\code{sg_read_nodes_p} read nodes.}
#' \item{\code{sg_read_edges_p} read edges.}
#' \item{\code{sg_read_exec_p} send read nodes and edges to JavaScript front end.}
#' }
#'
#' @examples
#' library(shiny)
#'
#' ui <- fluidPage(
#' actionButton("add", "add nodes & edges"),
#' sigmajsOutput("sg")
#' )
#'
#' server <- function(input, output, session){
#'
#' nodes <- sg_make_nodes()
#' edges <- sg_make_edges(nodes)
#'
#' output$sg <- renderSigmajs({
#' sigmajs() %>%
#' sg_nodes(nodes, id, label, color, size) %>%
#' sg_edges(edges, id, source, target) %>%
#' sg_layout()
#' })
#'
#' i <- 10
#'
#' observeEvent(input$add, {
#' new_nodes <- sg_make_nodes()
#' new_nodes$id <- as.character(as.numeric(new_nodes$id) + i)
#' i <<- i + 10
#' ids <- 1:(i)
#' new_edges <- data.frame(
#' id = as.character((i * 2 + 15):(i * 2 + 29)),
#' source = as.character(sample(ids, 15)),
#' target = as.character(sample(ids, 15))
#' )
#'
#' sigmajsProxy("sg") %>%
#' sg_force_kill_p() %>%
#' sg_read_nodes_p(new_nodes, id, label, color, size) %>%
#' sg_read_edges_p(new_edges, id, source, target) %>%
#' sg_read_exec_p() %>%
#' sg_force_start_p() %>%
#' sg_refresh_p()
#' })
#'
#' }
#'
#' if(interactive()) shinyApp(ui, server)
#'
#' @return The \code{proxy} object.
#'
#' @name read
#' @export
sg_read_nodes_p <- function(proxy, data, ...){
.test_proxy(proxy)
# build data
nodes <- data %>%
.build_data(...) %>%
.check_ids() %>%
.check_x_y() %>%
.as_list()
proxy$message$data$nodes <- nodes
return(proxy)
}
#' @rdname read
#' @export
sg_read_edges_p <- function(proxy, data, ...){
.test_proxy(proxy)
# build data
edges <- data %>%
.build_data(...) %>%
.check_ids() %>%
.check_x_y() %>%
.as_list()
proxy$message$data$edges <- edges
return(proxy)
}
#' @rdname read
#' @export
sg_read_exec_p <- function(proxy){
.test_proxy(proxy)
proxy$message$id <- proxy$id
if(is.null(proxy$message$data$edges))
proxy$message$data$edges <- list()
if(is.null(proxy$message$data$nodes))
proxy$message$data$nodes <- list()
proxy$session$sendCustomMessage("sg_read_exec_p", proxy$message)
return(proxy)
}
#' Batch read
#'
#' Read nodes and edges by batch with a delay.
#'
#' @param proxy An object of class \code{sigmajsProxy} as returned by \code{\link{sigmajsProxy}}.
#' @param data A \code{data.frame} of nodes or edges to add to the graph.
#' @param ... any column.
#' @param delay Column name of containing batch identifier.
#' @param refresh Whether to refresh the graph after each batch (\code{delay}) has been added to the graph.
#' Note that this will also automatically restart any running force layout.
#'
#' @details Add nodes and edges with \code{sg_read_delay_nodes_p} and \code{sg_read_delay_edges_p} then execute (send to JavaScript end) with \code{sg_read_delay_exec_p}.
#'
#' @examples
#' library(shiny)
#'
#' ui <- fluidPage(
#' actionButton("add", "add nodes & edges"),
#' sigmajsOutput("sg")
#' )
#'
#' server <- function(input, output, session){
#'
#' output$sg <- renderSigmajs({
#' sigmajs()
#' })
#'
#' observeEvent(input$add, {
#' nodes <- sg_make_nodes(50)
#' nodes$batch <- c(
#' rep(1000, 25),
#' rep(3000, 25)
#' )
#'
#' edges <- data.frame(
#' id = 1:80,
#' source = c(
#' sample(1:25, 40, replace = TRUE),
#' sample(1:50, 40, replace = TRUE)
#' ),
#' target = c(
#' sample(1:25, 40, replace = TRUE),
#' sample(1:50, 40, replace = TRUE)
#' ),
#' batch = c(
#' rep(1000, 40),
#' rep(3000, 40)
#' )
#' ) %>%
#' dplyr::mutate_all(as.character)
#'
#' sigmajsProxy("sg") %>%
#' sg_force_start_p() %>%
#' sg_read_delay_nodes_p(nodes, id, color, label, size, delay = batch) %>%
#' sg_read_delay_edges_p(edges, id, source, target, delay = batch) %>%
#' sg_read_delay_exec_p() %>%
#' sg_force_stop_p()
#' })
#'
#' }
#'
#' if(interactive()) shinyApp(ui, server)
#'
#' @return The \code{proxy} object.
#'
#' @name read-batch
#' @export
sg_read_delay_nodes_p <- function(proxy, data, ..., delay){
.test_proxy(proxy)
if(missing(delay) || missing(data))
stop("missing data or delay", call. = FALSE)
delay <- deparse(substitute(delay))
# build data
nodes <- data %>%
.build_data(..., delay = delay) %>%
.check_ids() %>%
.check_x_y() %>%
split(.[["delay"]]) %>%
purrr::map(.as_list)
proxy$message$data$nodes <- nodes
return(proxy)
}
#' @rdname read-batch
#' @export
sg_read_delay_edges_p <- function(proxy, data, ..., delay){
.test_proxy(proxy)
if(missing(delay) || missing(data))
stop("missing data or delay", call. = FALSE)
delay <- deparse(substitute(delay))
# build data
edges <- data %>%
.build_data(..., delay = delay) %>%
.check_ids() %>%
.check_x_y() %>%
split(.[["delay"]]) %>%
purrr::map(.as_list)
proxy$message$data$edges <- edges
return(proxy)
}
#' @rdname read-batch
#' @export
sg_read_delay_exec_p <- function(proxy, refresh = TRUE){
.test_proxy(proxy)
proxy$message$id <- proxy$id
if(is.null(proxy$message$data$edges))
proxy$message$data$edges <- list()
if(is.null(proxy$message$data$nodes))
proxy$message$data$nodes <- list()
proxy$message$data <- purrr::map2(proxy$message$data$nodes, proxy$message$data$edges, .grp) %>%
unname()
proxy$message$refresh <- refresh
proxy$session$sendCustomMessage("sg_read_bacth_exec_p", proxy$message)
return(proxy)
}
#' Animate
#'
#' Proxy to dynamically animate an already existing graph.
#'
#' @inheritParams sg_animate
#' @param proxy An object of class \code{sigmajsProxy} as returned by \code{\link{sigmajsProxy}}.
#'
#' @details You can animate, \code{x}, \code{y}, \code{size} and \code{color}.
#'
#' @note You have to make sure that all the columns you want to animate to
#' (e.g. \code{to_x}, \code{to_size}) are also provided as arguments when you
#' create the graph with \code{sigmajs() \%>\% sg_nodes()}.
#'
#' @seealso \code{\link{sg_animate}}
#'
#' @examples
#' \dontrun{
#' # generate graph
#' nodes <- sg_make_nodes(20)
#' edges <- sg_make_edges(nodes)
#'
#' # add transition
#' n <- nrow(nodes)
#' nodes$to_x <- runif(n, 5, 10)
#' nodes$to_y <- runif(n, 5, 10)
#' nodes$to_size <- runif(n, 5, 10)
#'
#' # in server function:
#' output$my_sigmajs_id <- renderSigmajs({
#' sigmajs() %>%
#' sg_nodes(nodes, id, label, size, color, to_x, to_y, to_size) %>%
#' sg_edges(edges, id, source, target)
#' })
#'
#' observeEvent(input$button, {
#' sigmajsProxy("my_sigmajs_id") %>%
#' sg_animate_p(mapping = list(x = "to_x", y = "to_y", size = "to_size"),
#' options = list(duration = 1000), delay = 0)
#' })
#' }
#'
#' @return The \code{proxy} object.
#'
#' @rdname animation_p
#' @export
sg_animate_p <- function(proxy, mapping, options = list(easing = "cubicInOut"), delay = 5000) {
if (missing(proxy) || missing(mapping))
stop("missing proxy or mapping", call. = FALSE)
.test_proxy(proxy)
message <- list(id = proxy$id, mapping = mapping, options = options, delay = delay) # create message
proxy$session$sendCustomMessage("sg_animate_p", message)
return(proxy)
}