Create videos from saved plots in R

Viewed 332

I have generated a series of plot using loop and saved them as .png files in a folder. How can I write a code to loop through these plots (in .png format) to create a video.

I have used av_capture_graphics (package av) in past and can use it to generate video. But re-running the code to do all the analysis to regenerate all plots will take lot of time.

Thanks

1 Answers

Here is an example using the magick package

library(tidyverse)
library(magick)

### list all png files
png_files <- list.files(".",
                        pattern = "\\.png$",
                        recursive = FALSE,
                        all.files = FALSE,
                        full.names = TRUE)

### create a GIF file from all the plots
png_files %>%
  map(image_read) %>% # reads each path file
  image_join() %>% # joins image
  image_animate(fps = 1) %>% # animates
  image_write("All_plots.gif")

Check out other functions: image_write_video() and image_write_gif()

Related