It depends a little bit on what your end goal is.
Quick and dirty hack for real-time demonstrations
Sys.sleep(seconds) in a loop where
seconds indicates the number of seconds between frames is a viable option. You'll need to set the
ylim parameters in your call to
plot to make things behave as expected.
Here's some simple demonstration code.
# Just a quick test of Sys.sleep() animation
x <- seq(0,2*pi, by=0.01)
y <- sin(x)
n <- 5
pause <- 0.5
ybnds <- quantile(n*y, probs=c(0,1))
# Draw successively taller sinewaves with a gradually changing color
for( i in 1:n )
plot(x, i*y, type="l", lwd=2, ylim=ybnds, col=topo.colors(2*n)[i])
This works pretty well, especially using X-Windows as the windowing system. I've found that Mac's
quartz() does not play nice, unfortunately.
If you need something that can be redistributed, posted on a webpage, etc., look at the
write.gif function in the caTools package. Displaying help on
write.gif gives several nice examples, including a couple of animations—one with a quite nice example using the Mandelbrot set.
See also here and here.
More fine-tuned control and fancier animations
There is an animation package that looks pretty capable. I haven't used it myself, though, so I can't give any real recommendations either way.
I have seen a few good examples of output from this package and they look pretty nice. Perhaps one of the "highlights" is the ability to embed an animation in a PDF.