Make an rstudio notebook inline animation that loops with gganimate

I have experimented a lot with animations in RStudio. The easiest way I found to have animations show both in the RStudio IDE and in a knit document is to save the animated gif with gifski::save_gif and then display in a subsequent chunk using include_graphics. Please take a look at this example .Rmd file I pasted below. (expanded from the example I found here: Create GIFs with gifski in knitr Documents - Yihui Xie | 谢益辉) Many thanks to @yihui and the RStudio folks for all that they've done with knitr.

This doesn't exactly answer your question because it uses gifski instead of gganimate. Sorry about that, but I thought it might be useful.

Also, I haven't found a way to get animations to show in an html_notebook. I always have to knit to html_document to have it display animations. If the developers are reading this, is it possible for notebooks to display animations? :slight_smile:

Sorry about the formatting of the code; you'll need to add the triple ` to close each chunk.

---
title: "animated gif test"
output: html_document
---

```{r}
library(gifski)
library(knitr)
Pac.Man <- function() for (i in 1:2) {pie(c(i %% 2, 6), col = c('red', 'yellow'), labels = NA)}

```{r, animation.hook='gifski', interval = 0.2}
Pac.Man()  
# animation will show when knitting to html_document.  
# no animation in RStudio or html_notebook html file.

```{r message = FALSE}
invisible(save_gif(Pac.Man(), "namco.gif", delay = 0.2, progress = FALSE))  
# must have parens () in function call

```{r}
include_graphics("namco.gif")  
# animation will show in RStudio, and in a knit html_document
# no animation in html_notebook.
3 Likes