Search code examples
rrstudiogifmp4gganimate

How to open animated plots (gif/mp4) in RStudio?


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)


Solution

  • 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 ;)