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

问题描述

目标

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

问题

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文件的图像文件夹。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...