Mohan Govindasamy
Mohan Govindasamy

Reputation: 906

How to create animation in html output using R Markdown

Here is the list of things I've tried.

---
title: "test_gif"
output: html_document
---


``` {r, animation.hook='gifski', dev='png', interval=0.2}
library(gganimate)
ggplot(airquality, aes(Day, Temp, group = Month)) + 
  geom_line() + 
  transition_reveal(Month)
```

Error: Quitting from lines 8-12 (test_gif.Rmd) Error in hook_animation(options)(x, options) : To use hook_gifski(), the code chunk must generate 'png' images instead of 'gif'. Calls: ... hook_plot -> hook_plot_md_base -> hook_plot_html -> Execution halted

Even though, I used dev = 'png' as mentioned here https://yihui.name/en/2018/08/gifski-knitr/, I couldn't able to make it work.

Then I've tried using FFmpeg renderer

---
title: "test_gif"
output: html_document
---


```{r, animation.hook='ffmpeg', interval=0.2}
library(gganimate)
ggplot(airquality, aes(Day, Temp, group = Month)) + 
  geom_line() + 
  transition_reveal(Month) -> p
animate(p)
```

Error: executing: ffmpeg -y -r 5 -i test_gif_files/figure-html/unnamed-chunk-1-%d.gif -b:v 1M -crf 10 test_gif_files/figure-html/unnamed-chunk-1.webm ffmpeg version 4.2.1 Copyright (c) 2000-2019 the FFmpeg developers built with Apple LLVM version 10.0.0 (clang-1000.11.45.5) configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.1 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr libavutil 56. 31.100 / 56. 31.100 libavcodec 58. 54.100 / 58. 54.100 libavformat 58. 29.100 / 58. 29.100 libavdevice 58. 8.100 / 58. 8.100 libavfilter 7. 57.100 / 7. 57.100 libavresample 4. 0. 0 / 4. 0. 0 libswscale 5. 5.100 / 5. 5.100 libswresample 3. 5.100 / 3. 5.100 libpostproc 55. 5.100 / 55. 5.100 test_gif_files/figure-html/unnamed-chunk-1-%d.gif: No such file or directory |.................................................................| 100% ordinary text without R code

output file: test_gif.knit.md

/usr/local/bin/pandoc +RTS -K512m -RTS test_gif.utf8.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash+smart --output test_gif.html --email-obfuscation none --self-contained --standalone --section-divs --template /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rmarkdown/rmd/h/default.html --no-highlight --variable highlightjs=1 --variable 'theme:bootstrap' --include-in-header /var/folders/pv/cs874rmn7dj9n08xdyc7s3nm0000gn/T//RtmpOqkC3V/rmarkdown-str28173033d049.html --mathjax --variable 'mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' --lua-filter /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rmarkdown/rmd/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rmarkdown/rmd/lua/latex-div.lua File test_gif_files/figure-html/unnamed-chunk-1.webm not found in resource path Error: pandoc document conversion failed with error 99 Execution halted

Then I followed this method, to save the gif using gifski::save_gif and then display in a subsequent chunk using include_graphics. https://community.rstudio.com/t/make-an-rstudio-notebook-inline-animation-that-loops-with-gganimate/27489/2

---
title: "test_gif"
output: html_document
---


```{r}
library(gganimate)
library(gifski)
ggplot(airquality, aes(Day, Temp, group = Month)) + 
  geom_line() + 
  transition_reveal(Month) -> p
animate(p)
```

```{r, animation.hook='gifski', interval = 0.2}
p
```

Error(same): Quitting from lines 18-19 (test_gif.Rmd) Error in hook_animation(options)(x, options) : To use hook_gifski(), the code chunk must generate 'png' images instead of 'gif'. Calls: ... hook_plot -> hook_plot_md_base -> hook_plot_html -> Execution halted

My end goal is to create animation in the final html document without producing any temporary files in-between. I would be happy even if there is any other alternative method to make this work.

Upvotes: 4

Views: 2053

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30104

With the gganimate package, you don't need to set the chunk option animation.hook. This is enough:

```{r, dev='png', interval=0.2}
library(gganimate)
ggplot(airquality, aes(Day, Temp, group = Month)) + 
  geom_line() + 
  transition_reveal(Month)
```

Upvotes: 5

Related Questions