【问题标题】:How to open animated plots (gif/mp4) in RStudio?如何在 RStudio 中打开动画图(gif/mp4)?
【发布时间】:2022-01-15 01:26:59
【问题描述】:

我在受限研究环境(经过若干修改的 Windows 10 VM)中使用带有 {gganimate} 软件包的 RStudio 来创建 .gif.mp4 格式的动画图 - 当它们在 RStudio 中呈现时,它们会显示很好,但是如果我使用 anim_save() 保存文件,我无法在提供的 Windows 图像查看器或 Web 浏览器(或.mp4 的情况下的媒体播放器)中打开生成的文件 - 它总是说文件已损坏或无法显示。

将这些文件从受限环境中导出需要一个漫长的过程,所以我想检查一下它们是否真的损坏了,或者由于某种原因无法在这个特定的操作系统中显示。 RStudio 可以打开/显示.gif 文件或视频吗?注意:我知道如何使用print()/plot() 方法显示动画 - 这是关于在导出后打开/显示外部动画文件。

生成动画情节并保存为.gif/.mp4的示例代码如下:

library(ggplot2)
library(gganimate)  # package {av} also required to save as mp4

animated_plot <- 
  ggplot(mtcars, aes(x = wt, y = hp, colour = as.factor(cyl))) +
  geom_point() +
  transition_states(cyl, transition_length = 3, state_length = 1) +
  enter_fade() +
  exit_fade() +
  labs(title = "Cyl: {closest_state}")

## save as gif
anim_save(
  filename = "animation.gif", 
  animation = animate(animated_plot)
  )

## save as mp4
anim_save(
  filename = "animation.mp4", 
  animation = animate(animated_plot, 
                      renderer = av_renderer())
)

(我的备用计划是使用file_renderer() 将单个帧导出为图像并稍后对其进行动画处理,例如Convert multiple png to gif as an animation in R

【问题讨论】:

  • 你能添加一个代码,生成一个简单的动画图(gif和mp4)吗?我想尝试一下。
  • @manro,我添加了一些示例代码!
  • 谢谢,我现在正在努力;)
  • 它有效,是的。我稍微更正了你的代码。我添加一个答案,请稍等。

标签: r rstudio gif mp4 gganimate


【解决方案1】:

我稍微更正了您的代码,现在它可以正常工作了。

library(ggplot2)
library(gganimate)  # package {av} also required to save as mp4

animated_plot <- 
    ggplot(mtcars, aes(x = wt, y = hp, colour = as.factor(cyl))) +
    geom_point() +
    transition_states(cyl, transition_length = 3, state_length = 1) +
    enter_fade() +
    exit_fade() +
    labs(title = "Cyl: {closest_state}")

## save as gif
animation = animate(animated_plot)
anim_save(
    filename = "animation.gif")
        
## save as mp4
animation1 = animate(animated_plot, 
                    renderer = av_renderer())
anim_save(
    filename = "animation.mp4")

animation1 #to display a video in the RStudio's viewer
animation  #to display a gif in the RStudio's viewer

一个例子:


补充:

如果需要,您可以在内部 RStudio 浏览器中打开 gif 或 mp4:

制作一个简单的 Rmarkdown 文件并编织成 html:

---
title: "Untitled"
output:
  html_document
---

<img src="animation.gif"/>

<video controls autoplay>
   <source src="animation.mp4" type="video/mp4">
</video>

另一个补充。

你应该安装library(rstudioapi)

查看此链接后: https://rstudio.github.io/rstudioapi/reference/viewer.html

制作一个带有 img / video 标签的简单 html 文件,例如animation.html 并通过查看器打开:

<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>blah</title>
</head>
<body>
  
<img src="animation.gif"/>

<video controls autoplay>
   <source src="animation.mp4" type="video/mp4">
   
</video>
</body>
</html>
rstudioapi::viewer("animation.html")

后备:) library(ricomisc) 带有命令:rstudio_viewer("xxx.html")

祝你好运;)

【讨论】:

  • 也许我的问题不够清楚:我正在尝试打开现有的 gif/mp4 并将其显示在 RStudio 中,我已经编辑了问题以澄清这一点。
  • @jsavn ... 查看器窗格中 RStudio 中的现有 gif,是吗?或任何地方,例如在 RmarkdownN 中?
  • 嘿,这是个好主意,我没想过渲染一个包含.gif.Rmd 文件!虽然理想情况下我想在 RStudio 查看器窗格中显示现有的 gif 或视频,是的。
  • @jsavn 我会试着想想 :) 稍后再告诉你。
  • @jsavn 我找到了另一个答案。如果我对你有帮助,你可以接受我的回答;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 2014-12-24
  • 2016-10-18
  • 1970-01-01
  • 1970-01-01
  • 2013-04-26
  • 1970-01-01
相关资源
最近更新 更多