2

I'm trying to create a GIF that loops with the animation package in R. But for some reason, even if I set the option loop=TRUE, the images I make only play once and then stop. I would like the GIF to keep playing indefinitely. Any tips?

install.packages("animation")
library(animation)

saveGIF({
  for (i in 1:10) plot(runif(10), ylim = 0:1)
},loop=TRUE,interval=0.2)
bobfet1
  • 1,603
  • 21
  • 22

1 Answers1

2

The following works for me. loop=TRUE is the default setting. Are you sure that the problem is not in your GIF viewer?

library(animation)

ani.options(
convert = shQuote('C:/Program Files (x86)/ImageMagick-6.8.1-Q16/convert.exe')
)

saveGIF(
{
  for (i in 1:10) plot(runif(10), ylim = 0:1)
},
movie.name = "test.gif", 
interval = 0.2, 
ani.width = 300, 
ani.height = 300,
outdir = getwd()
)

p.s. I'm guessing that your code works without the addition of the pointer to the convert.exe program since you are able to produce the .gif.

enter image description here

Marc in the box
  • 11,769
  • 4
  • 47
  • 97
  • 2
    Thanks. Including the `outdir` setting fixed my problem. What was happening was that R was generating the GIF to a temp folder, opening it up with Preview (OS X), and I was saving it from there. Apparently the step of saving the GIF from Preview instead of getting the output GIF was removing the loop behavior – bobfet1 Jul 11 '13 at 22:15
  • Yup, +1 for this comment. Setting the outdir() solved this problem for me to (I was having an identical issue with Preview removing the loop behavior). – Nate Mar 17 '14 at 14:41