isofun: Isolate Closures for Serialization

View source: R/utils.R

isofunR Documentation

Isolate Closures for Serialization

Description

These functions modify the environment of a function to isolate a closure from its original environment.

Usage

isofun(fun, envir = baseenv())

isoclos(fun, envir = baseenv())

Arguments

fun

A function to isolate.

envir

The new parent environment for the closure.

Details

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).

Value

A new function or closure, isolated from environment(fun).

Author(s)

Kylie A. Bemis

See Also

environment

Examples

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)

kuwisdelu/matter documentation built on Oct. 19, 2024, 10:31 a.m.