Carson Farmer wrote:
> Hi list!
>
> Normally in rpy2 I can do the following (or something similar):
>
> >>> Line_ = robjects.r["Line"]
> >>> Line_
> <RFunction - Python:0xa11688c / R:0x9c841a8>
>
> to get a function or variable from the global environment. Note: Line is
> part of the sp (spatial objects) R package. However, if I've
> (accidentally) created a variable named 'Line', when using the above
> method to retrieve the function it retrieves the variable instead:
>
> >>> robjects.r('Line <- "test"')
> <RVector - Python:0xa116a0c / R:0xa49efa8>
> >>> Line_ = robs.r['Line']
> >>> Line_
> <RVector - Python:0xa116a8c / R:0xa49efa8>
>
> Ok, that's expected..., so instead I try:
>
> >>> Line_ = rint.globalEnv.get("Line", wantFun=True)
> >>> Line_
> <rinterface.SexpClosure - Python:0xb7d383a0 / R:0x9c841a8>
>
> now my problem is that I can no longer do something like (for example):
>
> Line_(matrix_(unlist_([point for point in line]), nrow = X, byrow=True))
>
> because I get a ValueError: All parameters must be of type Sexp_Type.
Guessing from the example above (note: self-contained examples are often
better as they let one run the example), this is because _Line was
gotten from the low-level interface.
(And your report made me see that there is an issue in rpy2.robjects:
REnvironment.get() should accept the named parameter 'wantFun').
Workarounds are either:
- You can convert all parameters passed to Line into objects inheriting
from rinterface.Sexp
- You do:
Line_ = robjs.conversion.ri2py(rint.globalEnv.get("Line", wantFun=True))
Hoping this helps,
L.
> So my question is the following: Is it possible to get a function from
> the global (or a package) environment similar to the initial example,
> without having it blocked by variable names? Or similarly, is it
> possible to get a function using the second example, except separate
> from it's enclosing environment (i.e. SexpClosure)?
>
> Maybe this is a stupid question and I should be going about this in an
> entirely different way, and if that's the case, please let me know?!
>
> Regards,
>
> Carson
>
------------------------------------------------------------------------------
_______________________________________________
rpy-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rpy-list