Extends the 'base' graphics functions to support frame-by-frame animation and keyframes animation.
This function differs from the event
function in that events
registered through simple_event
do not require R at deployment to work.
connection
A handle for the WebSocket connection.
ready_state
The ready state of the connection.
shiny
TRUE or FALSE; whether the device is used with in a 'Shiny' app.
session
A 'Shiny' session.
virtual_meta
A list of device metadata.
virtual_session
A virtual session simulated with 'V8'.
event_handlers
A named list of user-defined functions for handling events.
new()
Constructor of the device
animate$new(width, height, id = "SVG_1", launch.browser, ...)
width
An integer; the width in pixels.
height
An integer; the height in pixels.
id
A character string; the id assigned to the device.
launch.browser
A function to launch a viewer; two options are
rstudioapi::viewer
and utils::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 = TRUE
to use the virtual
device, shiny = TRUE
for shiny application; everything else will be
passed to the SVG element that hosts the visualisation.
\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)
}
svg()
Initialise a SVG element
objects()
Add HTML objects to a plot
plot()
Generic X-Y plotting
points()
Add points to a plot
x
The x coordinates of the points.
y
The y coordinates of the points.
...
Additional graphical parameters.
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.
lines()
Add line segments / paths to a plot
abline()
Add straight lines to a plot
axis()
Add an axis to a plot
text()
Add text to a plot
image()
Add background image to a plot
event()
Attach an interactive event to an element
selector
A character string; a CSS selector.
event_type
A character string; the event type. For example, "click", "mouseover", "mouseout". See more options at https://www.w3schools.com/jsref/dom_obj_event.asp.
callback
A function, to be called when the event is triggered. The function should take an argument to receive the data from the browser end.
chain()
Chain a transition after another.
callback
A function, to be called when the event is triggered. The function should take an argument to receive the data from the browser end.
\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)
}
simple_event()
Attach a captured event to an element
selector
A character string; a CSS selector.
event_type
A character string; the event type. For example, "click", "mouseover", "mouseout". See more options at https://www.w3schools.com/jsref/dom_obj_event.asp.
method
A character string; the name of a device function (e.g. "points").
param
A named list of arguments to be called with.
par()
Set the graphical parameters
remove()
Remove elements from the active SVG element
record()
Record an animated plot as a MP4 video
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 `animate$new`
## ------------------------------------------------
# \donttest{
library(animate)
device <- animate$new(400, 400) # Launch a WebSocket server
#> Server started.
attach(device)
#> The following objects are masked from package:graphics:
#>
#> abline, axis, image, lines, par, plot, points, text
#> The following object is masked from package:grDevices:
#>
#> svg
#> The following object is masked from package:methods:
#>
#> initialize
#> The following objects are masked from package:base:
#>
#> objects, plot, remove
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)
#> Device is not yet available.
new_y <- 10:1
plot(x, new_y, id = id, transition = TRUE) # Use transition
#> Device is not yet available.
off()
#> Server stopped.
detach(device)
# }
## ------------------------------------------------
## Method `animate$chain`
## ------------------------------------------------
# \donttest{
library(animate)
device <- animate$new(600, 600) # Launch a WebSocket server
#> Server started.
attach(device)
#> The following objects are masked from package:graphics:
#>
#> abline, axis, image, lines, par, plot, points, text
#> The following object is masked from package:grDevices:
#>
#> svg
#> The following object is masked from package:methods:
#>
#> initialize
#> The following objects are masked from package:base:
#>
#> objects, plot, remove
par(xlim = c(0, 10), ylim = c(0, 10))
#> Device is not yet available.
plot(1:10, 1:10, id = 1:10)
#> Device is not yet available.
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))
})
))
#> Device is not yet available.
par(xlim = NULL, ylim = NULL) # Reset `xlim` and `ylim` in `par`
#> Device is not yet available.
off()
#> Server stopped.
detach(device)
# }