RE: [R] do.call and environments

2004-03-11 Thread Liaw, Andy
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 th

RE: [R] do.call and environments

2004-03-11 Thread 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

Re: [R] do.call and environments

2004-03-10 Thread Gabor Grothendieck
OTECTED]> To: <[EMAIL PROTECTED]>,R-Help <[EMAIL PROTECTED]> Subject: Re: [R] do.call and environments The reason in your example that fx() doesn't find 'x' is that the lexical scope of fx() does not include 'x'. So, this is what must be fixed, in one

RE: [R] do.call and environments

2004-03-10 Thread Liaw, Andy
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 as

Re: [R] do.call and environments

2004-03-10 Thread Tony Plate
The reason in your example that fx() doesn't find 'x' is that the lexical scope of fx() does not include 'x'. So, this is what must be fixed, in one way or another. One simple way to make your example work is to define fx() in a place where its lexical scope includes the variables you want it

RE: [R] do.call and environments

2004-03-10 Thread Gabor Grothendieck
Try this: x <- 7 fx <- function(y) print(x*y) f <- function(fun, x) { myfx <- eval(parse(text=deparse(fun))) myfx(3) } f(fx,2) # uses 2, not 7, for x This creates a new function myfx which has the same functionality as fx but is created in the body of f and therefore has that

[R] do.call and environments

2004-03-10 Thread 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