有没有一种方法可以将gganimate对象保存为带有动画控制按钮的HTML页面?

3

目标

使用开始/停止/暂停按钮控制动画。

问题

animation包可以使用saveHTML()函数将创建的动画保存为HTML页面 (example here)。但它需要以"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')
1个回答

2

以下内容适用于我在RStudio中,尽管我没有在gganimate::animate的设备选项中看到"current"作为一个选项:

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")

我有一个包含动画控制按钮和一个包含10个png文件的图像文件夹的html文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接