[R] Force evaluation of a symbol when a function is created
Bert Gunter
gunter.berton at gene.com
Tue Aug 7 16:46:47 CEST 2012
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) # ... F picks up this value "implicitly" -- no need for explicit assignment.
But then there is no need for force(), is there?
> fy <- function(Y)function(x) x*Y
> Y <- 2
> F <- fy(Y)
> F(5)
[1] 10
> Y <- 3
> F(5)
[1] 10
> G <- fy(Y)
> G(5)
[1] 15
That is, one simply relies on lexical scoping/closures to "retain" the
value of Y used as a free variable in function(x)x*Y when it is
defined. No need to explicitly force() it. If wrong, I would be
grateful for correction. This appears to me to duplicate the Matlab
behavior rather closely.
-- Bert
On Tue, Aug 7, 2012 at 3:48 AM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
> 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 Murdoch
>
> On 12-08-06 5:07 PM, Schoenfeld, David Alan,Ph.D.,Biostatistics wrote:
>>
>>
>> I am porting a program in matlab to R,
>> The problem is that Matlab has a feature where symbols that aren't
>> arguments are evaluated immediately.
>> That is:
>> Y=3
>> F=@(x) x*Y
>>
>> Will yield a function such that F(2)=6.
>> If later say. Y=4 then F(2) will still equal 6.
>>
>> R on the other hand has lazy evaluation.
>> F<-function(x){x*Y}
>> Will do the following
>> Y=3
>> F(2)=6
>> Y=4
>> F(2)=8.
>> Does anyone know of away to defeat lazy evaluation in R so that I can
>> easily simulate the Matlab behavior. I know that I can live without this in
>> ordinary programming but it would make my port much easier.
>>
>> Thanks.
>>
>>
>>
>>
>> The information in this e-mail is intended only for the ...{{dropped:14}}
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Bert Gunter
Genentech Nonclinical Biostatistics
Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
More information about the R-help
mailing list