Plots made with rpy sent to X11 suddenly close?

2019-02-20 05:25发布

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.

2条回答
小情绪 Triste *
2楼-- · 2019-02-20 06:02

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-02-20 06:10

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.

查看更多
登录 后发表回答