Hi all,
I am trying to wrap adk.test from R package adk using rpy2 2.2.1 but I
am running into problems with passing parameters to it. More
specifically I get a runtime error in R:
> In [1]: %run ad.py
> Carico il pacchetto richiesto: adk
> Error in sort.list(unique(x)) :
> 'x' must be atomic for 'sort.list'
> Have you called 'sort' on a list?
> ---------------------------------------------------------------------------
> RRuntimeError Traceback (most recent call
> last)
>
> /home/giovanni/repos/peers/ad.py in <module>()
> 20 if __name__ == '__main__':
> 21 r_adk = R_factory()
> ---> 22 r_adk(np.random.rand(100),np.random.rand(100))
> 23
> 24
>
> /home/giovanni/repos/peers/ad.py in r_adk(*args)
> 14 '''
> 15 args = map(Vector, map(list, args))
> ---> 16 return adk_test(*args)
> 17
> 18 return r_adk
>
> /usr/lib/python2.6/dist-packages/rpy2/robjects/functions.pyc in
> __call__(self, *args, **kwargs)
> 81 v = kwargs.pop(k)
> 82 kwargs[r_k] = v
> ---> 83 return super(SignatureTranslatedFunction,
> self).__call__(*args, **kwargs)
> 84
> 85
>
> /usr/lib/python2.6/dist-packages/rpy2/robjects/functions.pyc in
> __call__(self, *args, **kwargs)
> 33 for k, v in kwargs.iteritems():
> 34 new_kwargs[k] = conversion.py2ri(v)
> ---> 35 res = super(Function, self).__call__(*new_args,
> **new_kwargs)
> 36 res = conversion.ri2py(res)
> 37 return res
>
> RRuntimeError: Error in sort.list(unique(x)) :
> 'x' must be atomic for 'sort.list'
> Have you called 'sort' on a list?
What I do in Python is the following:
****************
''' Anderson-Darling k-samples test '''
import numpy as np
def R_factory():
from rpy2.robjects import r, Vector
require = r['require']
require('adk')
adk_test = r['adk.test']
def r_adk(*args):
'''
R version from adk
'''
args = map(Vector, map(list, args))
return adk_test(*args)
return r_adk
if __name__ == '__main__':
r_adk = R_factory()
r_adk(np.random.rand(100),np.random.rand(100))
*************
The actual code of adk.test is:
> function (...)
> {
> if (nargs() == 1 & is.list(list(...)[[1]])) {
> samples <- list(...)[[1]]
> }
> else {
> samples <- list(...)
> }
> k <- length(samples)
> if (k < 2)
> stop("Must have at least two samples.")
> ns <- sapply(samples, length)
> if (any(ns == 0))
> stop("One or more samples have no observations.")
> x <- NULL
> for (i in 1:k) x <- c(x, samples[[i]])
> n <- length(x)
> Z.star <- sort(unique(x))
I also tried putting the Vectors in an R list and passing that to r_adk
or redefining r_adk(a,b) instead of r_adk(*args) but doesn't seem to
like both. I am not an R expert so forgive if it's a very silly question.
Best
G
------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1, ECMAScript5, and DOM L2 & L3.
Spend less time writing and rewriting code and more time creating great
experiences on the web. Be a part of the beta today
http://p.sf.net/sfu/msIE9-sfdev2dev
_______________________________________________
rpy-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rpy-list