首页 > 解决方案 > 有没有办法将 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')

标签: ranimationgganimate

解决方案


以下内容在 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 文件的图像文件夹。


推荐阅读