Exemplo 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
Exemplo n.º 2
0
def _rpy2py_sexpenvironment(obj):
    return Environment(obj)
Exemplo n.º 3
0
def _(obj):
    return Environment(obj)
Exemplo n.º 4
0
                        res = vectors.IntVector(o)
                elif o.typeof == rinterface.REALSXP:
                    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


conversion.ri2py = default_ri2py


def default_py2ri(o):