R/restrict_environment.R

Defines functions restrict_environment

Documented in restrict_environment

#' Build a new function with a smaller environment
#'
#' As standard feature, R include in the environment of a function
#' all the variables that are available when the function is created.
#' This, however is prompt to leak reference when you have a factory
#' of function and they are created within a list.. it will include all
#' the component of the list in the function environment.
#' To prevent that, the random generator functions are encapsulated with a
#' restricted environment where only the variables that the function requires
#' to work are included
#'
#' @author  John J. Aponte
#' @param f input function
#' @param ... define the set of  variables to be included as variable = value.
#' @return new function with a restricted environment
#' @importFrom pryr named_dots
#' @export
#' @examples
#' a = 0
#' b = 1
#' myfunc <- restrict_environment(
#'  function(n) {
#'    rnorm(meanvalue, sdvalue)
#'  },
#'  meanvalue = a, sdvalue = b)
#'
#' myfunc(10)
#' ls(envir=environment(myfunc))
#'
restrict_environment <- function(f, ...) {
  dots <- named_dots(...)
  oldEnv <- environment(f)
  newEnv <- new.env(parent = parent.env(oldEnv))
  values <- lapply(dots,function(x){eval(x, envir = oldEnv)})
  if (length(values) > 0) {
    for (i in 1:length(values)) {
      assign(names(values)[i], values[[i]], envir = newEnv)
    }
  }
  environment(f) <- newEnv
  f
}

Try the convdistr package in your browser

Any scripts or data that you put into this service are public.

convdistr documentation built on April 20, 2021, 9:06 a.m.