Re: [R] Force evaluation of a symbol when a function is created

2012-08-07 Thread Duncan Murdoch
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

Re: [R] Force evaluation of a symbol when a function is created

2012-08-07 Thread Bert Gunter
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)

Re: [R] Force evaluation of a symbol when a function is created

2012-08-07 Thread Duncan Murdoch
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,

Re: [R] Force evaluation of a symbol when a function is created

2012-08-07 Thread Bert Gunter
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

Re: [R] Force evaluation of a symbol when a function is created

2012-08-06 Thread William Dunlap
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:

Re: [R] Force evaluation of a symbol when a function is created

2012-08-06 Thread Bert Gunter
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

Re: [R] Force evaluation of a symbol when a function is created

2012-08-06 Thread William Dunlap
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,

Re: [R] Force evaluation of a symbol when a function is created

2012-08-06 Thread Schoenfeld, David Alan,Ph.D.,Biostatistics
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;

Re: [R] Force evaluation of a symbol when a function is created

2012-08-06 Thread R. Michael Weylandt
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

Re: [R] Force evaluation of a symbol when a function is created

2012-08-06 Thread arun
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