如何使用R Markdown在html输出中创建动画

How to create animation in html output using R Markdown

这是我尝试过的事情清单。

1
2
3
4
5
6
7
8
9
10
11
12
---
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)
```

错误:
从第8-12行退出(test_gif.Rmd)
hook_animation(options)(x,options)中的错误:
要使用hook_gifski(),代码块必须生成" png"图像而不是" gif"。
调用:... hook_plot-> hook_plot_md_base-> hook_plot_html->
执行停止

尽管如此,我还是使用了https://yihui.name/en/2018/08/gifski-knitr/所述的dev ='png',但我无法使其工作。

然后我尝试使用FFmpeg渲染器

1
2
3
4
5
6
7
8
9
10
11
12
13
---
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)
```

错误:正在执行: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版本4.2.1版权所有(c)2000-2019 FFmpeg开发人员
使用Apple LLVM版本10.0.0(clang-1000.11.45.5)构建
配置:--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:没有这样的文件或目录
| ....................................................... ................ | 100%
没有R代码的普通文本

输出文件: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'--header 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
在资源路径中找不到文件test_gif_files / figure-html / unnamed-chunk-1.webm
错误:pandoc文档转换失败,错误为99
执行停止

然后我遵循此方法,使用gifski :: save_gif保存gif,然后使用include_graphics在后续块中显示。
https://community.rstudio.com/t/make-an-rstudio-notebook-inline-animation-that-loops-with-gganimate/27489/2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
---
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
```

错误(相同):
从第18-19行退出(test_gif.Rmd)
hook_animation(options)(x,options)中的错误:
要使用hook_gifski(),代码块必须生成" png"图像而不是" gif"。
调用:... hook_plot-> hook_plot_md_base-> hook_plot_html->
执行停止

我的最终目标是在最终的html文档中创建动画,而不在其间产生任何临时文件。即使有其他替代方法可以完成这项工作,我也很高兴。


使用gganimate软件包,您无需设置块选项animation.hook。这足够了:

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