Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

localfuncs

"Declare" child functions, allowing much tidier code


Description

Only call this within a function, say f. The named functions are copied into the environment of f, with their environments set to the environment of f. This means that when you call one of the named functions later in f, it will be able to see all the variables in f, just as if you had defined the function inside f. Using localfuncs avoids you having to clutter f with definitions of child functions. It differs from mlocal in that the local functions won't be changing objects directly in f unless they use <<- – they will instead have normal R lexical scoping.

Usage

localfuncs(funcs)

Arguments

funcs

character vector of function names

See Also

mlocal for a different approach

Examples

inner <- function( x) {
  y <<- y+x
  0
}
outer <- function( z) {
  # Multiply z by 2!
  y <- z
  localfuncs( 'inner')
  inner( z)
  return( y)
}
outer( 4) # 8

mvbutils

Workspace Organization, Code and Documentation Editing, Package Prep and Editing, Etc

v2.8.232
GPL (>= 2)
Authors
Mark V. Bravington <mark.bravington@csiro.au>
Initial release
2018-12-11

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.