From 808f5627dd2b8381b43ddc0e5f992ebb06db8004 Mon Sep 17 00:00:00 2001 From: Markus Voge Date: Thu, 3 Dec 2020 16:45:11 +0100 Subject: [PATCH] Add proxy function sg_animate_p... ... to enable triggering animations from Shiny. --- NAMESPACE | 1 + R/graph_proxies.R | 59 +++++++++++++++++++++++++++++++++ inst/htmlwidgets/sigmajs.js | 12 +++++++ man/animation_p.Rd | 66 +++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 man/animation_p.Rd diff --git a/NAMESPACE b/NAMESPACE index ecacf93..fa6b94c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/graph_proxies.R b/R/graph_proxies.R index 95ecf68..8487b2d 100644 --- a/R/graph_proxies.R +++ b/R/graph_proxies.R @@ -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) +} diff --git a/inst/htmlwidgets/sigmajs.js b/inst/htmlwidgets/sigmajs.js index fe09631..9d268c5 100644 --- a/inst/htmlwidgets/sigmajs.js +++ b/inst/htmlwidgets/sigmajs.js @@ -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) { diff --git a/man/animation_p.Rd b/man/animation_p.Rd new file mode 100644 index 0000000..facca70 --- /dev/null +++ b/man/animation_p.Rd @@ -0,0 +1,66 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/graph_proxies.R +\name{sg_animate_p} +\alias{sg_animate_p} +\title{Animate} +\usage{ +sg_animate_p( + proxy, + mapping, + options = list(easing = "cubicInOut"), + delay = 5000 +) +} +\arguments{ +\item{proxy}{An object of class \code{sigmajsProxy} as returned by \code{\link{sigmajsProxy}}.} + +\item{mapping}{Variables to map animation to.} + +\item{options}{Animations options.} + +\item{delay}{Delay in milliseconds before animation is triggered.} +} +\value{ +The \code{proxy} object. +} +\description{ +Proxy to dynamically animate an already existing graph. +} +\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()}. +} +\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) +}) +} + +} +\seealso{ +\code{\link{sg_animate}} +}