Environments in R Programming
The environment is a virtual space that is triggered when an interpreter of a programming language is launched. Simply, the environment is a collection of all the objects, variables, and functions. Or, Environment can be assumed as a top-level object that contains the set of names/variables associated with some values. In this article, let us discuss creating a new environment in R programming, list all environments, removing a variable from the environment, searching for a variable or function among environments and function environments with the help of examples.
Why the Environment Differ from the List?
- Every object in an environment has a name.
- The environment has a parent environment.
- Environments follow reference semantics.
Create a New Environment
An environment in R programming can be created using new.env() function. Further, the variables can be accessed using $ or [[ ]] operator. But, each variable is stored in different memory locations. There are four special environments: globalenv(), baseenv(), emptyenv() and environment()
Syntax: new.env(hash = TRUE)
hash: indicates logical value. If TRUE, environments uses a hash table
To know about more optional parameters, use below command in console: help(“new.env”)
 1 2 3 4 5 6 7 8 9 10
List all Environments
Every environment has a parent environment but there is an empty environment that does not have any parent environment. All the environments can be listed using ls() function and search() function. ls() function also list out all the bindings of the variables in a particular environment.
These functions need no argument
 "al" "e" "e1" "f" "newEnv" "pts" "x" "y"  "z"  "x" "y" "z"  ".GlobalEnv" "package:stats" "package:graphics"  "package:grDevices" "package:utils" "package:datasets"  "package:methods" "Autoloads" "package:base"
Removing a Variable From an Environment
A variable in an environment is deleted using rm() function. It is different from deleting entries from lists as entries in lists are set as NULL to be deleted. But, using rm() function, bindings are removed from the environment.
…: indicates list of objects
 "al" "e" "e1" "f" "pts" "x" "y" "z"
Search a Variable or Function Among Environments
A variable or a function can be searched in R programming by using where() function among all the environments and packages present. where() function is present in pryr package. This function takes only two arguments, the name of the object to search for and the environment from where to start the search.
name: indicates object to look for
<environment: R_GlobalEnv> <environment: base>