Skip to content
Prev 302547 / 398503 Next

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: