RE: [R] Summary: do.call and environments

2004-03-12 Thread Simon Fear
It is possible to fake something very like dynamic scoping in S-PLUS as in the following example: fx - function(y) print(get(x, inherit=T) * y) fxx - function(fun,x) fun(3) fxx(fx,2) [1] 6 As several posters already pointed out; this strategy will not always do what you want (it will pick up

RE: [R] Summary: do.call and environments

2004-03-11 Thread Gabor Grothendieck
To use the modify the solution from Tony and I so that you can pass the name of the function, rather than the function itself, like this: x - 7 fx - function(y) print(x*y) f - function(fun, x) { fun - get(fun) environment(fun) - environment() do.call(fun,list(3)) }

Re: [R] Summary: do.call and environments

2004-03-11 Thread Spencer Graves
That looks great, but I'm confused. In R 1.8.1 under Windows 2000, the suggested script produced for me the following: f(fx,2) [1] 6 I would have naively expected 14. From whence cometh 6? Also, I prefer to use transportable code wherever feasible. The same script generated

Re: [R] Summary: do.call and environments

2004-03-11 Thread Thomas Petzoldt
Hello, f(fx,2) [1] 6 I would have naively expected 14. From whence cometh 6? Also, I prefer to use transportable code wherever feasible. The 2*3=6, which was the intention. It is in fact only a proof of correctness, that 7 is not used here. The proposal of Gabor does exactly,

Re: [R] Summary: do.call and environments

2004-03-11 Thread Spencer Graves
] Cc: [EMAIL PROTECTED], [EMAIL PROTECTED] Subject: Re: [R] Summary: do.call and environments Hello, f(fx,2) [1] 6 I would have naively expected 14. From whence cometh 6? Also, I prefer to use transportable code wherever feasible. The 2*3=6, which was the intention

Re: [R] Summary: do.call and environments

2004-03-11 Thread Spencer Graves
ff1 - function(fun, x) { assign(fun, get(fun), 1) assign(x, x, 1) do.call(fun, list(y=3)) } tst - ff1(fx, 2) This assigns 6 to tst in both S-Plus 6.2 as R 1.8.1. However, it also prints [1] 6 in S-Plus 6.2 but not R. ??? Best Wishes, Spencer Graves Thomas Petzoldt wrote:

RE: [R] Summary: do.call and environments

2004-03-11 Thread Liaw, Andy
exercise can be regarded as simulating S-style dynamic scoping in R. --- Date: Thu, 11 Mar 2004 09:45:32 +0100 From: Thomas Petzoldt [EMAIL PROTECTED] To: Spencer Graves [EMAIL PROTECTED] Cc: [EMAIL PROTECTED], [EMAIL PROTECTED] Subject: Re: [R] Summary: do.call and environments

RE: [R] Summary: do.call and environments

2004-03-11 Thread Thomas Lumley
On Thu, 11 Mar 2004, Liaw, Andy wrote: Gabor, From: Gabor Grothendieck Note that R and S are fundamentally different when it comes to scoping. R uses lexical scoping, i.e. the parent environment of a function is the environment at the point where it is *defined* whereas S uses

Re: [R] Summary: do.call and environments

2004-03-11 Thread Douglas Bates
Thomas Lumley [EMAIL PROTECTED] writes: Lisp developers also used to think that dynamic scope was more useful and easier than dynamic scope. They don't now. Not surprising, given the way that you wrote the first sentence. :-) Did you mean for one of the dynamic scope phrases to be lexical

Re: [R] Summary: do.call and environments

2004-03-11 Thread Spencer Graves
Hi, Thomas: (see inline) Thomas Lumley wrote: On Thu, 11 Mar 2004, Liaw, Andy wrote: Gabor, From: Gabor Grothendieck Note that R and S are fundamentally different when it comes to scoping. R uses lexical scoping, i.e. the parent environment of a function is the environment at the

Re: [R] Summary: do.call and environments

2004-03-11 Thread Peter Dalgaard
Gabor Grothendieck [EMAIL PROTECTED] writes: S uses dynamic scoping, i.e. the parent environment in a function is the environment at the point where the function is *called*. Not really. The parent env. in S (or should I say: the other implementation of S) is essentially the global