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
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)
def rcopytype(_, s): # noqa return text_type if lib.Rf_length(s) == 1 else list
def rcopytype(_, s): # noqa return complex if lib.Rf_length(s) == 1 else list
def rcopytype(_, s): # noqa return float if lib.Rf_length(s) == 1 else list
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
def rcopy(_, s): # noqa return [rcopy(lib.VECTOR_ELT(s, i)) for i in range(lib.Rf_length(s))]
def rcopy(_, s): # noqa return [_string(lib.STRING_ELT(s, i)) for i in range(lib.Rf_length(s))]
def rcopy(_, s): # noqa return ffi.string(lib.RAW(s), lib.Rf_length(s))
def rcopy(_, s): # noqa return [ complex(lib.COMPLEX(s)[i].r, lib.COMPLEX(s)[i].i) for i in range(lib.Rf_length(s)) ]
def rcopy(_, s): # noqa return [lib.REAL(s)[i] for i in range(lib.Rf_length(s))]
def rcopy(_, s): # noqa return [bool(lib.LOGICAL(s)[i]) for i in range(lib.Rf_length(s))]