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

标签 r r-markdown knitr gganimate

这是我尝试过的事情的列表。

---
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 --自包含 --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/资源/library/rmarkdown/rmd/lua/latex-div.lua 在资源路径中找不到文件 test_gif_files/figure-html/unnamed-chunk-1.webm 错误:pandoc 文档转换失败,错误 99 执行暂停

然后我按照这个方法,使用 gifski::save_gif 保存 gif,然后使用 include_graphics 在后续 block 中显示。 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 包,您不需要设置 block 选项animation.hook。这足够了:

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

关于r - 如何使用 R Markdown 在 html 输出中创建动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58625341/

相关文章:

r - 从长到宽的结构化 R Dataframe

r - 堆叠条形图显示 R 中的反向标签

rmarkdown 矩阵未按预期运行或在 rstudio 中看到

r - 将 RStudio 演示文稿 (.Rpres) 转换为 rmarkdown 演示文稿 (.Rmd)

r - 使用 knitr 和 xtable 在 LaTex 中交叉引用的问题

r - 连接线像r中的树

r - 试图告诉我的错误 `vec_assign()` : `value` should have been recycled to fit `x` . 是什么

r - 密码保护 Bookdown 文件

通过过滤代码块的名称来运行knitr中选定的代码块

r - 如何将 ggplot 旋转为横向?