I’m learning to use rpy2 in Jupyter notebook. I’m having troubles with the plotting. When I use this example from the rpy2 docs for interactive work:
from rpy2.interactive import process_revents from rpy2.robjects.packages import importr from rpy2.robjects.vectors import IntVector process_revents.start() graphics = importr("graphics") graphics.barplot(IntVector((1,3,2,5,4)), ylab="Value")
Jupyter opens a new window with the plot. The window “title” reads: R Graphics: Device 2 (ACTIVE) (Not Responding). My Jupyter kernel is active. When I try to close the window with the plot, windows claims that python.exe is not responsing and if I force close then the jupyter kernel restarts.
First: How can I make rpy2 plot inline? Second: If inline plotting is not possible, how to get the plot in a window without python.exe becoming unresponsive?
Advertisement
Answer
It seems that this is the answer to your question: https://bitbucket.org/rpy2/rpy2/issues/330/ipython-plotting-wrapper
with rpy2.robjects.lib.grdevices.render_to_bytesio(grdevices.png, width=1024, height=896, res=150) as img: graphics.barplot(IntVector((1,3,2,5,4)), ylab="Value") IPython.display.display(IPython.display.Image(data=img.getvalue(), format='png', embed=True))