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
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))
}
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
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,
]
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
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:
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
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
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
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
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
11 matches
Mail list logo