Instead of modifying a closure's parent environment directly,
sometimes it may be desirable to do a one-time injection that
overrides what would normally be accessible through the closure.
call_with
allows this by extending the usual do.call
to a third argument that is a list or environment temporarily
injected during the course of the call.
call_with(fn, args, with)
fn | function. |
---|---|
args | list. The arguments to call the |
with | list or environment. Additional locals to make available during the call. |
The result of calling fn
with the injection provided
by the with
parameter.
not_run({ fn <- local({ x <- 1; function(y) { x + y } }) stopifnot(fn(1) == 2) stopifnot(call_with(fn, list(1), list(x = 2)) == 3) })