force: Force Evaluation of an Argument

Description Usage Arguments Details Note Examples

View source: R/eval.R

Description

Forces the evaluation of a function argument.

Usage

1
force(x)

Arguments

x

a formal argument of the enclosing function.

Details

force forces the evaluation of a formal argument. This can be useful if the argument will be captured in a closure by the lexical scoping rules and will later be altered by an explicit assignment or an implicit assignment in a loop or an apply function.

Note

This is semantic sugar: just evaluating the symbol will do the same thing (see the examples).

force does not force the evaluation of other promises. (It works by forcing the promise that is created when the actual arguments of a call are matched to the formal arguments of a closure, the mechanism which implements lazy evaluation.)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
f <- function(y) function() y
lf <- vector("list", 5)
for (i in seq_along(lf)) lf[[i]] <- f(i)
lf[[1]]()  # returns 5

g <- function(y) { force(y); function() y }
lg <- vector("list", 5)
for (i in seq_along(lg)) lg[[i]] <- g(i)
lg[[1]]()  # returns 1

## This is identical to
g <- function(y) { y; function() y }

robertzk/monadicbase documentation built on May 27, 2019, 10:35 a.m.