Esempio n. 1
0
def default_ri2ro(o):
    """ Convert an :class:`rpy2.rinterface.Sexp` object to a higher-level object,
    without copying the R object.

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

    res = None
    try:
        rcls = o.do_slot("class")
    except LookupError as le:
        rcls = [None]

    if isinstance(o, RObject):
        res = o
    elif isinstance(o, SexpVector):
        if 'data.frame' in rcls:
            res = vectors.DataFrame(o)
        if res is None:
            try:
                dim = o.do_slot("dim")
                if len(dim) == 2:
                    res = vectors.Matrix(o)
                else:
                    res = vectors.Array(o)
            except LookupError as le:
                if o.typeof == rinterface.INTSXP:
                    if 'factor' in rcls:
                        res = vectors.FactorVector(o)
                    else:
                        res = vectors.IntVector(o)
                elif o.typeof == rinterface.REALSXP:
                    if o.rclass[0] == 'POSIXct':
                        res = vectors.POSIXct(o)
                    else:
                        res = vectors.FloatVector(o)
                elif o.typeof == rinterface.STRSXP:
                    res = vectors.StrVector(o)
                elif o.typeof == rinterface.VECSXP:
                    res = vectors.ListVector(o)
                elif o.typeof == rinterface.LANGSXP and 'formula' in rcls:
                    res = Formula(o)
                else:
                    res = vectors.Vector(o)

    elif isinstance(o, SexpClosure):
        res = SignatureTranslatedFunction(o)
    elif isinstance(o, SexpEnvironment):
        res = Environment(o)
    elif isinstance(o, SexpS4):
        res = RS4(o)
    elif isinstance(o, SexpExtPtr):
        res = o
    elif o is NULL:
        res = o
    else:
        res = RObject(o)
    return res
Esempio n. 2
0
def _rpy2py_object(obj):
    return RObject(obj)
Esempio n. 3
0
def _(obj):
    return RObject(obj)
Esempio n. 4
0
                    res = Formula(o)
                else:
                    res = vectors.Vector(o)

    elif isinstance(o, SexpClosure):
        res = SignatureTranslatedFunction(o)
    elif isinstance(o, SexpEnvironment):
        res = Environment(o)
    elif isinstance(o, SexpS4):
        res = RS4(o)
    elif isinstance(o, SexpExtPtr):
        res = o
    elif o is NULL:
        res = o
    else:
        res = RObject(o)
    return res


conversion.ri2py = default_ri2py


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
    (wich means data copying).

    :param o: object
    :rtype: :class:`rpy2.rinterface.Sexp` (and subclasses)

    """