[R-sig-eco] auto graphics saving

Gavin Simpson gavin.simpson at ucl.ac.uk
Sat Jul 5 01:51:07 CEST 2008


On Fri, 2008-07-04 at 18:45 +0100, Gavin Simpson wrote:
> On Fri, 2008-07-04 at 10:00 -0700, Megan O'Rourke wrote:
> > I am working on a spatial simulation in R and would like to present a
> > movie of my simulation within a ppt presentation.  With my current
> > knowledge, I can activate "history" in the graphic output during my
> > simulation loop and then manually save individual graphical frames of
> > the simulation as picture files that I import into a movie making
> > program.  Is there an easier way to save a movie in R?  
> >    
> >   Along that same line of questioning, is there any way to save
> > consecutive data files automatically from within a loop?  For example,
> > I am creating consecutive matrices in a loop that I store within a
> > three-dimensional array within the R program.  I would like to write a
> > line in my loop that I imagine would say something like:
> > write.table(X, "c:\\blah\\index.txt") and the loop would output
> > consecutive 2-dimensional layers of my array in separate files.  If I
> > add a line similar to above, R creates one file that it rewrites over
> > every step of the loop.  Is there a way to automate the data saving
> > process?

Sorry, completely misread your second question, nothing to do with
multiple graphics files. Something like

path.name <- "c:\\blah\\"
for(i in 1:10) {
  dat <- matrix(rnorm(20), ncol = 4)
  ## some other code to do what you do
  write.csv(dat, file = paste(path.name, "index_", i, ".txt", sep = ""))
}

Will do what you want. The key is building the file name up from the
path to the directory where you want to write the files, then a file
name containing an indicator, which above we take from the loop counter,
i. paste() creates the filename from the various pieces specified.

Here I use write.csv so I don't have to specify all the file parameters
to load into Excel. But any of the write.table family of function can be
used here.

HTH

G

> 
> Here is one way, that uses ImageMagick to produce the animated gif:
> 
> ## generate some random data
> set.seed(123)
> ydat <- matrix(runif(10000), ncol = 500)
> xdat <- matrix(runif(10000), ncol = 500)
> 
> jpeg("myplot%03d.jpeg")
> for(i in seq_len(nrow(ydat))) 
>     plot(xdat[1:i,], ydat[1:i,], ylim = c(0,1), xlim = c(0,1))
> dev.off()
> 
> This I think answers your 2nd question as well. Note the %03d bit in the
> file name. R will create plots with names myplotxxx.jpeg where xxx is
> incremented from 001 by 1 each time a plot is drawn on the device. We
> produce a loop and plot something, each call to plot gives a new figure.
> 
> Then we need to animate the images: With ImageMagick installed and in
> your path:
> 
> convert -delay 5 -loop 0 *.jpeg animation.gif
> 
> will take the jpeg files and squash them into a animated gif of the
> given name. On my system the resulting GIF was 1.1MB in size.
> 
> For this example, the output quality is not great when I loaded it into
> OpenOffice.org Impress (on Linux so no MS PPT here), but I didn't
> produce good quality jpegs nor tweak anything in the ImageMagick convert
> programme. That is left as an exercise for the reader ;-)
> 
> If the quality isn't good enough, then at least the way I create the
> jpegs above will allow you to create the files automagically that you
> require for the movie making software you mention. This general approach
> works for the other graphics devices as well, like png if you prefer
> them.
> 
> HTH
> 
> G
> 
> >    
> >   Thanks,
> >    
> >   Megan O'Rourke
> >   PhD student, Cornell University 
> > 
> >        
> > 	[[alternative HTML version deleted]]
> > 
> > _______________________________________________
> > R-sig-ecology mailing list
> > R-sig-ecology at r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-ecology



More information about the R-sig-ecology mailing list