ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:58:47 24 4
gpt4 key购买 nike

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

---
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 .webmffmpeg 版本 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.100test_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/

24 4 0
文章推荐: pandas - 从 python 中的 sas 数据集中的 x 行读取 n 行
文章推荐: amazon-web-services - 刷新 EC2 实例标签失败 : SharedCredsLoad
文章推荐: flatpak - 什么是 flatpak 运行时?
文章推荐: ruby - Rspec 字符串截断的最佳解决方法是什么?
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com