Animated Statistics Using R (to be moved to AniWiki!)
It's difficult for me to choose an appropriate device for animated pictures. At last I turned to HTML & JavaScript.
About GIF Format

I've read some stories on the GIF format since a long time ago, among which the instruction from GNU (Why There Are No GIF files on GNU Web Pages) and the website "Burn All GIFs" impressed me most.

Although the patent for GIF has already expired in the USA, Canada, Japan, and the European Union till now, I still have some fear about it. Anyway, we can ultimately use it legally now. For this site, it's inevitable to make use of a certain kind of image format (or video format), and surely GIF may be one of the available choices.

However, currently it doesn't seem to be easy to create GIF animations using R conveniently. To the best of my knowledge, I only know there are two functions read.gif() and write.gif() in the package "caTools" which are able to deal with images in GIF format. The most critical limitation is that this package treats GIF images as 2D matrices (or 3D arrays when images contain several frames, i.e. animated), which restricted the use of GIF format to a quite small area, or more clearly speaking, only color images can be used -- for a matrix named image, the columns and rows determine where to draw, while the value image[i, j] determine which color to use.

Sure, what we need is to combine several images together and specify a certain time delay to constitute an animated image. Thus the package "caTools" is not appropriate for a general purpose of making animations. In fact I've communicated with Jarek Tuszynski who is the author of "caTools", and he didn't want to expand his functions to a general purpose of dealing with GIF images. Therefore, we can only limit GIF animations using write.gif() from "caTools" to the quite narrow field, i.e. producing animated color images.

Currently what I can do about this format is to make use of a special software "ImageMagick" to convert several animation frames into a single GIF file, and this idea has been implemented in the function saveMovie() in my package animation.

PDF Documents

The PDF is the file format created by Adobe Systems in 1993 for document exchange. PDF is used for representing two-dimensional documents in a device-independent and display resolution-independent fixed-layout document format. Usually PDF files are my favorite when generating statistical figures, because vector plots are certainly superior to bitmaps for graphics in statistics due to the special characteristic of statistical graphs (there are usually numbers "behind" images).

"Then, does PDF has anything to do with animations?" You may ask. The answer is "Yes"! My idea is as follows:

First creat a sequence of plots in a single PDF device using pdf() in package grDevices (you need to specify the argument onefile = TRUE, but this is the default value, so you may ignore it), and the rest work is easy. Either of the following two approaches will do: (1) Open the PDF file and view it in fullscreen mode, then press PageDown again and again with an appropriate time delay to enjoy a manual animation (seems silly...); (2) If you have installed Adobe Acrobat Professional in your computer, just set proper auto filp time interval of pages (Document -> Pages -> Set Page Transitions), then play your PDF file in fullscreen mode.

However, the first method is apparently somewhat silly and inconvenient, while the second one need an expensive shareware. As a result, making animations through the way of PDF documents is unrealistic to some degree.

Other R Graphics Devices

The R package grDevices has offered a variety of graphics devices, and it's really a great help when we need to produce single image files -- there are several choices such as PNG, JPEG, BMP, PDF, PS, TeX/LaTeX and WMF, etc. All of them work very well when producing images files one by one, but the essential problem is still that none of them is able to make animations directly. At most what we can do is to produce a sequence of images; it's difficult to animate them by R alone.

Nevertheless, what I used most in the past are just Windows graphics devices (under Windows XP) or X Window System graphics devices (under Ubuntu) inside R to make animations. Again, there is an obvious drawback: it's inconvenient for users who don't have R installed in their computers to watch the animations, as the pictures are displayed inside R and we need an independent media to show our animations.

For users who have installed R, you may easily make animations just using Sys.sleep() in a loop, as what I did on top of this page. Obviously, I intentionally used Sys.sleep() to make the loop to run more slowly so that we can see the whole process clearly. (Note that such a way of animation can only be implemented in a Windows graphics device or X Window System graphics device in side R.)

HTML and JavaScript

So... After my boring introduction to my previous trials, now I'll give my last solution: to use HTML with the help of JavaScript to complete the illustration.

Why use HTML & JavaScript? I believe there are at least three reasons: (1) R already has functions for reading and writing ASCII files, so we can create HTML files easily, e.g. use cat(); (2) Although R has no devices for GIF, there are still many other "static" graphics formats which can be well shown in web pages; (3) Generally speaking, no additional programs are needed in order to display the animations, as long as you have a web browser that supports JavaScript -- I believe most computers can meet such a simple requirement.

And how? Below is some sample code:

<body onload="displayImage()">
<img id="myimg" src=""/>
<script language="javascript">
    var imglist = new Array("plot1.png", "plot2.png", "plot3.png", "plot4.png");
    var n = -1;
    function displayImage() {
        ++n;
        if (n == imglist.length)
           n = 0;
        document.all("myimg").src = imglist[n];
        setTimeout("displayImage()",1000); //every 1 seconds
    }
</script>
</body>

We just have to define an array of images and then change the src attribute of the img tag, in the mean time, we should specify a time interval for the display function. In the end, we can freely use animations in web pages! All details are in this page (there are several modifications in order to improve the quality of animations).

Any More Ideas?

I'd like to listen to other ideas on how to make animations using R conveniently. If you have one (or some), please share it (them) with me. Thanks!