Skip to content

Commit

Permalink
Add proxy function sg_animate_p...
Browse files Browse the repository at this point in the history
... to enable triggering animations from Shiny.
  • Loading branch information
sgrubsmyon committed Dec 3, 2020
1 parent 5666c1b commit 808f562
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -12,6 +12,7 @@ export(sg_add_nodes)
export(sg_add_nodes_delay_p)
export(sg_add_nodes_p)
export(sg_animate)
export(sg_animate_p)
export(sg_button)
export(sg_change_edges_p)
export(sg_change_nodes_p)
Expand Down
59 changes: 59 additions & 0 deletions R/graph_proxies.R
Expand Up @@ -719,3 +719,62 @@ sg_read_delay_exec_p <- function(proxy, refresh = TRUE){
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)
}
12 changes: 12 additions & 0 deletions inst/htmlwidgets/sigmajs.js
Expand Up @@ -1131,6 +1131,18 @@ if (HTMLWidgets.shinyMode) {
}
);

// animate
Shiny.addCustomMessageHandler('sg_animate_p',
function (message) {
var s = get_sigma_graph(message.id);
if (typeof s != 'undefined') {
setTimeout(function() {
sigma.plugins.animate(s, message.mapping, message.options);
}, message.delay);
}
}
);

// noverlap
Shiny.addCustomMessageHandler('sg_noverlap_p',
function (message) {
Expand Down
66 changes: 66 additions & 0 deletions man/animation_p.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 808f562

Please sign in to comment.