On 1/13/10 3:41 PM, Nick Schurch wrote:
> Hi rpy-ers,
>
> I'm having a bit of a problem persuading rpy to forget a user defined
> function.
>
> Suppose I write a python subroutine that defines and runs a R
> function, something like:
>
> def mysubroutine(data, string, condition)
> set_default_mode(NO_CONVERSION)
> if condition=="False":
> r('filter<-
> function(dat,string){index=which(attributes(dat)$forenames==string)\nreturn(index)}')
> elif condition=="True":
> r('filter<-
> function(dat,string){index=which(attributes(dat)$surnames==string)\nreturn(index)}')
> dataout = r.filter(rdata, string)
> return(dataout)
Why use "True" and "False" when Python has True and False ?
Why define the function and assign it to an R variable name at each call ?
> If I now load the subroutine into ipython and run the subroutine,
> using some appropriate data that is already an robj:
>
>>>> x=mysubroutine(customers, "Joe", "False")
>
> x is now an robj containing the R indexes of all customers whose
> forename is Joe.
>
> If I then run the subroutine again:
>
>>>> y=mysubroutine(customers, "Blogs", "True")
>
> y should be an robj containing the R indexes of all customers whose
> surname is Blogs; but it isn't. It is an robj containing the R indexes
> of all customers whose forename is Blogs. The user defined R-function
> defined from the first run is not overwritten by the second run.
There is the remote possibility that there is caching involved, and
r.filter is bound to your first function definition (and not rebound
when redefined... since rpy is not told to do so).
Anyway, what is shown can be seen as not-so-elegant; what about defining
2 functions filter_true, filter_false one for all call one or the other.
Or even better, make named list element ("forenames" or "surnames" a
parameter to *one* function) ?
(rpy2 facilitates the use of anonymous functions, and help you from
clogging the .GlobalEnv with variables, but that for a separate thread).
> Is
> there any way to clear an R-function from rpys R instance?
In R, one would delete the object going by the symbol "filter" from
.GlobalEnv:
rm("filter")
L.
> --
> Cheers,
>
> Nick Schurch
>
> Data Analysis Group (The Barton Group),
> School of Life Sciences,
> University of Dundee,
> Dow St,
> Dundee,
> DD1 5EH,
> Scotland,
> UK
>
> Tel: +44 1382 388707
> Fax: +44 1382 345 893
>
> ------------------------------------------------------------------------------
> This SF.Net email is sponsored by the Verizon Developer Community
> Take advantage of Verizon's best-in-class app development support
> A streamlined, 14 day to market process makes app distribution fast and easy
> Join now and get one step closer to millions of Verizon customers
> http://p.sf.net/sfu/verizon-dev2dev
> _______________________________________________
> rpy-list mailing list
> rpy-list@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/rpy-list
------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list