def formals(self): """ Return the signature of the underlying R function (as the R function 'formals()' would). """ res = _formals_fixed(self) res = conversion.rpy2py(res) return res
def test_dataframe_to_numpy(self): df = robjects.vectors.DataFrame( {'a': 1, 'b': 2, 'c': robjects.vectors.FactorVector('e')}) rec = conversion.rpy2py(df) assert numpy.recarray == type(rec) assert rec.a[0] == 1 assert rec.b[0] == 2 assert rec.c[0] == 'e'
def __call__(self, *args, **kwargs): new_args = [conversion.py2rpy(a) for a in args] new_kwargs = {} for k, v in kwargs.items(): # TODO: shouldn't this be handled by the conversion itself ? if isinstance(v, rinterface.Sexp): new_kwargs[k] = v else: new_kwargs[k] = conversion.py2rpy(v) res = super(Function, self).__call__(*new_args, **new_kwargs) res = conversion.rpy2py(res) return res
def eval(x, envir=ri.globalenv): """ Evaluate R code. If the input object is an R expression it evaluates it directly, if it is a string it parses it before evaluating it. By default the evaluation is performed in R's global environment but a specific environment can be specified.""" if isinstance(x, str): p = _parse(x) else: p = x res = _reval(p, envir=envir) res = conversion.rpy2py(res) return res
def get_deseq_result(self, contrast=None, **kwargs): self.comparison = deseq.resultsNames(self.dds) if contrast: if len(contrast) == 3: contrast = robjects.numpy2ri.numpy2ri(np.array(contrast)) else: assert len(contrast) == 2, 'Contrast must be length of 3 or 2' contrast = robjects.ListVector({None: con for con in contrast}) print('Using contrast: ', contrast) self.deseq_result = deseq.results(self.dds, contrast=contrast, **kwargs) else: self.deseq_result = deseq.results(self.dds, **kwargs) self.deseq_result = to_dataframe(self.deseq_result) self.deseq_result = conversion.rpy2py(self.deseq_result) return (self.deseq_result)
def set_accessors(cls, cls_name, where, acs): # set accessors (to be abandonned for the metaclass above ?) if where is None: where = rinterface.globalenv else: where = "package:" + str(where) where = StrSexpVector((where, )) for r_name, python_name, as_property, docstring in acs: if python_name is None: python_name = r_name r_meth = getmethod(StrSexpVector((r_name, )), signature = StrSexpVector((cls_name, )), where = where) r_meth = conversion.rpy2py(r_meth) if as_property: setattr(cls, python_name, property(r_meth, None, None)) else: setattr(cls, python_name, lambda self: r_meth(self))
def eval(x: str, envir: ri.SexpEnvironment = ri.globalenv) -> ri.Sexp: """ Evaluate R code. If the input object is an R expression it evaluates it directly, if it is a string it parses it before evaluating it. By default the evaluation is performed in R's global environment but a specific environment can be specified. Args: x (str): a string to be parsed and evaluated as R code envir (rpy2.rinterface.SexpEnvironment): An R environment in which to evaluate the R code. Returns: The R objects resulting from the evaluation.""" if isinstance(x, str): p = _parse(x) else: p = x res = _reval(p, envir=envir) res = conversion.rpy2py(res) return res
def __new__(mcs, name, bases, cls_dict): try: cls_rname = cls_dict['__rname__'] except KeyError: cls_rname = name try: accessors = cls_dict['__accessors__'] except KeyError: accessors = [] for rname, where, \ python_name, as_property, \ docstring in accessors: if where is None: where = rinterface.globalenv else: where = StrSexpVector(('package:%s' % where, )) if python_name is None: python_name = rname signature = StrSexpVector((cls_rname, )) r_meth = getmethod(StrSexpVector((rname, )), signature=signature, where=where) r_meth = conversion.rpy2py(r_meth) if as_property: cls_dict[python_name] = property(r_meth, None, None, doc=docstring) else: cls_dict[python_name] = lambda self: r_meth(self) return type.__new__(mcs, name, bases, cls_dict)
def test_convert_empty_df_with_rows(r2py): df = r("S4Vectors::DataFrame(a=1:10)[, -1]") assert df.slots["nrows"][0] == 10 df_py = r2py(anndata2ri, lambda: conversion.rpy2py(df)) assert isinstance(df_py, pd.DataFrame)
def do_slot(self, name): return conversion.rpy2py(super(RS4, self).do_slot(name))
def __getitem__(self, key): value = self._robj.do_slot(key) return conversion.rpy2py(value)
def test_dataframe_to_numpy(self): df = robjects.vectors.DataFrame(dict((('a', 1), ('b', 2)))) rec = conversion.rpy2py(df) assert numpy.recarray == type(rec) assert rec.a[0] == 1 assert rec.b[0] == 2