2

I'm using RStudio with the {gganimate} package in a restricted research environment (a Windows 10 VM with several modifications) to create animated graphs in .gif and .mp4 formats - when they are rendered within RStudio they display fine, but if I save the files using anim_save() I can't open the resulting files in either the provided Windows Image Viewer or web browser (or the media player in case of .mp4) - it always says the file is broken or cannot be displayed.

There is a lengthy process involved in exporting these files out of the restricted environment, so I'd like to check if they are actually broken, or just cannot be displayed in this particular OS for whatever reason. Can RStudio open/display .gif files or videos? Note: I know how to display an animation using print()/plot() methods - this is about opening/displaying an external animated file after it's been exported.

Example code to generate animated plot and save as .gif/.mp4 below:

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

(My backup plan is to use file_renderer() to export the individual frames as images and animate them later, as in e.g. Convert multiple png to gif as an animation in R)

jsavn
  • 701
  • 1
  • 8
  • 17

1 Answers1

2

I corrected your code a little, now it works right.

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

An example:

enter image description here


An addition:

If you want, you can open your gifs or mp4 in the internal RStudio browser:

Make a simple Rmarkdown-file and knit to html:

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

<img src="animation.gif"/>

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

An another addition.

You should install library(rstudioapi)

After look this link: https://rstudio.github.io/rstudioapi/reference/viewer.html

Make a simple html file with img / video tag, e.g. animation.html and open via viewer:

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

A fallback :) library(ricomisc) with command: rstudio_viewer("xxx.html")

Good luck ;)

jsavn
  • 701
  • 1
  • 8
  • 17
manro
  • 3,529
  • 2
  • 9
  • 22
  • Perhaps I wasn't clear enough in my question: I'm trying to open an existing gif/mp4 and display it inside RStudio, and I've edited the question to clarify that. – jsavn Dec 10 '21 at 14:46
  • @jsavn ... existing gif inside RStudio in the viewer pane, yes? Or anywhere, f.e. in RmarkdowN? – manro Dec 10 '21 at 15:08
  • Hey, that's a good idea, I hadn't thought of rendering an `.Rmd` file that includes a `.gif`! Though ideally I'd like to display an existing gif or video inside the RStudio viewer pane, yes. – jsavn Dec 10 '21 at 15:41
  • @jsavn I'll try to think :) Say to you later. – manro Dec 10 '21 at 15:47
  • @jsavn I found an additional answer. If I helped to you, you can accept my answer ;) – manro Dec 10 '21 at 17:13
  • Thanks, the idea with knitting a markdown file was great, as is the html file with the animation included... I've added a minimal html file to your answer to illustrate how! – jsavn Dec 13 '21 at 11:21
  • @jsavn Good luck. It was a nice question ;) – manro Dec 13 '21 at 13:11