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

William Dunlap wdunlap at tibco.com
Tue Aug 7 00:21:39 CEST 2012


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, function(y) {
                          env <- new.env(parent=baseenv());
                          env[["y"]] <- y ;
                          environment(f) <- env ; f })
  > # fs is a list of functions, all identical except for their environments, which contain 'y'.
  > fs[[2]]
  function (x) 
  y * x
  <environment: 0x0000000005df1c38>
  > fs[[2]](10)
  [1] 30
  > fs[[3]]
  function (x) 
  y * x
  <environment: 0x0000000005def8c0>
  > fs[[3]](10)
  [1] 50

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: Bert Gunter [mailto:gunter.berton at gene.com]
> Sent: Monday, August 06, 2012 3:03 PM
> To: William Dunlap
> Cc: Schoenfeld, David Alan,Ph.D.,Biostatistics; r-help at r-project.org
> Subject: Re: [R] Force evaluation of a symbol when a function is created
> 
> 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 function definition
> to _implicitly_ pick up the value of Y at the time the function is
> defined, rather than explicitly assigning it in local(). The following
> are two essentially identical approaches: I prefer the second, because
> it's more transparent to me, but that's just a matter of taste.
> 
> Y <- 3
> F <-local({y <- Y;function(x)x*y})
> G <- evalq(function(x)x*y,env=list(y=Y))
> 
> Yielding:
> 
> > Y <- 3
> > F <-local({y <- Y;function(x)x*y})
> > G <- evalq(function(x)x*y,env=list(y=Y))
> > F(5)
> [1] 15
> > G(5)
> [1] 15
> > Y <- 2
> > F(5)
> [1] 15
> > G(5)
> [1] 15
> > F <-local({y <- Y;function(x)x*y})
> > G <- evalq(function(x)x*y,env=list(y=Y))
> > F(5)
> [1] 10
> > G(5)
> [1] 10
> 
> Cheers,
> Bert
> 
> On Mon, Aug 6, 2012 at 2:24 PM, William Dunlap <wdunlap at tibco.com> wrote:
> > 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.
> >
> > ______________________________________________
> > 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