RPy2 importing R source function generates error

Question:

As the title says, I have a Python script that uses Rpy2 and imports an R file.

R file contents (named r_code.R):

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

Python file contents (named rpy2_test.py):

import numpy as np
import rpy2.robjects as 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)
    return mc[0]

a = np.array([1,2,3,4,5])
mc = py_test_function(a)
print mc[0]

Both files are in the same directory. I type “python rpy2_test.py” and this is my error:

Error in readLines(file, warn = FALSE) :
5 arguments passed to .Internal(readLines) which requires 6
Traceback (most recent 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 to .Internal(readLines) which requires 6

The funny thing is, it was working not so long ago and I haven’t changed anything (at least I don’t think I have) on my 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!

Asked By: user1269942

||

Answers:

OK, after several hours of frustration.

I had compiled my own R in the past but this time it would compile/install fine but would not let me build some libs(that would then throw errors in rpy2) so I uninstalled/cleaned/removed/deleted as many R files as I could. Also used apt-get –purge remove as well in case there was an older install.

Made sure my apt-get sources.list file had a valid cran mirror listed, then apt-get update.

Then apt-get install r-base got everything back in.

Followed by a pip install rpy2 –upgrade (even though I tried removing it, somehow it was still hanging on like a dog turd on a shoe which meant I couldn’t do a fresh install)

Back to business. For the life of me, I don’t know why my rpy2 bridge stopped working.

There has not been much interest on this question so I’ll accept my own answer and close it. Maybe it’ll help someone in the future.

@cdeterman: maybe it was the R version…who knows! (still, +1 for the best suggestion)

update: I have a hunch that perhaps it may have been ubuntu updates that interfered with my source install somehow. I’d be curious to know if anyone else has had conflicts.

Answered By: user1269942

I’m getting RParsingError as well as UsageError: Cell magic %%R not found in google colab. Any suggestion?
Also, why It happened a lot that one day the codes run and the next day they don’t run.

Answered By: F MSh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.