简体   繁体   English

如何在python环境中将x11()用于rpy2?

[英]how can I get x11() available for rpy2 in python environment?

I am trying to plot a simple graph in python 3 using the rpy2 library: 我正在尝试使用rpy2库在python 3中绘制一个简单的图形:

import rpy2.robjects as robjects

r = robjects.r

x = robjects.IntVector(range(10))
y = r.rnorm(10)

r.X11(1,1)

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"

But I get this error: 但是我得到这个错误:

/Users/Irbin/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:145: RRuntimeWarning: Error in (function (display = "", width, height, pointsize, gamma, bg,  : 
X11 is not available
warnings.warn(x, RRuntimeWarning)---------------------------------------------------------------------------
RRuntimeError                             Traceback (most recent call last)
<ipython-input-3-bdd0b9cb9184> in <module>()
----> 1 import codecs, os, ast;__pyfile = codecs.open('''/var/folders/7q/2v_j6pcx0bb9jq2r_4b2b30w0000gn/T/pyDtdUIr''', encoding='''utf-8''');__code = __pyfile.read().encode('''utf-8''');__pyfile.close();os.remove('''/var/folders/7q/2v_j6pcx0bb9jq2r_4b2b30w0000gn/T/pyDtdUIr''');__block = ast.parse(__code, '''/Users/Irbin/Downloads/scratch.py''', mode='exec');__last = __block.body[-1];__isexpr = isinstance(__last,ast.Expr);__block.body.pop() if __isexpr else None;exec(compile(__block, '''/Users/Irbin/Downloads/scratch.py''', mode='exec'));eval(compile(ast.Expression(__last.value), '''/Users/Irbin/Downloads/scratch.py''', mode='eval')) if __isexpr else None

~/Downloads/scratch.py in <module>()
  6 # r Into (run inside sub codes)
  7 
----> 8 
  9 import pdb
 10 

~/anaconda3/lib/python3.6/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs)
176                 v = kwargs.pop(k)
177                 kwargs[r_k] = v
--> 178         return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
179 
180 pattern_link = re.compile(r'\\link\{(.+?)\}')

~/anaconda3/lib/python3.6/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs)
104         for k, v in kwargs.items():
105             new_kwargs[k] = conversion.py2ri(v)
--> 106         res = super(Function, self).__call__(*new_args, **new_kwargs)
107         res = conversion.ri2ro(res)
108         return res

RRuntimeError: Error in (function (display = "", width, height, pointsize, gamma, bg,  : 
X11 is not available

I googled it and I have understood X11 is necessary on OSX for graphics. 我用谷歌搜索,并且我知道X11在OSX上是必需的。 When I check it with rpy2 I obtain the following: 当我用rpy2检查它时,我得到以下信息:

print(robjects.r("capabilities()"))

jpeg         png        tiff       tcltk         X11        aqua 
TRUE        TRUE        TRUE        TRUE       FALSE        TRUE 
http/ftp     sockets      libxml        fifo      cledit       iconv 
TRUE        TRUE        TRUE        TRUE        TRUE        TRUE 
NLS     profmem       cairo         ICU long.double     libcurl 
TRUE        TRUE        TRUE        TRUE        TRUE        TRUE 

Which indicate X11() is not available (FALSE), but if I run native R and code the same instruction X11() is available, as you can see: 这表明X11()不可用(FALSE),但是如果我运行本机R并编写代码,则相同的指令X11()可用,如您所见:

capabilities()

jpeg         png        tiff       tcltk         X11        aqua 
TRUE        TRUE        TRUE        TRUE        TRUE        TRUE 
http/ftp     sockets      libxml        fifo      cledit       iconv 
TRUE        TRUE        TRUE        TRUE       FALSE        TRUE 
NLS     profmem       cairo         ICU long.double     libcurl 
TRUE        TRUE        TRUE        TRUE        TRUE        TRUE

So, how can I get x11() available for rpy2 in a python environment? 那么,如何在python环境中为rpy2提供x11()?

Thanks, 谢谢,

X11 is working on my side. X11在我这边工作。

Whenever you observe differences between R in rpy2 and the "regular R on the system", this is often means that different installations of R are used. 每当您观察到rpy2中的R与“系统上的常规R”之间的差异时,这通常意味着使用了不同的R安装。

There is a small utility module tell a user more about which R rpy2 is using: 有一个小的实用程序模块,可向用户详细说明正在使用哪个R rpy2:

python -m rpy2.situation

Does this match the R that has x11 ? 这是否匹配具有x11的R?

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

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