Animating

library(animation)

ani.options control the behaviour of the animation. Some egs with default values:

oopt = ani.options(
  nmax=50,         # maximum number of steps in a loop
  interval=0.1,      # the time interval of the animation (unit in seconds)
  ani.width=480,   # dimension of the animation
  ani.height=480,
  imgdir="images", # directory (relative path) for images when creating HTML page
  htmlfile="index.html",      # name of the produced html file
  autobrowse = interactive(), # whether auto-browse the animation after created
  loop = TRUE,     # whether to iterate or not
  title = "",      # the title of the html page
  description = "" # the description of the html page
) 

Creating an animated GIF

Examples from here.

library(animation)
 
saveGIF({
  
  for(i in 1:100) {
    curve(sin(x), from = -5+(i*0.05), to = 5 + (i * 0.05), col = "red", ylab = "")
    curve(cos(x), from = -5+(i*0.05), to = 5 + (i * 0.05), add = TRUE, col = "blue", ylab = "")
    legend("topright", legend=c("sin(x)","cos(x)"), fill=c("red","blue"), bty="n")
  }
  ani.options(oopt)
  
}, movie.name="sine2D_animation.gif")