| animate {animate} | R Documentation |
A web-based graphics device for animated visualisations
Description
Extends the 'base' graphics functions to support frame-by-frame animation and keyframes animation.
Public fields
connectionA handle for the WebSocket connection.
ready_stateThe ready state of the connection.
shinyTRUE or FALSE; whether the device is used with in a 'Shiny' app.
sessionA 'Shiny' session.
virtual_metaA list of device metadata.
virtual_sessionA virtual session simulated with 'V8'.
event_handlersA named list of user-defined functions for handling events.
Methods
Public methods
Method new()
Constructor of the device
Usage
animate$new(width, height, id = "SVG_1", launch.browser, ...)
Arguments
widthAn integer; the width in pixels.
heightAn integer; the height in pixels.
idA character string; the id assigned to the device.
launch.browserA function to launch a viewer; two options are
rstudioapi::viewerandutils::browseURL. It defaults to the first option if the user is using RStudio and to the second option otherwise. The default applies to interactive session only....Additional arguments. Use
virtual = TRUEto use the virtual device,shiny = TRUEfor shiny application; everything else will be passed to the SVG element that hosts the visualisation.
Examples
\donttest{
library(animate)
device <- animate$new(400, 400) # Launch a WebSocket server
attach(device)
x <- 1:10
y <- 1:10
id <- new_id(x) # Give each point an ID: c("ID-1", "ID-2", ..., "ID-10")
plot(x, y, id = id)
new_y <- 10:1
plot(x, new_y, id = id, transition = TRUE) # Use transition
off()
detach(device)
}
Method off()
Switch off the device; this function closes the WebSocket connection.
Usage
animate$off()
Method send()
Send commands to device
Usage
animate$send(message)
Arguments
messageThe message to send to the device.
Method set_max_stacksize()
Set the maximum size of the stack
Usage
animate$set_max_stacksize(n)
Arguments
nThe number of commands the plot stack can hold. Use -1 for unlimited number of commands.
Method svg()
Initialise a SVG element
Usage
animate$svg(width = 800, height = 600, ...)
Arguments
widthWidth of the canvas in pixels.
heightHeight of the canvas in pixels.
...Additional parameters. Some commonly used parameters are
idandroot.idassigns an id to the SVG element for future reference;rootspecifies the DOM element to insert the SVG element into.
Method bars()
Add bars to a plot
Usage
animate$bars(x, y, w, h, ...)
Arguments
xThe x coordinates of the bars.
yThe y coordinates of the bars.
wThe width of the bars.
hThe height of the bars.
...Additional graphical parameters.
Method objects()
Add HTML objects to a plot
Usage
animate$objects(x, y, w, h, content, ...)
Arguments
xThe x coordinates of the objects.
yThe y coordinates of the objects.
wThe width of the objects.
hThe height of the objects.
contentThe content of the objects; the HTML string.
...Additional graphical parameters.
Method plot()
Generic X-Y plotting
Usage
animate$plot(x, y, type = "p", ...)
Arguments
xThe x coordinates of the data.
yThe y coordinates of the data.
typeType of the plot; one of 'p' and 'l'.
...Additional graphical parameters.
Method points()
Add points to a plot
Usage
animate$points(x, y, ...)
Arguments
xThe x coordinates of the points.
yThe y coordinates of the points.
...Additional graphical parameters.
Details
Options for the "pch" parameter: "circle", "plus", "diamond", "square", "star", "triangle", "wye", "triangle_down", "triangle_left", "triangle_right", "diamond_alt", "diamond_square", "pentagon", "hexagon", "hexagon_alt", "octagon", "octagon_alt", "cross".
The unit of the "cex" parameter is squared pixels, corresponding to how much pixel space the symbol would cover. The convention comes from the 'D3' library, and the choice is (believed) to make plots visually consistent across the different symbols.
Method lines()
Add line segments / paths to a plot
Usage
animate$lines(x, y, ...)
Arguments
xThe x coordinates of the line.
yThe y coordinates of the line.
...Additional graphical parameters.
Method abline()
Add straight lines to a plot
Usage
animate$abline(a, b, h, v, ...)
Arguments
aThe intercept.
bThe slope.
hThe y-value(s) for horizontal line(s).
vThe x-value(s) for vertical line(s).
...Additional graphical parameters.
Method axis()
Add an axis to a plot
Usage
animate$axis(x, ...)
Arguments
xThe x coordinates of the text.
...Additional graphical parameters.
yThe y coordinates of the text.
labelsThe text.
Method text()
Add text to a plot
Usage
animate$text(x, y, labels, ...)
Arguments
xThe x coordinates of the text.
yThe y coordinates of the text.
labelsThe text.
...Additional graphical parameters.
Method image()
Add background image to a plot
Usage
animate$image(href, width, height, ...)
Arguments
hrefThe link to the image.
widthThe width of the image.
heightTh height of the image.
...Additional graphical parameters.
Method event()
Attach an interactive event to an element
Usage
animate$event(selector, event_type, callback)
Arguments
selectorA character string; a CSS selector.
event_typeA character string; the event type. For example, "click", "mouseover", "mouseout". See more options at https://www.w3schools.com/jsref/dom_obj_event.asp.
callbackA function, to be called when the event is triggered. The function should take an argument to receive the data from the browser end.
Method chain()
Chain a transition after another.
Usage
animate$chain(callback)
Arguments
callbackA function, to be called when the event is triggered. The function should take an argument to receive the data from the browser end.
Examples
\donttest{
library(animate)
device <- animate$new(600, 600) # Launch a WebSocket server
attach(device)
par(xlim = c(0, 10), ylim = c(0, 10))
plot(1:10, 1:10, id = 1:10)
points(1:10, sample(10, 10), id = 1:10,
transition = list(
duration = 1000,
on = chain(function(message) {
print(message)
points(1:10, sample(10, 10), id = 1:10, bg = "green",
transition = list(duration = 2000))
})
))
par(xlim = NULL, ylim = NULL) # Reset `xlim` and `ylim` in `par`
off()
detach(device)
}
Method simple_event()
Attach a captured event to an element
Usage
animate$simple_event(selector, event_type, method, param)
Arguments
selectorA character string; a CSS selector.
event_typeA character string; the event type. For example, "click", "mouseover", "mouseout". See more options at https://www.w3schools.com/jsref/dom_obj_event.asp.
methodA character string; the name of a device function (e.g. "points").
paramA named list of arguments to be called with.
Method set()
Set the active device to a SVG element
Usage
animate$set(device_id)
Arguments
device_idA character vector; ID of the device.
Method par()
Set the graphical parameters
Usage
animate$par(...)
Arguments
...The graphical parameters
Method remove()
Remove elements from the active SVG element
Usage
animate$remove(id = NULL, selector = "*")
Arguments
idA character vector; the ID of the elements.
selectorA character vector; a CSS selector.
Method clear()
Remove all elements from the active SVG element
Usage
animate$clear()
Method delete()
Remove a SVG element
Usage
animate$delete(id = NULL)
Arguments
idA character string; the ID of the SVG. If not provided, remove the active SVG element. #' @description #' Perform a group of graphical operations to a plot #' @param ... Any number of graphical operations. group = function(...) self$send(Message("fn_group", c(...))) ,
Method import()
Import an animated plot
Usage
animate$import(setting)
Arguments
settingA JSON file exported from previous runs.
Method export()
Export an animated plot
Usage
animate$export(path = "./animate.json", handler = "browser")
Arguments
pathA character string; the file path to export to.
handler'r' or 'browser'; the program to handle the export operation.
Method record()
Record an animated plot as a MP4 video
Usage
animate$record()
Details
This function will prompt you to select a screen / window / tab to record. Once started, the recording can be stopped by using the stop button at the notification box, or clicking anywhere on the page near the device. Always confirm that the screen recording notification box is gone. The captured video will be downloaded right after the recording stops.
This uses web browsers' Media Streams API to record the screen
and return the captured frames as a video. The entire process runs locally.
The source file that provides this functionality can be found at
system.file("addons/screen_record.js", package = "animate").
This function is disabled for 'Shiny' app and R Markdown document.
This function does not work in the RStudio viewer. Please use the "show in new window" button to launch the page with a web browser.
See browser compatibility at: https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_Recording_API#browser_compatibility
See Media Streams API reference at: https://developer.mozilla.org/en-US/docs/Web/API/Media_Streams_API
Method observeAnimateEvent()
Event handler
Usage
animate$observeAnimateEvent(input)
Arguments
inputThe input object in the
serverfunction of a 'Shiny' app.
Method clone()
The objects of this class are cloneable with this method.
Usage
animate$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Note
This function differs from the event function in that events
registered through simple_event do not require R at deployment to work.
Examples
## ------------------------------------------------
## Method `animate$new`
## ------------------------------------------------
library(animate)
device <- animate$new(400, 400) # Launch a WebSocket server
attach(device)
x <- 1:10
y <- 1:10
id <- new_id(x) # Give each point an ID: c("ID-1", "ID-2", ..., "ID-10")
plot(x, y, id = id)
new_y <- 10:1
plot(x, new_y, id = id, transition = TRUE) # Use transition
off()
detach(device)
## ------------------------------------------------
## Method `animate$chain`
## ------------------------------------------------
library(animate)
device <- animate$new(600, 600) # Launch a WebSocket server
attach(device)
par(xlim = c(0, 10), ylim = c(0, 10))
plot(1:10, 1:10, id = 1:10)
points(1:10, sample(10, 10), id = 1:10,
transition = list(
duration = 1000,
on = chain(function(message) {
print(message)
points(1:10, sample(10, 10), id = 1:10, bg = "green",
transition = list(duration = 2000))
})
))
par(xlim = NULL, ylim = NULL) # Reset `xlim` and `ylim` in `par`
off()
detach(device)