What is the environment() function in R?
Overview
In R, the environment() function returns the environment associated with a given function or formula.
An environment is a collection of objects such as functions, variables, and so on. Whenever we hit up the R interpreter, an environment is created. So at this point, any new variable we create is now contained in the new environment.
Syntax
environment(fun)
Parameter value
This function takes the parameter value, func, representing a formula function.
Example
# creating different R functionsfunction1 <- function() {print("Hello World!")}function2 <- function() {print(2*3)}environment(function1)environment(function2)
Explanation
- Line 2–8: We create
function1andfunction2. - Line 10–11: We implement the
environment()method on the functions we created.