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=1,      # the time interval of the animation (unit in seconds)
                   ani.width=480,   # dimension of the animation
                   ani.height=480,
                   imgdir="images", # directory (a relative path) for images when creating HTML animation pages
                   htmlfile="index.html",      # name of the produced html file
                   autobrowse = interactive(), # whether auto-browse the animation after it is created
                   loop = TRUE,     # whether to iterate or not
                   title = "",      # the title of the html page
                   description = "" # the description of the html page
                  ) 

HTMLs

Make HTMLs with function saveHTML. It produces a new html file with the images in folder images:

A package example of the Buffon’s needle:

## create an HTML animation page
saveHTML({
     par(mar = c(3, 2.5, 1, 0.2), pch = 20, mgp = c(1.5, 0.5, 0))
     ani.options(nmax = ifelse(interactive(), 300, 10), interval = 0.1)
     buffon.needle(type = "S", redraw = FALSE)
   }, 
   img.name = "buffon.needle", 
   htmlfile = "buffon.needle.html", 
   ani.height = 500, 
   ani.width = 600, 
   title = "Simulation of Buffon's Needle", 
   description = c("There are three graphs made in each step: the", 
                   "top-left, one is a simulation of the scenario, the top-right one", 
                   "is to help us understand the connection between dropping needles", 
                   "and the mathematical method to estimate pi, and the bottom one is", 
                   "the result for each dropping.")
)

ani.options(oopt)

Check the result.

GIFs

To have animated GIFs it’s necessary to install ImageMagick.

Function saveGIF produces an animated GIF:

## [1] FALSE

However, saveGIFmight not work properly. In that case you could do it yourself. This takes the previous made pictures from saveHTML and create the animated GIF:

im.convert(files="images/dnorms*.png",
           output="images/dnorm_animation2.gif",
           cmd.fun=system)

We can present the Buffon’s needle here too:

im.convert(files="images/buffon.needle*.png",
           output="images/buffon.needle.gif",
           cmd.fun=system)