yihui / animation

A gallery of animations in statistics and utilities to create animations
https://yihui.org/animation/
205 stars 59 forks source link

too-small interval gives puzzling error #123

Open MichaelChirico opened 4 years ago

MichaelChirico commented 4 years ago

I was trying to plot many frames and target the output to be a certain length (5 seconds):

nn = 720L
saveGIF({for (ii in 1:nn) plot(1:10)}, interval = 5/nn)

This then failed:

Error in if (100%%fps) stop("argument 'fps' must be a factor of 100") : 
  argument is not interpretable as logical

Quite strange. However, the stack trace gives a lot clearer understanding:

4: magick::image_animate(img, loop = loop, fps = 100/as.integer(interval * 
       100), dispose = dispose)
3: magick.convert(files = files, output = output, loop = loop, interval = interval, 
       dispose = dispose)
2: im.convert(img.files, output = path.expand(movie.name), convert = convert, 
       cmd.fun = cmd.fun, clean = clean, extra.opts = extra.opts)
1: saveGIF({
       for (ii in seq_along(grd)) {
           t = grd[ii]
           par(oma = c(0.1, 0.1, 0.1, 0.1))
           plot(NA, xlim = c(-e, e), ylim = c(-e, e), asp = 1, axes = FALSE, 
               xlab = "", ylab = "")
           lines(a * cos(grd), a * sin(grd), lty = 2L)
           x0 = a * cos(t)
           y0 = a * sin(t)
           segments(0, 0, x0, y0)
           lines(x0 + b * cos(grd), y0 + b * sin(grd), lty = 3L)
           points(a * cos(t) - b * sin(t), a * sin(t) + b * cos(t), 
               pch = 19L, col = "red")
           segments(x0, y0, x0 + b * cos(t + pi/2), y0 + b * sin(t + 
               pi/2))
           lines(r * cos(grd[1:ii] - t0), r * sin(grd[1:ii] - t0))
       }
   }, interval = 5/nn)

In particular:

100/as.integer(interval * 100)

Will give Inf since interval < 1/100 so as.integer will truncate to 0.

It would be helpful if this were pointed out sooner

yulijia commented 4 years ago

Hi,

This problem is introduced by the Magick package. In magick::image_animate function, fps must be a factor of 100, https://github.com/ropensci/magick/blob/master/R/animation.R#L37 I don't know why the author set this as default, but in animation package, you always have another choice.

You can avoid this problem by using the ImageMagick convert function. First of all, please make sure you installed ImageMagick convert function in your local machine (some special tips: http://www.imagemagick.org/discourse-server/viewtopic.php?t=29582). Then run the code below.

library(animation)
nn = 720L
saveGIF({for (ii in 1:nn) plot(runif(10), ylim = 0:1)}, 
interval = 5/nn,
convert="convert")