Created
September 4, 2017 14:17
-
-
Save jeroen/1af6098a7d24045ec2b08f7aa795c55a to your computer and use it in GitHub Desktop.
animation benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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