Environments in R Programming - GeeksforGeeks

05 Aug, 2020
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.
Every object in an environment has a name.
The environment has a parent environment.
Environments follow reference semantics.
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)
Parameters: 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”) 
Example:
R
# R program to illustrate# Environments in R  # Create new environmentnewEnv <- new.env()  # Assigning variablesnewEnv$x <- 1newEnv$y <- "GFG"newEnv$z <- 1:10  # Printprint(newEnv$z)
Output: 
 [1]  1  2  3  4  5  6  7  8  9 10
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.  
Syntax: 
ls() search() 
Parameters:These functions need no argument
Example: 
R
# R program to illustrate# Environments in R  # Prints all the bindings and environments # attached to Global Environmentls()  # Prints bindings of newEnvls(newEnv)  # Lists all the environments of the parent environmentsearch()
Output: 
[1] "al"     "e"      "e1"     "f"      "newEnv" "pts"    "x"      "y"     
[9] "z"

[1] "x" "y" "z"

[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base" 

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. 
Syntax: rm(...) 
Parameters: ...: indicates list of objects 
Example: 
R
# R program to illustrate# Environments in R  # Remove newEnvrm(newEnv)  # Listls()
Output: 
[1] "al"  "e"   "e1"  "f"   "pts" "x"   "y"   "z"

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.
Syntax: where(name) 
Parameters: name: indicates object to look for 
Example: 
R
# R program to illustrate# Environments in R  # Install pryr packageinstall.packages("pryr")  # Load the packagelibrary(pryr)  # Searchwhere("x")  where("mode")
Output: 
<environment: R_GlobalEnv>
<environment: base>

R-basics
R-Objects
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change column name of a given DataFrame in R
How to Replace specific values in column in R DataFrame ?
Filter data by multiple conditions in R using Dplyr
Adding elements in a vector in R programming - append() method
Loops in R (for, while, repeat)
Change Color of Bars in Barchart using ggplot2 in R
How to change Row Names of DataFrame in R ?
Convert Factor to Numeric and Numeric to Factor in R Programming
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr