Search code examples
pythonrjupyterrpy2pyper

Python and R: how do you show a plot with Pyper in a Jupyter notebook?


I am creating a report using Jupyter. Most of my code is in Python, but I need to use some R functionalities.

I use a package called Pyper to call R in Python. It works well, but I could not figure out how to display a plot made in R (via Pyper) in the Jupiter notebook. Everything seems to work well, but Jupyter does not show the plot.

Here's my test code:

In [17]: from pyper import *
         r = R()
         r("library(TSA)")
         r("data(star)")
         r("periodogram(star)")

And this is the output from Jupyter (without the periodogram plot):

Out[17]: 'try({periodogram(star)})\n'

Solution

  • I have found a workaround if anyone is using Pyper and wants to add a plot to Jupyter:

    from pyper import *
    r = R()
    r("library(TSA)")
    r("data(star)") 
    
    # Save the figure
    r("png('rplot.png');periodogram(star);dev.off()")
    
    
    # Upload the figure to Jupyter
    from IPython.display import Image
    Image("rplot.png",width=600,height=400)