简体   繁体   English

如何在 Rpy2 中等待?

[英]How to do hold on in Rpy2?

我已经使用 rpy2 绘制了一个图表。我正在使用 Eclipse。当我运行代码时,情节只会闪烁 1 秒然后消失。如何使图表保持不变?我的代码是:

#!/usr/bin/python

import rpy2.robjects as robjects
r = robjects.r

r('x = c(1, 2, 3)')
r('y = c(6, 7, 8)')

r('plot(x, y)')

Terminating an R process closes interactive plotting windows left open... and this is what is happening at the end of your (Python) script. 终止R进程将关闭处于打开状态的交互式绘图窗口...这就是(Python)脚本结尾处发生的情况。

If you have to keep your Python process alive (since the embedded R depends on it), there are several strategies. 如果必须保持Python进程有效(因为嵌入式R依赖于它),可以采用几种策略。 The simplest might be to add at the end: 最简单的方法可能是在末尾添加:

import time
while True:
    time.sleep(1)

You could use Rstudio, this does does kill the window instantly. 您可以使用Rstudio,它确实会立即杀死窗口。 Alternatively, dump the plot to file using eg png . 或者,使用例如png将图转储到文件中。

Start the program manually with python your_program.py . 使用python your_program.py手动启动程序。 It must be a colateral from using eclipse 使用eclipse必须是附带条件

from rpy2.robjects.packages import importr
import rpy2.robjects as robjects

grdevices = importr('grDevices')

def mainloop():
    while True:
        if isinstance(grdevices.dev_list(), type(robjects.NULL)):
            break

robjects.r('x = c(1, 2, 3)')
robjects.r('y = c(6, 7, 8)')

robjects.r('plot(x, y)')
mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM