有没有办法将 gganimate 对象保存为带有动画控制按钮的 HTML 页面?
Is there a way to save a gganimate object as a HTML page with animation control buttons?
目标
使用 start/stop/pause 个按钮控制动画。
问题
animation
包可以使用saveHTML()
功能(example here)将创建的动画保存为HTML页面。但它需要“expr
= 一个要计算的 R 表达式以创建图像序列”作为第一个参数。
但是,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 文件的图像文件夹。
目标
使用 start/stop/pause 个按钮控制动画。
问题
animation
包可以使用saveHTML()
功能(example here)将创建的动画保存为HTML页面。但它需要“expr
= 一个要计算的 R 表达式以创建图像序列”作为第一个参数。
但是,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 文件的图像文件夹。