问题
I'm using RPy2 to make some plots. The plot shows up but the X11 window immediately disappears.
All I'm typing is the following, where CCFS is a data matrix
import rpy2.robjects as robjects
r = robjects.r
pca = r.princomp(CCFS)
r.plot(pca,main="Eigenvalues")
r.biplot(pca,main="biplot")
r['dev.off']() #*EDIT* the problem persists even if I remove this line.
Am I failing to include something? I know that there is something to plot because princomp returns a ListVector that isn't null.
回答1:
Add
input() # for Python3
# raw_input() # for Python2
to the end of the script to prevent the program from ending until the user presses Enter.
回答2:
As Zack pointed it out calling dev.off()
on interactive graphical devices will just cause the device to close (and what it displays to disappear). On the other hand, closing non-interactive devices is mostly required before the plot can be checked (See rpy2's documentation on graphical devices).
I am guessing that your Python code is called as a standalone program / script rather than part of an interactive Python session. In other words I am suspecting that you are having something like python myscript.py
in a shell (the content of myscript.py
being your code above); when the script ends the embedded R is obviously terminated, taking with it the interactive plot. This is likely also what Zack is thinking. Try python -i myscript.py
, and then once in the Python console import sys; sys.exit(0)
to see that exiting Python means closing R interactive devices.
来源:https://stackoverflow.com/questions/12655771/plots-made-with-rpy-sent-to-x11-suddenly-close