How to use the rpy2.rinterface.SexpVector function in rpy2

To help you get started, we’ve selected a few rpy2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github randy3k / rpy2 / rpy / robjects / __init__.py View on Github external
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):
        # special case for NA_Logical
        if o is rinterface.NA_Logical:
            res = rinterface.SexpVector([o, ], rinterface.LGLSXP)
        else:
            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
github rpy2 / rpy2 / rpy / rpy_classic.py View on Github external
def py2rpy(obj):
    if isinstance(obj, int):
        robj = ri.SexpVector([obj, ], ri.INTSXP)
        return robj 
    if isinstance(obj, float):
        robj = ri.SexpVector([obj, ], ri.REALSXP)
        return robj 
    if isinstance(obj, str):
        robj = ri.SexpVector([obj, ], ri.STRSXP)
        return robj 
    if isinstance(obj, complex):
        robj = ri.SexpVector([obj, ], ri.CPLSXP)
        return robj 
    if isinstance(obj, list) or isinstance(obj, tuple):
        robj = seq2vec(obj)
        return robj
    raise ValueError("Don't know what to do with 'obj'.")
github beiko-lab / gengis / bin / Lib / site-packages / rpy2 / robjects / __init__.py View on Github external
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):
        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
github jonovik / cgptoolbox / cgp / utils / rnumpy.py View on Github external
def test_rbinder():
    sexp = ri.SexpVector([1, 2, 3, 4], ri.INTSXP)
    sexp.do_slot_assign("dim", ri.SexpVector([2, 2], ri.INTSXP))
    binder = RBinder(sexp)
    assert repr(binder) == _r_repr(sexp)
    # simple indexing:
    assert binder[1] == 1
    assert binder(1) == 1
    # multidimensional indexing:
    assert binder[1, 2] == 3
    # slice indexing:
    assert (binder[1, :] == [1, 3]).all()
    assert (binder[1, 1:2] == [1, 3]).all()
    # kwargs indexing:
    assert binder(1, 1, drop=True).shape == (1,)
    assert binder(1, 1, drop=False).shape == (1, 1)
    # "method" calls:
    assert (binder.dim() == np.array([2, 2])).all()
github beiko-lab / gengis / bin / Lib / site-packages / rpy2 / robjects / __init__.py View on Github external
"""
    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):
        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
github beiko-lab / gengis / bin / Lib / site-packages / rpy2 / robjects / __init__.py View on Github external
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):
        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
github jonovik / cgptoolbox / cgp / utils / rnumpy.py View on Github external
def _set_sexp_shape(sexp, new_dim):
    # We don't convert 1-d (or 0-d) arrays to R arrays, but leave them as
    # vectors. This is because 1-d arrays in R are not very useful -- for
    # instance, if you have a length-n vector then matrix multiplication
    # will automatically coerce it to either nx1 or 1xn, but if you have a
    # length-n 1d array then matrix multiplication will not accept it at
    # all. Which breaks real code:
    if len(new_dim) < 2:
        return
    assert not sexp.named
    sexp.do_slot_assign("dim", ri.SexpVector(new_dim, ri.INTSXP))
github jonovik / cgptoolbox / cgp / utils / rnumpy.py View on Github external
def _arraylike_to_sexp(obj, recurse, **kwargs):
    if isinstance(obj, ri.Sexp):
        assert not kwargs
        return obj
    array = np.asarray(obj, **kwargs)
    sexp_seq = array.ravel("F")
    shape = array.shape
    if array.dtype.kind == "O":
        if not recurse:
            raise ValueError, "Cannot convert object arrays without recursing"
        else:
            sexp_seq = map(py2ri, sexp_seq)
    # Most types map directly to R arrays (or in one case an R list):
    if array.dtype.kind in _kind_to_sexp:
        sexp = ri.SexpVector(sexp_seq,
                             _kind_to_sexp[array.dtype.kind])
        _set_sexp_shape(sexp, shape)
        return sexp
    # R does not support unsigned types:
    elif array.dtype.kind == "u":
        msg = "Cannot convert numpy array of unsigned values -- "
        msg += "R does not have unsigned integers."
        raise ValueError, msg
    # Record arrays map onto R data frames:
    elif array.dtype.kind == "V":
        if len(array.shape) != 1:
            msg = "Only unidimensional record arrays can be "
            msg += "converted to data frames"
            raise ValueError, msg
        if array.dtype.names is None:
            msg = "Cannot convert void array of type %r to data.frame -- it "
github beiko-lab / gengis / bin / Lib / site-packages / rpy2 / robjects / __init__.py View on Github external
def default_ri2py(o):
    """ Convert :class:`rpy2.rinterface.Sexp` to higher-level objects,
    without copying the R objects.

    :param o: object
    :rtype: :class:`rpy2.robjects.RObject (and subclasses)`
    """

    res = None
    if isinstance(o, RObject):
        res = o
    elif isinstance(o, rinterface.SexpVector):
        try:
           cl = o.do_slot("class")[0]
           if cl == 'data.frame':
               res = RDataFrame(o)
        except LookupError, le:
            pass
        if res is None:
            try:
                dim = o.do_slot("dim")
                res = RArray(o)
            except LookupError, le:
                res = RVector(o)
    elif isinstance(o, rinterface.SexpClosure):
        res = RFunction(o)
    elif isinstance(o, rinterface.SexpEnvironment):
        res = REnvironment(o)