【问题标题】:rpy2 plot problems(device not responding) rpy2.rinterface.NULLrpy2 绘图问题(设备无响应) rpy2.rinterface.NULL
【发布时间】:2017-10-31 06:56:19
【问题描述】:
Setup: 
1. win10,
2. python3.5/3.6, 
3. R 3.42,
4. rpy2 2.90 or 2.8x
5. Ipython 6.1.0

当我使用 rpy2 作为 rpy2 文档时,绘图有问题:

In [26]: import rpy2.robjects as robjects
...:
...: r = robjects.r
...:
...: x = robjects.IntVector(range(10))
...: y = r.rnorm(10)
...:
...: r.X11()
...:
...: r.layout(r.matrix(robjects.IntVector([1,2,3,2]), nrow=2, ncol=2))
...: r.plot(r.runif(10), y, xlab="runif", ylab="foo/bar", col="red")
...:
Out[26]: rpy2.rinterface.NULL

绘图后有一个图形,但是图形有问题,标题变成:R Graphics: Device3 (ACTIVE) (没有响应),然后python崩溃了,我需要重新启动它。

because my system language is chinese, the tranlation of the picture title is R Graphics: Device3 (ACTIVE) (not responding)

我尝试了不同版本的rpy2或python,他们也有这个问题。

谁能帮助我?

【问题讨论】:

  • 尝试过使用不同的系统语言,如英语?有没有向 rpy2 的开发者报告这个问题?
  • rpy2 对于 Windows 没有官方支持。在 VM 或 Docker 容器中尝试 Linux。
  • 删除指令 r.X11() 会发生什么?

标签: plot graphics rpy2


【解决方案1】:

尝试以下解决方法,即使在 2019 年似乎也需要。如果预览中几乎没有显示或未显示绘图,并且单击时窗口没有反应: 加载绘图两次并在两者之间使用 r.windows() / r.X11() / r.quartz() (图形命令 Windows / Unix / Mac 根据https://www.statmethods.net/graphs/creating.html --> 查看多个图形,尽管至少 X11( )似乎也适用于Windows)。 然后使用 grdevices.dev_off() 两次,并在两者之间选择等待时间,或者只执行一次,然后随时使用隔离的 grdevices.dev_off() 语句关闭它。

import time
from rpy2.robjects.packages import importr
from rpy2.robjects import r
from rpy2.robjects.lib import ggplot2
grdevices = importr('grDevices')
rprint = robjects.globalenv.get("print")

pp = ggplot2.ggplot(mtcars) + \
     ggplot2.aes_string(x='wt', y='mpg', col='factor(cyl)') + \
     ggplot2.geom_point() + \
     ggplot2.geom_smooth(ggplot2.aes_string(group = 'cyl'), method = 'lm')

rprint(pp)
r.windows()
rprint(pp)
grdevices.dev_off()

#From here optional, if you want a waiting time
#Elsewise close the plot manually afterwards with grdevices.dev_off()
time.sleep(10)
grdevices.dev_off()

如果窗口仍然崩溃,执行grdevices.dev_off(),不需要强制关闭

替代方案:根本不显示情节,而是保存图片。

grdevices.dev_copy(device = r.png, filename = "plot.png", width = 1000, height = 500)
rprint(pp)
grdevices.dev_off()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 2012-10-14
    相关资源
    最近更新 更多