spread: A function to make available list elements inside a function...

View source: R/spread.R

spreadR Documentation

A function to make available list elements inside a function or method

Description

Inside a function, a call to spread() will attach to the function environment, sys.frame(), the elements in the list, or of the conversion to list of the object (e.g. named vector or FLPar), so that they be called by name. The function environment will be deleted once the function returns, so those variables won't make it to the environment from which the function was called, or further up in the call stack.

Usage

spread(object, FORCE = FALSE)

Arguments

object

A named list or vector whose elements are to be loaded into the calling environment.

FORCE

Should existing variable with matching names be redefined?

Details

By default, spread() will not overwrite variables in the function environment with the same name as any list element, unless FORCE=TRUE

Value

Invisibly the names of the variables loaded into the calling environment.

Author(s)

The FLR Team

See Also

sys.nframe

Examples


# EXAMPLE function
foo <- function (params) {
  a <- spread(params)
 print(a)
 x*y
}
# x and y are accesible to the internal calculation
foo(params=list(x=3.5, y=9))

# Works with FLPar
foo(params=FLPar(x=3L, y=0.99238))

# Elements in object must be named
## Not run: foo(list(3, y=0.99238))

# If a variable is missing from the spread object, function will fail
## Not run: foo(list(x=4))
# Unless the variable is already defined in the calling environment,
# in this case <environment: R_GlobalEnv>
y <- 45
foo(params=list(x=4))

flr/FLCore documentation built on May 4, 2024, midnight