Learn R Programming

animation (version 1.1-4)

saveMovie: Convert Images to A Single Animated Movie...

Description

Convert Images to A Single Animated Movie This function opens a graphical device first to generate a sequence of images based on expr, then makes use of the command convert in `ImageMagick' to convert these images to a single animated movie (in formats such as GIF and MPG, etc).

Usage

saveMovie(expr, interval=1, moviename="animation.gif", loop=0, dev=png,
    filename="Rplot", fmt="%03d", fileext="png", outdir=getwd(),
    convert="convert", cmd.fun, clean=TRUE, ani.first,
    para=par(no.readonly = TRUE), ...)

Arguments

expr
an expression to generate animations; use either the animation functions (e.g. brownian.motion()) in this package or a custom expression (e.g. for(i in 1:10) plot(runif(10), ylim = 0:1)).
interval
duration between animation frames (unit in seconds)
moviename
file name of the movie (with the extension)
loop
iterations of the movie; set iterations to zero to repeat the animation an infinite number of times, otherwise the animation repeats itself up to loop times (N.B. for GIF only!)
dev
a function for a graphical device such as png, jpeg and bmp, etc.
filename
file name of the sequence of images (`pure' name; without any format or extension)
fmt
a C-style string formatting command, such as %3d
fileext
the file extensions of the image frames
outdir
the directory for the movie frames
convert
the ImageMagick command to convert images (default to be convert, but might be imconvert under some Windows platforms); see the 'Note' section for details
cmd.fun
a function to invoke the OS command; by default, shell under Windows and system under other OS
clean
whether to delete the individual image frames
ani.first
an expression to be evaluated before plotting (this will be useful to set graphical parameters in advance, e.g. ani.first = par(pch = 20)
para
a list: the graphics parameters to be set before plotting; passed to par; note ani.first can override this argument
...
other arguments passed to the graphical device, such as height and width, ...

Value

  • An integer indicating failure (-1) or success (0) of the converting (refer to system and im.convert).

Details

This function calls im.convert to convert images to a single animation. The convenience of this function is that it can create a single movie file, however, two drawbacks are obvious too: (1) we need a special (free) software ImageMagick; (2) the speed of the animation cannot be conveniently controlled, as we have specified a fixed interval. Other approaches in this package may have greater flexibilities, e.g. the HTML approach (see ani.start).

References

http://www.imagemagick.org/script/convert.php http://animation.yihui.name/animation:start

See Also

im.convert, saveSWF, system, png, jpeg

Examples

Run this code
## make sure ImageMagick has been installed in your system 
saveMovie(for(i in 1:10) plot(runif(10), ylim = 0:1), loop = 1)
oopt = ani.options(nmax = 100)
saveMovie(brownian.motion(pch = 21, cex = 5, col = "red", bg = "yellow"),
interval = 0.1, width = 600, height = 600)
ani.options(oopt)

Run the code above in your browser using DataLab