如何使用 R Markdown 在 html 输出中创建动画
How to create animation in html output using R Markdown
这是我尝试过的事情的列表。
---
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 ->
执行暂停
尽管如此,我使用了此处提到的 dev = 'png' https://yihui.name/en/2018/08/gifski-knitr/,但我无法使其工作。
然后我尝试使用 FFmpeg 渲染器
---
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
libav 格式 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' --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-过滤器/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
---
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
。够了:
```{r, dev='png', interval=0.2}
library(gganimate)
ggplot(airquality, aes(Day, Temp, group = Month)) +
geom_line() +
transition_reveal(Month)
```
这是我尝试过的事情的列表。
---
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 -> 执行暂停
尽管如此,我使用了此处提到的 dev = 'png' https://yihui.name/en/2018/08/gifski-knitr/,但我无法使其工作。
然后我尝试使用 FFmpeg 渲染器
---
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 libav 格式 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' --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-过滤器/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
---
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
。够了:
```{r, dev='png', interval=0.2}
library(gganimate)
ggplot(airquality, aes(Day, Temp, group = Month)) +
geom_line() +
transition_reveal(Month)
```