Ejemplo n.º 1
0
def reval_p(s, envir=None):
    ensure_initialized()
    with protected(s):
        if envir:
            # `sys.frame()` doesn't work with R_tryEval as it doesn't create R stacks,
            # we use `base::eval` instead.
            ret = rcall_p(("base", "eval"), s, _envir=envir)
        else:
            ret = lib.R_NilValue
            status = ffi.new("int[1]")
            with capture_console():  # need to capture stderr
                for i in range(0, lib.Rf_length(s)):
                    ret = lib.R_tryEval(lib.VECTOR_ELT(s, i), lib.R_GlobalEnv,
                                        status)
                    if status[0] != 0:
                        err = read_stderr().strip() or "Error"
                        raise RuntimeError("{}".format(err))
    return ret
Ejemplo n.º 2
0
def xptr_callback(exptr, arglist, asis, convert):
    asis = rcopy(bool, asis)
    convert = rcopy(bool, convert)
    f = from_xptr(exptr)
    args = []
    kwargs = {}
    names = rnames(arglist)
    for i in range(lib.Rf_length(arglist)):
        if asis:
            if names and names[i]:
                kwargs[names[i]] = lib.VECTOR_ELT(arglist, i)
            else:
                args.append(lib.VECTOR_ELT(arglist, i))
        else:
            if names and names[i]:
                kwargs[names[i]] = rcopy(lib.VECTOR_ELT(arglist, i))
            else:
                args.append(rcopy(lib.VECTOR_ELT(arglist, i)))

    ret = f(*args, **kwargs)
    with sexp_context(asis=asis, convert=convert):
        return sexp(ret) if convert else sexp_as_py_object(ret)
Ejemplo n.º 3
0
def rcopytype(_, s):  # noqa
    return text_type if lib.Rf_length(s) == 1 else list
Ejemplo n.º 4
0
def rcopytype(_, s):  # noqa
    return complex if lib.Rf_length(s) == 1 else list
Ejemplo n.º 5
0
def rcopytype(_, s):  # noqa
    return float if lib.Rf_length(s) == 1 else list
Ejemplo n.º 6
0
def rcopy(_, s):  # noqa
    ret = OrderedDict()
    names = rnames(s)
    for i in range(lib.Rf_length(s)):
        ret[names[i]] = rcopy(lib.VECTOR_ELT(s, i))
    return ret
Ejemplo n.º 7
0
def rcopy(_, s):  # noqa
    return [rcopy(lib.VECTOR_ELT(s, i)) for i in range(lib.Rf_length(s))]
Ejemplo n.º 8
0
def rcopy(_, s):  # noqa
    return [_string(lib.STRING_ELT(s, i)) for i in range(lib.Rf_length(s))]
Ejemplo n.º 9
0
def rcopy(_, s):  # noqa
    return ffi.string(lib.RAW(s), lib.Rf_length(s))
Ejemplo n.º 10
0
def rcopy(_, s):  # noqa
    return [
        complex(lib.COMPLEX(s)[i].r,
                lib.COMPLEX(s)[i].i) for i in range(lib.Rf_length(s))
    ]
Ejemplo n.º 11
0
def rcopy(_, s):  # noqa
    return [lib.REAL(s)[i] for i in range(lib.Rf_length(s))]
Ejemplo n.º 12
0
def rcopy(_, s):  # noqa
    return [bool(lib.LOGICAL(s)[i]) for i in range(lib.Rf_length(s))]