gifski: Gifski

Description Usage Arguments Examples

View source: R/gifski.R

Description

Gifski converts image frames to high quality GIF animations. Either provide input png files, or automatically render animated graphics from the R graphics device.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
gifski(
  png_files,
  gif_file = "animation.gif",
  width = 800,
  height = 600,
  delay = 1,
  loop = TRUE,
  progress = TRUE
)

save_gif(
  expr,
  gif_file = "animation.gif",
  width = 800,
  height = 600,
  delay = 1,
  loop = TRUE,
  progress = TRUE,
  ...
)

Arguments

png_files

vector of png files

gif_file

output gif file

width

gif width in pixels

height

gif height in pixel

delay

time to show each image in seconds

loop

if the gif should be repeated. Set to FALSE to only play once, or a number to indicate how many times to repeat after the first.

progress

print some verbose status output

expr

an R expression that creates graphics

...

other graphical parameters passed to png

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Manually convert png files to gif
png_path <- file.path(tempdir(), "frame%03d.png")
png(png_path)
par(ask = FALSE)
for(i in 1:10)
  plot(rnorm(i * 10), main = i)
dev.off()
png_files <- sprintf(png_path, 1:10)
gif_file <- tempfile(fileext = ".gif")
gifski(png_files, gif_file)
unlink(png_files)
utils::browseURL(gif_file)


# Example borrowed from gganimate
library(gapminder)
library(ggplot2)
makeplot <- function(){
  datalist <- split(gapminder, gapminder$year)
  lapply(datalist, function(data){
    p <- ggplot(data, aes(gdpPercap, lifeExp, size = pop, color = continent)) +
      scale_size("population", limits = range(gapminder$pop)) + geom_point() + ylim(20, 90) +
      scale_x_log10(limits = range(gapminder$gdpPercap)) + ggtitle(data$year) + theme_classic()
    print(p)
  })
}

# High Definition images:
gif_file <- file.path(tempdir(), 'gapminder.gif')
save_gif(makeplot(), gif_file, 1280, 720, res = 144)
utils::browseURL(gif_file)

Example output

png 
  2 

Frame 1 (10%)
Frame 2 (20%)
Frame 3 (30%)
Frame 4 (40%)
Frame 5 (50%)
Frame 6 (60%)
Frame 7 (70%)
Frame 8 (80%)
Frame 9 (90%)
Frame 10 (100%)
Finalizing encoding... done!
[1] "/work/tmp/tmp/RtmpqdB1s0/file294b734748d58.gif"
sh: 1: xdg-open: Permission denied

Frame 1 (8%)
Frame 2 (16%)
Frame 3 (25%)
Frame 4 (33%)
Frame 5 (41%)
Frame 6 (50%)
Frame 7 (58%)
Frame 8 (66%)
Frame 9 (75%)
Frame 10 (83%)
Frame 11 (91%)
Frame 12 (100%)
Finalizing encoding... done!
[1] "/work/tmp/tmp/RtmpqdB1s0/gapminder.gif"
sh: 1: xdg-open: Permission denied

gifski documentation built on May 2, 2021, 9:07 a.m.

Related to gifski in gifski...