Learn R Programming

animation (version 0.2-0)

savePNG: Save PNG image from the current graphical device

Description

Copy the current graphical device to a PNG device to generate a PNG image file.

Usage

savePNG(outdir = tempdir(), n = 1, width = 480, height = 480, ...)

Arguments

outdir
physical directory to store the PNG image (default in the temporary directory)
n
file name of the PNG image; can be either numeric or characters
width
width of PNG image
height
height of PNG image
...
other parameter passed to png

Value

  • None (invisible `NULL').

Details

Actually for Windows users, savePNG is almost identical to savePlot(type = "png"). However, there's no savePlot for Linux users, so this special funtion just acts as a wrapper for saving plots. The work behind this function is just to copy the plot in the current graphical device to a png device using dev.copy. To make animations, we need to specify the argument n as a sequence of integers so that we can ultimately get a series of image files named 1.png, 2.png, ... Thus the JavaScript in the HTML animation page will be able to find these image frames. Check the souce code of any animation functions to understand this approach.

See Also

ani.start, ani.stop

Examples

Run this code
# a plot save to file.path(getwd(), "1.png") 
plot(rnorm(100)) 
savePNG(getwd(), n = 1)

# a sequence of plots saved in the work directory 
# this is just how animations are produced 
for(i in 1:10){
  plot(rnorm(1), rnorm(1), xlim = c(-1, 1), ylim = c(-1, 1))
  savePNG(getwd(), n = i) 
}
# you might need file.remove(paste(1:10, ".png", sep = ""))

Run the code above in your browser using DataLab