Making data accessible in functions
Suppose you wanted to create a function that can access
certain data, but you do not wish to pass the data as
an argument. Rather, you would like to `equip' the
function with the data. The function provide
will do
just that.
provide <- function(f, provision) { environment(f) <- list2env(provision, parent = environment(f)) f }
It will take a function f
and a list provision
,
and make the data in provision
accessible in f
.
Here is an example function.
fun <- function(x) x + a
Let us try fun
.
a <- 2
fun(1)
Suppose we wanted to fix the value of a
.
fun2 <- provide(fun, list(a = 41))
Let us try fun2
.
a <- 2
fun2(1)