Learn R Programming

zlib (version 1.0.3)

publicEval: Evaluate Expression with Public and Private Environments

Description

publicEval creates an environment hierarchy consisting of public, self, and private environments. The expression expr is evaluated within these nested environments, allowing for controlled variable scope and encapsulation.

Usage

publicEval(expr, parentEnv = parent.frame(), name = NULL)

Value

Returns an invisible reference to the public environment.

Arguments

expr

An expression to evaluate within the constructed environment hierarchy.

parentEnv

The parent environment for the new 'public' environment. Default is the parent frame.

name

Optional name attribute to set for the public environment.

Environments

  • Public: Variables in this environment are externally accessible.

  • Self: Inherits from Public and also contains Private and Public as children.

  • Private: Variables are encapsulated and are not externally accessible.

Examples

Run this code
publicEnv <- publicEval({
  private$hidden_var <- "I am hidden"
  public_var <- "I am public"
}, parentEnv = parent.frame(), name = "MyEnvironment")

print(exists("public_var", envir = publicEnv))  # Should return TRUE
print(exists("hidden_var", envir = publicEnv))  # Should return FALSE

Run the code above in your browser using DataLab