JAnimateGIF | R Documentation |
Writes each frame to a PNG file, then combines them into a GIF file. Frames are created by calling a custom plot function. The PNG to GIF conversion is performed by a 3rd party R package or application.
JAnimateGIF(
videoFileName,
nFrames = NULL,
frameKeys = 1:nFrames,
plotFn,
frameRate = 30,
loop = 0,
method = c("auto", "magick-r", "gifski", "magick", "ffmpeg"),
optimize = FALSE,
progress = FALSE,
tmpDir = tempdir(TRUE),
...
)
videoFileName |
Name of the video file to be created. The file type is
inferred from the file extension, but must be GIF if |
nFrames |
Number of frames to be generated. You must specify one of
|
frameKeys |
Vector of keys to be passed to |
plotFn |
Function which is called once for each frame. It is called once
for each frame to be generated, with a single argument which is one of the
values from |
frameRate |
Play back frame rate - used to set the frame delay in the GIF file. |
loop |
Number of times animation should be played. 0 means loop infinitely. |
method |
Specify the library/tool used to convert from PNG to GIF:
|
optimize |
Only used if |
progress |
Only used if |
tmpDir |
Name of a directory to be used to create temporary files in. |
... |
Any additional arguments are passed to the |
You must have a suitable image conversion tool installed - see the documentation
of the argument method
for details. ImageMagick
installed, and the ImageMagick bin subdirectory must be in your PATH
environment variable. If ImageMagick is not installed, or is not in your PATH,
JAnimateGIF
will fail with an exception such as:
Error in system2("magick", c("convert", "jp*.png", "-delay", 100/frameRate, : '"magick"' not found
.
For a general discussion about creating an animation in R, see https://stackoverflow.com/questions/1298100/creating-a-movie-from-a-series-of-plots-in-r, or https://r-graph-gallery.com/animation.
The png to gif conversion can be quite time consuming for a large number of frames, so start with a small number of frames to ensure everything works as expected.
In theory, magick can create mpeg files, but I can't get it to work, so use
method="ffmpeg"
for file formats other than GIF.
The error message (as a character vector) from the ImageMack convert
command, or character(0)
(returned invisibly) on success.
JPlotToPNG
, JAnimateScenes
for smoothly animating changing parameters.
## Not run:
# Number of frames in the animation
nFrames <- 50
# A function to plot a coloured polygon
.plotPoly <- function(frame) {
# Created an empty plot
plot(NULL, xlim = c(-1, 1), ylim = c(-1, 1), asp = 1, xlab = "", ylab = "", axes = FALSE)
# Frame starts from 1, but a 1 cornered polygon is not very visually interesting
nCorners <- frame + 1
angles <- 2 * pi * (1:nCorners) / nCorners
f <- frame / nFrames
# Pick a colour based on the frame number
col <- rgb(sin(pi / 2 * f), cos(pi / 2 * f), sin(pi / 2 * (f + .5)))
# Draw a polygon
polygon(cos(angles), sin(angles), col = col, border = "black", lwd = 4)
}
JAnimateGIF("poly.gif", nFrames, plotFn = .plotPoly, frameRate = 10)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.