[R] do.call and environments

Liaw, Andy andy_liaw at merck.com
Thu Mar 11 19:34:58 CET 2004


Tony,

Thanks very much for the correction.  I agree completely.  The real
nightmare is in not being able to ensure which object, if there can be
several of the same name within the scope, that the code is going to pickup.
With lexical scope, the developer has the responsibility and control over
that, but not with dynamic scope.

Best,
Andy

> From: Tony Plate
> 
> Andy, I think you're correct that Thomas Petzoldt wants 
> dynamic scoping, but
> isn't your argument here about strong typing vs weak typing?  To 
> paraphrase, one
> could say: For example, if weak typing were allowed I might write a 
> function `g' that
> passes argument `x' to fx() as a character, or a data frame, 
> or a list, or 
> an `lm' object,
> or a connection, ....  How would you write fx() to deal with 
> that nightmare 
> if you have
> weak typing? (Actually, the S language does have weak typing, 
> and some 
> functions
> deal with the "nightmare" by checking the types of their 
> arguments.  Other 
> functions
> just pass their arguments along without type checking, 
> resulting in cryptic 
> error
> messages).
> 
> I think the issues around lexical vs dynamic scope are more 
> to do with dynamic
> scope making programming accidents more likely, because of 
> the risk that
> a variable in a higher frame is masked unintentionally by 
> another variable of
> the same name in an intervening frame, and more difficult to 
> track down,
> because the error can depend on the exact calling sequence 
> when it occurred.
> 
> cheers,
> 
> Tony Plate
> 
> At Wednesday 11:03 AM 3/10/2004, Liaw, Andy wrote:
> >Seems to me what you want is dynamic scoping: `x' is not 
> defined in `fx'.
> >You want `x' to be found in the scope of the function(s) 
> that calls `fx',
> >rather than the environment where `fx' is defined.  I was 
> told (thanks,
> >Robert!) that that is a very bad idea:  as the author of 
> `fx', you want some
> >assurance of what `x' might be.  This is done via R's 
> lexical scope.  With
> >dynamic scope, there is absolutely no way to do that.  For 
> example, I might
> >write a function `g' that define `x' as a character, or a 
> data frame, or a
> >list, or an `lm' object, or a connection, ....  How would 
> you write `fx' to
> >deal with that nightmare if you have dynamic scope?
> >
> >Andy
> >
> > > From: Thomas Petzoldt
> > >
> > > Hello,
> > >
> > > I want to call a function "fx" given by name, where some "global"
> > > variables (in the environment of fx) are passed to the 
> function. For
> > > compatibility reasons I cannot modify the parameter list 
> of fx and I
> > > want to avoid setting variables in the global environment
> > > (e.g. via <<-)
> > >
> > > Is there a way, how to do this?
> > >
> > > Thomas P.
> > >
> > > The example:
> > >
> > > fx <- function(y) print(x*y)
> > >
> > > f <- function(fun, xx) {
> > >    fxx <- function() {do.call(fun, list(y=3))}
> > >    x <- x
> > >    fxx()
> > > }
> > >
> > > f("fx", 13)
> > >
> > > ## does not work, because fx does not find x
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide!
> > > http://www.R-project.org/posting-guide.html
> > >
> > >
> >
> >
> >-------------------------------------------------------------
> -----------------
> >Notice:  This e-mail message, together with any 
> attachments,...{{dropped}}
> >
> >______________________________________________
> >R-help at stat.math.ethz.ch mailing list
> >https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}




More information about the R-help mailing list