isofun | R Documentation |
These functions modify the environment of a function to isolate a closure from its original environment.
isofun(fun, envir = baseenv())
isoclos(fun, envir = baseenv())
fun |
A function to isolate. |
envir |
The new parent environment for the closure. |
A common challenge with parallel programming in R is unintentionally serializing large environments that have been captured by closures. The functions isofun
and isoclos
provide straightforward ways to isolate functions and closures from their original parent environments which may contain large objects.
isofun
simply replaces the environment of fun
with envir
, which defaults to the base environment. This is appropriate for simple functions that do not need to enclose any variables, instead taking all of their inputs as formal arguments.
isoclos
creates a new closure that is isolated from fun
's original environment. All objects in environment(fun)
are first copied into a new environment with parent envir
, and then this new environment is assigned to fun
.
Note that the default envir=baseenv()
means that any functions not in the base environment must be fully qualified (e.g., stats::sd
versus sd
).
A new function or closure, isolated from environment(fun)
.
Kylie A. Bemis
environment
register(SerialParam())
bigfun <- function(x)
{
# isolate 'smallfun' from 'x'
smallfun <- isofun(function(xi) {
(xi - mean(xi)) / stats::sd(xi)
})
chunkApply(x, 2L, smallfun)
}
set.seed(1)
x <- matrix(runif(100), nrow=10, ncol=10)
bigfun(x)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.