Environment

The Environment class is used to wrap the environment objects in R. It can be useful to get or assign an object in an environment by specifying the variable name. Some advanced features of environment, such as locks and namespace, are also supported in the member functions.

Public Member Functions

Constructors

Environment()

Default constructor. Resulting object is the global environment R_GlobalEnv (.GlobalEnv in R).

Environment(const Environment &other)

Copy constructor. Resulting object will share the SEXP data with other.

template <typename Proxy>

Environment(const GenericProxy<Proxy> &proxy)

Create object from a proxy, such as attribute, slot, field, etc.

Environment(SEXP x)

Wrap a given environment.

Environment(const std::string &name)

Get the environment asscociated with the given name, such as ”.GlobalEnv” and “package:stats”. Equivalent to as.environment() in R.

Environment(int pos)

Get the environment in the given position (1-based) in search path. Equivalent to as.environment() in R.

Defined in Environment

SEXP ls(bool all) const
  • all: whether to list object whose name begins with a dot (.).

List objects in the environment, equivalent to ls(envir = this, all = all) in R.

SEXP get(const std::string &name) const

Get an object from the environment. If not found, R_NilValue will be returned.

SEXP find(const std::string &name) const

Get an object from the environment, or from one of its parents. If not found, an error will be thrown.

bool exists(const std::string &name) const

Test whether an object named name exists in the environment.

bool assign(const std::string &name, SEXP x) const

Attemp to assign x to name in this environment. Return true if the assignment is successful, and throw an error if the binding is locked.

template <typename WRAPPABLE>

bool assign(const std::string &name, const WRAPPABLE x) const

Wrap and assign. If there is a wrap() method taking an object of WRAPPABLE type, then it is wrapped and the corresponding SEXP data is assigned in the environment.

bool isLocked() const

Return true if this environment is locked. Equivalent R function is environmentIsLocked().

bool remove(const std::string &name)

Remove an object from this environment. Return true if the removal is successful. If the object doesn’t exist, an error will be thrown.

void lock(bool bindings = false)
  • bindings: whether to lock the bindings of this environment as well.

Lock this environment. Equivalent R function is lockEnvironment().

void lockBinding(const std::string &name)

Lock the given binding in this environment. Equivalent R function is lockBinding().

void unlockBinding(const std::string &name)

Unlock the given binding in this environment. Equivalent R function is unlockBinding().

bool bindingIsLocked(const std::string &name) const

Return true if the binding is locked in this environment. Equivalent R function is bindingIsLocked().

bool bindingIsActive(const std::string &name) const

Return true if the binding is active in this environment. Equivalent R function is bindingIsActive().

bool is_user_database() const

Indicates if this is a user defined database.

Environment parent() const

Return the parent environment.

Environment new_child(bool hashed)
  • hashed: if true, the environment will use a hash table.

Create a new environment whose parent is this environment.

Inherited from BindingPolicy

Binding operator[](const std::string &name)

Extract the object with name name in this environment. If this appears in the left hand side of assignment, the object in the right hand side will be assigned to name.

const_Binding operator[](const std::string &name) const

Extract the object with name name in this environment. Read-only.

Here is an example showing the usage of the getter/setter above:

Environment glob; // Default is the global environment
glob["x"] = Rcpp::rnorm(10); // Assign a vector to variable "x"
NumericVector xx = glob["x"]; // Get variable "x"

Inherited from other classes

See RObject.

Static Public Member Functions

static Environment global_env()

Return the global environment. Equivalent R function is globalenv().

static Environment empty_env()

Return the empty environment. Equivalent R function is emptyenv().

static Environment base_env()

Return the base environment. Equivalent R function is baseenv().

static Environment base_namespace()

Return the base namespace. Equivalent R object is .BaseNamespaceEnv.

static Environment Rcpp_namespace()

Return the Rcpp namespace.

static Environment namespace_env(const std::string &package)

Return the namespace of package package. If there is no such package, an error will be thrown.