r - 有没有办法将 gganimate 对象保存为带有动画控制按钮的 HTML 页面?
问题描述
目标
使用开始/停止/暂停按钮控制动画。
问题
该包可以使用该函数animation
将创建的动画保存为 HTML 页面(此处为示例)。但它需要“ = 一个要评估的 R 表达式以创建图像序列”作为第一个参数。saveHTML()
expr
但是,gganimate
创建了一个gganim
对象,我似乎找不到可以将动画保存为带有按钮的 HTML 页面的功能。可用anim_save()
功能不另存为 HTML。有解决方法吗?
动画示例代码
如果您有任何想法,请分享。供您参考,我将其放在gganimate
其网站的代码下方。
library(ggplot2)
library(gganimate)
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot() +
# Here comes the gganimate code
transition_states(
gear,
transition_length = 2,
state_length = 1
) +
enter_fade() +
exit_shrink() +
ease_aes('sine-in-out')
解决方案
以下内容在 RStudio 中对我有用,尽管我没有看到“当前”列为设备的选项gganimate::animate
:
library(gganimate)
library(animation)
# name the above gganimate example as p
p <- ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot() +
transition_states(
gear,
transition_length = 2,
state_length = 1
) +
enter_fade() +
exit_shrink() +
ease_aes('sine-in-out')
# pass the animate command to saveHTML
saveHTML(animate(p,
nframes = 10, # fewer frames for simplification
device = "current"),
img.name = "gganimate_plot",
htmlfile = "gg.html")
我得到了一个带有动画控制按钮的 html 文件和一个带有 10 个 png 文件的图像文件夹。
推荐阅读
- ios - 我想在选择或取消选择时以字典格式发送参数应该在字典中添加或删除元素
- amazon-web-services - 如何从 aws 秘密管理器中检索 terraform 中的秘密
- discord.js - 如何在我的命令 discord.js 项目中获取 name/id/@
- python - 根据条件创建总和行
- php - ErrorException 从空值创建默认对象:尝试同时插入和更新
- clang - 使用 llvm 在循环向量器中收集和分散内部函数
- javascript - 无法修改函数内的全局变量
- java - 如何设置另一个窗口(不同的应用程序)的大小?
- flutter - 键盘显示时脚手架不再调整大小
- snakemake - InputFunctionException:解析时出现意外的 EOF