Assemble a directory of PNG frames into a GIF
make_gif.RdCollects every .png file in frame_dir (sorted alphabetically), builds
a per-frame delay vector with distinct hold times for the first and last
frames, and writes the result to a GIF via gifski::gifski().
Usage
make_gif(
frame_dir,
output,
width,
height,
first_duration = 1,
frame_duration = 0.4,
last_duration = 4
)Arguments
- frame_dir
Path to a directory containing numbered
.pngframe files.- output
Path for the output
.giffile (e.g."animation.gif").- width
GIF width in pixels.
- height
GIF height in pixels.
- first_duration
Seconds to hold the first frame (default
1).- frame_duration
Seconds to show each middle frame (default
0.4).- last_duration
Seconds to hold the last frame (default
4).
Details
The typical workflow is:
Create a temporary directory.
Loop over your data, saving one plot per frame with
ggplot2::ggsave().Call
make_gif()to assemble the frames.
Examples
if (FALSE) { # \dontrun{
frame_dir <- file.path(tempdir(), "my-frames")
dir.create(frame_dir, showWarnings = FALSE)
for (i in 1:5) {
p <- ggplot2::ggplot() + ggplot2::ggtitle(paste("Frame", i))
ggplot2::ggsave(
filename = file.path(frame_dir, sprintf("%04d.png", i)),
plot = p,
width = 800,
height = 400,
units = "px"
)
}
make_gif(frame_dir, "animation.gif", width = 800, height = 400)
} # }