[R-sig-eco] auto graphics saving
Gavin Simpson
gavin.simpson at ucl.ac.uk
Fri Jul 4 19:45:56 CEST 2008
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?
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
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Dr. Gavin Simpson [t] +44 (0)20 7679 0522
ECRC, UCL Geography, [f] +44 (0)20 7679 0565
Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-sig-ecology
mailing list