Here's one more way. It seems to me this is the most R-like way to do
what you want:
multiply_by_Y - function(Y) {
force(Y)
function(x) x*Y
}
F - multiply_by_Y(3)
The force call forces Y to be evaluated at that point, so its value is
fixed from that point forward.
Duncan
Duncan, et.al:
Many thanks: let the closure do the work automatically rather than
manually manipulating it.
However, in the spirit of the OP's original request, I believe the
call would be:
Y - 3 ## That is, Y gets a value at some prior point, perhaps
programmatically.
F - multiply_by_Y(Y)
On 12-08-07 10:46 AM, Bert Gunter wrote:
Duncan, et.al:
Many thanks: let the closure do the work automatically rather than
manually manipulating it.
However, in the spirit of the OP's original request, I believe the
call would be:
Y - 3 ## That is, Y gets a value at some prior point,
Thanks Duncan. That clarifies it!
-- Bert
On Tue, Aug 7, 2012 at 12:28 PM, Duncan Murdoch
murdoch.dun...@gmail.com wrote:
On 12-08-07 10:46 AM, Bert Gunter wrote:
Duncan, et.al:
Many thanks: let the closure do the work automatically rather than
manually manipulating it.
However, in the
You could use local(), as in
F - local({
+Y - 3
+function(x) x * Y
+})
F(7)
[1] 21
Y - 19
F(5)
[1] 15
Look into 'environments' for more.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-Original Message-
From:
Thanks to both: Cute question, clever, informative answer.
However, Bill, I don't think you **quite** answered him, although the
modification needed is completely trivial. Of course, I could never
have figured it out without your response.
Anyway, I interpret the question as asking for the
Both of those approaches require the function to be created
at the same time that the environment containing some of its
bindings is created. You can also take an existing function and
assign a new environment to it. E.g.,
f - function(x) y * x
ys - c(2,3,5,7,11)
fs - lapply(ys,
Thank you both, this was very helpful. I need to study environments more. Do
either of you know a good source?
-Original Message-
From: Bert Gunter [mailto:gunter.ber...@gene.com]
Sent: Monday, August 06, 2012 6:03 PM
To: William Dunlap
Cc: Schoenfeld, David Alan,Ph.D.,Biostatistics;
On Mon, Aug 6, 2012 at 9:03 PM, Schoenfeld, David
Alan,Ph.D.,Biostatistics dschoenf...@partners.org wrote:
Thank you both, this was very helpful. I need to study environments more. Do
either of you know a good source?
Disclaimer: I really have no idea what I'm talking about.
They are a
Hi,
Try this:
F-function(x,type=local){Y=3
x*Y}
F(3)
#[1] 9
Y-4
F(3)
#[1] 9
Y-5
F(3)
#[1] 9
A.K.
- Original Message -
From: Schoenfeld, David Alan,Ph.D.,Biostatistics dschoenf...@partners.org
To: 'r-help@r-project.org' r-help@r-project.org
Cc:
Sent: Monday, August 6, 2012 5:07
10 matches
Mail list logo