def __call__(self, *args, **kwargs): new_args = [conversion.py2ri(a) for a in args] new_kwargs = {} for k, v in kwargs.iteritems(): new_kwargs[k] = conversion.py2ri(v) res = super(RFunction, self).__call__(*new_args, **new_kwargs) res = conversion.ri2py(res) return res
def numpy2ri(o): if isinstance(o, numpy.ndarray): if not o.dtype.isnative: raise(ValueError("Cannot pass numpy arrays with non-native byte orders at the moment.")) # The possible kind codes are listed at # http://numpy.scipy.org/array_interface.shtml kinds = { # "t" -> not really supported by numpy "b": rinterface.LGLSXP, "i": rinterface.INTSXP, # "u" -> special-cased below "f": rinterface.REALSXP, "c": rinterface.CPLXSXP, # "O" -> special-cased below "S": rinterface.STRSXP, "U": rinterface.STRSXP, # "V" -> special-cased below } # Most types map onto R arrays: if o.dtype.kind in kinds: # "F" means "use column-major order" vec = rinterface.SexpVector(o.ravel("F"), kinds[o.dtype.kind]) dim = rinterface.SexpVector(o.shape, rinterface.INTSXP) res = ro.r.array(vec, dim=dim) # R does not support unsigned types: elif o.dtype.kind == "u": raise(ValueError("Cannot convert numpy array of unsigned values -- R does not have unsigned integers.")) # Array-of-PyObject is treated like a Python list: elif o.dtype.kind == "O": res = conversion.py2ri(list(o)) # Record arrays map onto R data frames: elif o.dtype.kind == "V": if o.dtype.names is None: raise(ValueError("Nothing can be done for this numpy array type %s at the moment." % (o.dtype,))) df_args = [] for field_name in o.dtype.names: df_args.append((field_name, conversion.py2ri(o[field_name]))) res = ro.baseenv["data.frame"].rcall(tuple(df_args), ro.globalenv) # It should be impossible to get here: else: raise(ValueError("Unknown numpy array type.")) else: res = ro.default_py2ri(o) return res
def default_py2ri(o): """ Convert arbitrary Python object to :class:`rpy2.rinterface.Sexp` to objects, creating an R object with the content of the Python object in the process (wichi means data copying). :param o: object :rtype: :class:`rpy2.rinterface.Sexp` (and subclasses) """ if isinstance(o, RObject): res = rinterface.Sexp(o) if isinstance(o, rinterface.Sexp): res = o elif isinstance(o, array.array): if o.typecode in ('h', 'H', 'i', 'I'): res = rinterface.SexpVector(o, rinterface.INTSXP) elif o.typecode in ('f', 'd'): res = rinterface.SexpVector(o, rinterface.REALSXP) else: raise(ValueError("Nothing can be done for this array type at the moment.")) elif isinstance(o, bool): res = rinterface.SexpVector([o, ], rinterface.LGLSXP) elif isinstance(o, int) or isinstance(o, long): res = rinterface.SexpVector([o, ], rinterface.INTSXP) elif isinstance(o, float): res = rinterface.SexpVector([o, ], rinterface.REALSXP) elif isinstance(o, str): res = rinterface.SexpVector([o, ], rinterface.STRSXP) elif isinstance(o, unicode): res = rinterface.SexpVector([o, ], rinterface.STRSXP) elif isinstance(o, list): res = r.list(*[conversion.ri2py(conversion.py2ri(x)) for x in o]) elif isinstance(o, complex): res = rinterface.SexpVector([o, ], rinterface.CPLXSXP) else: raise(ValueError("Nothing can be done for the type %s at the moment." %(type(o)))) return res
def __setitem__(self, i, value): value = conversion.py2ri(value) res = super(RVector, self).__setitem__(i, value)
def __init__(self, o): if not isinstance(o, rinterface.SexpVector): o = conversion.py2ri(o) super(RVector, self).__init__(o) self.r = RVectorDelegator(self)