Thursday 15 July 2010

python - RPy2 importing R source function generates error -



python - RPy2 importing R source function generates error -

as title says, have python script uses rpy2 , imports r file.

r file contents (named r_code.r):

test_function <- function(a) { <- as.vector(a) return(list(sum=sum(a))) }

python file contents (named rpy2_test.py):

import numpy np import rpy2.robjects robjects import rpy2.robjects.numpy2ri rpy2.robjects.numpy2ri.activate() robjects.r('''source('r_code.r')''') r_test_function = robjects.globalenv['test_function'] def py_test_function(a): mc = r_test_function(a) homecoming mc[0] = np.array([1,2,3,4,5]) mc = py_test_function(a) print mc[0]

both files in same directory. type "python rpy2_test.py" , error:

error in readlines(file, warn = false) : 5 arguments passed .internal(readlines) requires 6 traceback (most recent phone call last): file "rpy2_test.py", line 7, in robjects.r('''source('r_code.r')''') file "/usr/local/lib/python2.7/dist-packages/rpy2/robjects/init.py", line 246, in call

res = self.eval(p) file "/usr/local/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 166, in call

return super(signaturetranslatedfunction, self).call(*args, **kwargs) file "/usr/local/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 99, in call

res = super(function, self).call(*new_args, **new_kwargs) rpy2.rinterface.rruntimeerror: error in readlines(file, warn = false) :

5 arguments passed .internal(readlines) requires 6

the funny thing is, working not long ago , haven't changed (at to the lowest degree don't think have) on system.

python version: 2.7.3

numpy version: 1.8.0

rpy2 version: 2.4.4

r version: 3.0.2

ubuntu: 12.04.2

any ideas? thanks!

ok, after several hours of frustration.

i had compiled own r in past time compile/install fine not allow me build libs(that throw errors in rpy2) uninstalled/cleaned/removed/deleted many r files could. used apt-get --purge remove in case there older install.

made sure apt-get sources.list file had valid cran mirror listed, apt-get update.

then apt-get install r-base got in.

followed pip install rpy2 --upgrade (even though tried removing it, somehow still hanging on dog turd on shoe meant couldn't fresh install)

back business. life of me, don't know why rpy2 bridge stopped working.

there has not been much involvement on question i'll take own reply , close it. maybe it'll help in future.

@cdeterman: maybe r version...who knows! (still, +1 best suggestion)

update: have hunch perhaps may have been ubuntu updates interfered source install somehow. i'd curious know if else has had conflicts.

python r rpy2

No comments:

Post a Comment