Reputation: 1749
While using package rpy2, I get the error
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 'x' must be atomic Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 86, in call return super(SignatureTranslatedFunction, self).call(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 35, in call res = super(Function, self).call(*new_args, **new_kwargs) rpy2.rinterface.RRuntimeError: Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 'x' must be atomic
when executing
file.R_func.rdc([1,2,3,4,5],[1,3,4,5,6],20,1.67)
where file.py is defined as follows:
from rpy2.robjects.packages import SignatureTranslatedAnonymousPackage
string = """
rdc <- function(x,y,k,s) {
x <- cbind(apply(as.matrix(x),2,function(u) ecdf(u)(u)),1)
y <- cbind(apply(as.matrix(y),2,function(u) ecdf(u)(u)),1)
wx <- matrix(rnorm(ncol(x)*k,0,s),ncol(x),k)
wy <- matrix(rnorm(ncol(y)*k,0,s),ncol(y),k)
cancor(cbind(cos(x%*%wx),sin(x%*%wx)), cbind(cos(y%*%wy),sin(y%*%wy)))$cor[1]
}
"""
R_func = SignatureTranslatedAnonymousPackage(string, "R_func")
How do I have to pass x and y to rdc()?
Upvotes: 0
Views: 217
Reputation: 11545
When doing
file.R_func.rdc([1,2,3,4,5],[1,3,4,5,6],20,1.67)
an implicit conversion of Python objects is performed before passing them as parameters to the underlying R function.
By default, [1,2,3,4,5]
(which is a Python list
) will be converted to an R list
and R lists are "non-atomic vectors", meaning that each element in the list can be an arbitrary object by opposition to an "atomic" type such as boolean ("logical" in R lingo), an integer, a string, etc...
Try:
from rpy2.robjects.vectors import IntVector, FloatVector
# FloatVector is imported as an alternative if you need/prefer floats
file.R_func.rdc(IntVector([1,2,3,4,5]),
IntVector([1,3,4,5,6]),
20,
1.67)
Upvotes: 0