Make an rstudio notebook inline animation that loops with gganimate

I like the default behavior of gganimate that makes a looping gif of my output, and would like to get that looping input to show up in the .Rmd file of an RStudio Notebook.

The following code, found here, comes close to what I want. However, unlike the gif, it only plays once and doesn't loop.
```{r, warning=FALSE, message=FALSE, results = FALSE}

library(ggplot2)
library(gganimate)

p <- ggplot(iris) +
  aes(Petal.Width, Petal.Length) +
  geom_point() +
  transition_states(Species,
                    transition_length = 2,
                    state_length = 1)
animate(p, renderer = ffmpeg_renderer(format = "webm"))

```

Furthermore, as per OP's unanswered comment on that site, I also get the same unwanted output to the console

Input #0, image2, from '/tmp/RtmpnXCx5F/df873f8219e/gganim_plot%4d.png':
Duration: 00:00:04.00, start: 0.000000, bitrate: N/A
Stream #0:0: Video: png, pal8(pc), 480x480, 25 fps, 25 tbr, 25 tbn, 25 tbc

and so on, even with the warnings, message and results set to false.

Any suggestions for how I can get this ffmpeg to loop, at least a few times, or better yet loop indefinately while keeping it inline in the .Rmd file? Suggestions for banishing the information about the rendering would be appreciated as well. Thanks.

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

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.