
recordPlot
and
replayPlot
to record image
frames and replay the animation respectively.Replay the animation
ani.record(reset = FALSE, replay.cur = FALSE)ani.replay(list)
TRUE
, the recording list will be
cleared, otherwise new plots will be appended to the
existing list of recorded plotsreset
and replay.cur
to
TRUE
so that low-level plotting changes can be
captured by off-screen graphics devices without storing
all the plots in memory; see ani.record
will be usedNULL
.ani.record
uses
recordPlot
to record the plots
when any changes are made on the current plot. For a
graphical device to be recordable, you have to call
dev.control('enable')
before plotting. ani.replay
can replay the recorded plots as
an animation. Moreover, we can convert the recorded plots
to other formats too, e.g. use saveHTML
and
friends.
The recorded plots are stored as a list in
.ani.env$.images
, which is the default value to be
passed to ani.replay
; .ani.env
is an
invisible environment
created when
this package is loaded, and it will be used to store some
commonly used objects such as animation options
(ani.options
).
recordPlot
and
replayPlot
;
ani.pause
library(animation)
n = 20
x = sort(rnorm(n))
y = rnorm(n)
## set up an empty frame, then add points one by one
par(bg = "white") # ensure the background color is white
plot(x, y, type = "n")
ani.record(reset = TRUE) # clear history before recording
for (i in 1:n) {
points(x[i], y[i], pch = 19, cex = 2)
ani.record() # record the current frame
}
## now we can replay it, with an appropriate pause between frames
oopts = ani.options(interval = 0.5)
ani.replay()
## or export the animation to an HTML page
saveHTML(ani.replay(), img.name = "record_plot")
## record plots and replay immediately
saveHTML({
dev.control("enable") # enable recording
par(bg = "white") # ensure the background color is white
plot(x, y, type = "n")
for (i in 1:n) {
points(x[i], y[i], pch = 19, cex = 2)
ani.record(reset = TRUE, replay.cur = TRUE) # record the current frame
}
})
ani.options(oopts)
Run the code above in your browser using DataLab