use ggplot2 of rpy2 in jupyterlab

Issue #574 invalid
freakwill created an issue
import rpy2.robjects.lib.ggplot2 as ggplot2

mtcars = data(datasets).fetch('mtcars')['mtcars']

pp = ggplot2.ggplot(mtcars) + ggplot2.aes_string(x='wt', y='mpg', col='factor(cyl)') + \
     ggplot2.geom_point() 
#+ ggplot2.geom_smooth(ggplot2.aes_string(group = 'cyl'), method = 'lm')

from rpy2.ipython.ggplot import image_png
image_png(pp) # as suggested, I use the function

I tried to plot something with ggplot2 of rpy2 in jupyterlab, but no output no error.

I was suggested to use image_png, it works on colab, but fails on jupyterlab. What I got is a broken picture symbol, meaning a picture should be here but could not be found.

latest version of rpy2, in mac, python3

I also try the following complicated version.

grdevices = importr('grDevices')
# grdevices.png(file="file.png", width=512, height=512)

from rpy2.robjects.lib import grid
grid.activate()

import rpy2.robjects.lib.ggplot2 as ggplot2

mtcars = data(datasets).fetch('mtcars')['mtcars']

pp = ggplot2.ggplot(mtcars) + ggplot2.aes_string(x='wt', y='mpg', col='factor(cyl)') + \
     ggplot2.geom_point() 
#+ ggplot2.geom_smooth(ggplot2.aes_string(group = 'cyl'), method = 'lm')

from rpy2.ipython.ggplot import image_png
image_png(pp)

grdevices.dev_off()

Comments (14)

  1. Laurent Gautier

    This is the first report of that kind of issue, IIRC, which makes me think that this is either triggered by a very recent change in jupyterlab or in one of the R functions used... or this is something specific to your installation. If the latter, going over the steps for `image_png()` (see https://bitbucket.org/rpy2/rpy2/src/cd2445780383db4085849c079598eeb38f3577a5/rpy/ipython/ggplot.py#lines-32) and checking that results obtained for each step make sense would be helpful.

  2. freakwill reporter
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
        343             method = get_real_method(obj, self.print_method)
        344             if method is not None:
    --> 345                 return method()
        346             return None
        347         else:
    
    /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rpy2/robjects/vectors.py in _repr_html_(self, max_items)
        676         for e in self._iter_repr(max_items=max_items):
        677             if hasattr(e, '_repr_html_'):
    --> 678                 elements.append(e._repr_html_())
        679             else:
        680                 elements.append(e)
    
    /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rpy2/robjects/vectors.py in _repr_html_(self, max_items)
        682         names = list()
        683         if len(self) <= max_items:
    --> 684             names.extend(self.names)
        685         else:
        686             half_items = max_items // 2
    
    TypeError: 'NULLType' object is not iterable
    

    I replace the function `image_png` with its body, get the unexpected error. The error was caught in

    with grdevices.render_to_bytesio(grdevices.png,
    type="cairo-png",
    width=800,
    height=400,
    antialias="subpixel") as b:
    
  3. Laurent Gautier

    No follow-up and I can't reproduce. Closing. Reopen if additional information suggests that it should be reopened.

  4. Log in to comment