Schoenfeld, David Alan,Ph.D.,Biostatistics
2012-Aug-06 21:07 UTC
[R] Force evaluation of a symbol when a function is created
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}}
William Dunlap
2012-Aug-06 21:24 UTC
[R] Force evaluation of a symbol when a function is created
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: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Schoenfeld, David Alan,Ph.D.,Biostatistics > Sent: Monday, August 06, 2012 2:08 PM > To: 'r-help at r-project.org' > Subject: [R] Force evaluation of a symbol when a function is created > > > 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.
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" <DSCHOENFELD at partners.org> To: "'r-help at r-project.org'" <r-help at r-project.org> Cc: Sent: Monday, August 6, 2012 5:07 PM Subject: [R] Force evaluation of a symbol when a function is created 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.
Duncan Murdoch
2012-Aug-07 10:48 UTC
[R] Force evaluation of a symbol when a function is created
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. >