Last month Jeroen announced the gifski package, which looked pretty cool to me. While thinking about improving the gganimate support in knitr (per Thomas’s request), I learned a little more about gifski. The usage seems to be quite straightforward to me, so I wrote a hook function in knitr today to support this package, which means you can easily create a GIF animation from a code chunk now if you have installed gifski, and at least version 1.20.14 of knitr:
install.packages('gifski')
if (packageVersion('knitr') < '1.20.14') {
remotes::install_github('yihui/knitr')
}
Basic usage
To make use of gifski, you may set the chunk option animation.hook='gifski'. Below is an R Markdown example:
```{r, animation.hook='gifski'}
for (i in 1:2) {
pie(c(i %% 2, 6), col = c('red', 'yellow'), labels = NA)
}
```
This will produce the “Pac man” below (assuming the output format is HTML):

Customization
The main things you may want to tweak for the GIF animation are perhaps the dimension (controlled by fig.width and fig.height, or fig.dim), and interval (the delay between frames), e.g.,
```{r, animation.hook='gifski', interval=0.2, fig.width=4}
for (i in 1:2) {
pie(c(i %% 2, 6), col = c('red', 'yellow'), labels = NA)
}
```
There are other options that you can customize, but I guess you probably don’t care (e.g., dpi). Note that the graphical device must produce PNG images since gifski only supports PNG at the moment, which means the chunk option dev must be a PNG device (e.g., 'png' or 'CairoPNG').
FFmpeg also works
You can also use FFmpeg to create a GIF animation, e.g.,
```{r, animation.hook='ffmpeg', ffmpeg.format='gif', dev='jpeg'}
for (i in 1:2) {
pie(c(i %% 2, 6), col = c('red', 'yellow'), labels = NA)
}
```
Without setting ffmpeg.format='gif', you will get a video instead (the default format is WebM). Gifski claims to be “the fastest GIF encoder in the universe”, and the package gifski does not have system dependencies (you don’t need to install external software like FFmpeg), so if you want to create a GIF, it is probably the best way to go.
Please test it yourself
Please help me test the development version of knitr, and I’ll appreciate your feedback. Many thanks to the R ninja Jeroen Ooms for the gifski package! For gganimate users, I guess a tighter integration between knitr and gganimate should be coming soon. Please stay tuned.
Donate
As a freelancer (currently working as a contractor) and a dad of three kids, I truly appreciate your donation to support my writing and open-source software development! Your contribution helps me cope with financial uncertainty better, so I can spend more time on producing high-quality content and software. You can make a donation through methods below.
-
Venmo:
@yihui_xie, or Zelle:xie@yihui.name -
Paypal
-
If you have a Paypal account, you can follow the link https://paypal.me/YihuiXie or find me on Paypal via my email
xie@yihui.name. Please choose the payment type as “Family and Friends” (instead of “Goods and Services”) to avoid extra fees. -
If you don’t have Paypal, you may donate through this link via your debit or credit card. Paypal will charge a fee on my side.
-
-
Other ways:
WeChat Pay (微信支付:谢益辉) Alipay (支付宝:谢益辉) 

When sending money, please be sure to add a note “gift” or “donation” if possible, so it won’t be treated as my taxable income but a genuine gift. Needless to say, donation is completely voluntary and I appreciate any amount you can give.
Please feel free to email me if you prefer a different way to give. Thank you very much!
I’ll give back a significant portion of the donations to the open-source community and charities. For the record, I received about $30,000 in total (before tax) in 2024-25, and gave back about $15,000 (after tax).