Skip to content

Instantly share code, notes, and snippets.

@jeroen
Created September 4, 2017 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroen/1af6098a7d24045ec2b08f7aa795c55a to your computer and use it in GitHub Desktop.
Save jeroen/1af6098a7d24045ec2b08f7aa795c55a to your computer and use it in GitHub Desktop.
animation benchmark
create_plots <- function(n = 100){
myplot <- ggplot2::qplot(speed, dist, data = cars, geom = c("smooth", "point"))
filepath <- file.path(tempdir(), "plot%03d.png")
png(filepath, width = 800, height = 600)
range <- seq_len(n)
for(i in range){
print(myplot + ggplot2::ggtitle(paste("Frame", i)))
}
dev.off()
sprintf(filepath, range)
}
magick.convert <- function(x, output = "animation.gif"){
image_read(x) %>%
image_animate(fps = 2, dispose = 'background') %>%
image_write(output)
invisible()
}
# Join with 'magick' package
library(magick)
library(animation)
library(microbenchmark)
png_files <- create_plots(100)
microbenchmark::microbenchmark(
magick = magick.convert(png_files, output = 'animation1.gif'),
cmdline = im.convert(png_files, output = 'animation2.gif'),
times = 5
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment