【问题标题】:Is there a way to save a gganimate object as a HTML page with animation control buttons?有没有办法将 gganimate 对象保存为带有动画控制按钮的 HTML 页面?
【发布时间】:2021-01-03 18:02:38
【问题描述】:

目标

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

问题

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

【问题讨论】:

    标签: r animation gganimate


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多