rpy2 and inline rendering in IPython Notebook

Issue #221 resolved
ijstokes created an issue

I am trying to get rpy2 inline rendering in IPython Notebook to work, however I keep getting hit with the "X11 is not available" error. I've created a PR with some modifications to "rmagic.py", but I feel like there may be something else obvious I am missing. I saw the other issue related to this, and it seemed like the problem had been solved ~4 months ago, but I am still having difficulty.

Is this expected to work?

Comments (6)

  1. Laurent Gautier

    I'd expect the notebook to work even in the absence of X11.

    Do you have a (self-contained) snippet of code to demonstrate the issue ? (this is quite a great help for us when wanting to close an issue).

  2. ijstokes reporter

    OK, it looks like I've resolved the X11 issue in the course of trying to resolve this, so now there are no errors, but there is still no inline output. An example notebook demonstrating this can be found here:

    https://www.wakari.io/sharing/bundle/ijstokes/rpy2%20tests

    This also shows: R version, rpy2 version, regular matplotlib inline plotting (working), R "capabilities()", ggsave.

    In fact, I am now suspecting that this is purely down to my own misunderstanding about how to use rpy2 and/or the %%R rmagic in order to generate plots inline. As best I can tell, the R kernel thinks it is non-interactive (dev.interactive() returns FALSE), and this may be the cause. I don't know how to get it into interactive mode.

    If you want to experiment with this, you can do so on Wakari (http://wakari.io) -- exactly the environment I am using will be available when you login, and rpy2 can be updated/modified if necessary (will only affect your local account).

  3. Laurent Gautier

    In the absence of X11, R is opening a PDF plotting device (which is a file, named Rplot.pdf by default IIRC) and the rmagic leave it with that device open (dev.list() will show this).

    When doing %Rdevice png before

    %%R -i pp
    print(pp)
    

    ...nothing happens.

    Changing the "R magic" to the one in rpy2 your code example is using the one in ipython (and is deprecated) to

    %load_ext rpy2.ipython
    

    Gives a slightly different result:

    %%R -i pp
    print(pp)
    

    now returns a string with the name of a PNG file.

    but giving a hint that an image is expected, for example:

    %%R -i pp -w 400
    print(pp)
    

    leads to an error and a traceback:

    /opt/anaconda/envs/np18py27-1.9/lib/python2.7/site-packages/rpy2/ipython/rmagic.py in <genexpr>((a, v))
        383             if self.device == 'png':
        384                 # Note: that %% is to pass into R for interpolation there
    --> 385                 print 'ro.r.png("%s/Rplots%%03d.png"' % tmpd_fix_slashes, ",".join("{a}={v|r}".format(a=a,v=v) for a, v in argdict.iteritems()), ')'
        386                 #ro.r.png("%s/Rplots%%03d.png" % tmpd_fix_slashes, **argdict)
        387             elif self.device == 'svg':
    
    KeyError: 'v|r'
    

    This is looking like a problem in rpy2...

  4. Laurent Gautier

    Are you using rpy2 release 2.4.3 tag ? (hg update -r 24fa146c2d8d). The code in rmagic appear to differ and key error unlikely to happen there.

  5. Log in to comment