rpy2 / rpy2/rpy2

Support gganimate - allow embedding gifs in notebooks

Open
#641 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
704
Forks
83
Avg merge
3d 4h
Merged PRs (30d)
4

Description

gganimate is a popular ggplot extension allowing to add animations to plots. It can be installed from CRAN with:

install.packages('gganimate')

One needs to install gifski to render actual gifs rather than a sequence of PNG images:

install.packages('gifski')

However, the resulting animations do not work with rpy2 straight away. Here is an example:

%load_ext rpy2.ipython
%%R
library(ggplot2)
library(gganimate)

animated_plot = ggplot(mtcars, aes(factor(cyl), mpg)) + 
  geom_boxplot() + 
  transition_states(
    gear,
    transition_length = 2,
    state_length = 1
  )
animated_plot

results in an error:

RRuntimeError: Error in check.options(new, name.opt = ".X11.Options", envir = .X11env) : 
  invalid argument name ‘useS4’ in 'device(files[i], ...)'
Calls: <Anonymous> ... animate.gganim -> do.call -> <Anonymous> -> device -> check.options

I am able to work around this with a two-step solution:

First, generate the animation in R:

%%R -o animation
# this can take a minute or two
animation = gganimate::animate(
    animated_plot,
    renderer=gifski_renderer(),
    width=350, height=300, res=90
)

Secondly, embed the file animation as a gif picture:

from IPython.display import Image, display

def embed_gif(path):
    with open(path, 'rb') as f:
        display(Image(data=f.read(), format='gif'))

embed_gif(animation[0])

image

While skipping the second step and returning the animation is also possible...

%%R
animation

...it leads to the system image-browser popping up, so it would not work on remote deployments of Jupyter (e.g. JupyterHub).

Screenshot from 2020-02-12 17-40-31

PS. to support relevance of this issue, I will note someone previously placed a misdirected question about the possibility of using gganimate from Python: https://github.com/thomasp85/gganimate/issues/291

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the rpy2 IPython extension entry point used by %load_ext rpy2.ipython and the %%R result-display path. Reproduce the gganimate example in a notebook, then determine how returned animations can render inline as GIFs without opening a system image browser; done means the animation displays correctly on remote Jupyter deployments.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter-notebook, python, r
Domain
data-visualization
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.