[Rd] Behaviour of environment(foo)<- (PR#1509)

Thomas Lumley tlumley@u.washington.edu
Tue, 7 May 2002 08:38:59 -0700 (PDT)


On Tue, 7 May 2002, Jonathan Rougier wrote:

> Luke Tierney wrote:
> >
> >
> > In fact, given these issues I would argue that a pretty strong case
> > can be made for eliminating formals<-, body<- and environment<- (at
> > least for functions), though there are some very limited circumstances
> > where they can be useful if used with care.
> >
> > So, after all that, the real question: why are you doing this and are
> > you sure there isn't a better way?
>
> Now you've scared me!  I've been using the "list of functions with a
> common environment" approach to implement objects with methods and
> shared data, similar to that used by Douglas Bates and Saikat DebRoy in
> "nls", particularly their function "nlsModel".  So I can write
> foo$method() to use method on the foo object and its collection of
> common variables.  If I want to assign a function defined outside as a
> method then I need something like
>
> robj$method <- functionDefinedOutside
> environment(robj$method) <- myEnv
>

Tony Rossini and I have also been using something similar for the
interface to Orca, essentially for the same reasons.

It would also be possible to do this with a macro:

   robj$method<-functionFactory()
with
   functionFactory<-defmacro(body={
			functionDefinedOutside(args){body;body;body}})
using the macro code from R-News 3/2001
or equivalently
   functionFactory<-function(){
		  fn<-quote(functionDefinedOutside(args){body;body;body})
	          eval.parent(fn)
		}



	-thomas

Thomas Lumley			Asst. Professor, Biostatistics
tlumley@u.washington.edu	University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._