简体   繁体   中英

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:

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. When I check it with rpy2 I obtain the following:

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:

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?

Thanks,

X11 is working on my side.

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.

There is a small utility module tell a user more about which R rpy2 is using:

python -m rpy2.situation

Does this match the R that has x11 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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