R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Environments in R are an important and somewhat unique feature, fundamental for understanding how variables are stored, located, and how scoping works. In this tutorial, we'll explore the basics of environments in R programming.
In R, an environment is a collection of pairs, associating names with values. It is, in essence, a way of organizing variables. Every environment has a parent environment (except for the empty environment), forming a chain of environments.
a. Global Environment:
This is where your user-defined variables reside.
x <- 10 y <- 20 ls() # Lists the variables in the global environment
b. Creating a New Environment:
You can create a new environment using the new.env()
function.
new_env <- new.env() new_env$x <- 100
c. Parent Environment:
By default, the parent environment of a new environment is the global environment.
parent.env(new_env) # Returns the parent environment, which is the global environment.
d. Assigning Variables to an Environment:
You can assign variables to a specific environment using the assign()
function.
assign("z", 30, envir = new_env)
e. Searching for Variables in an Environment:
Use the ls()
function to list variables in an environment.
ls(envir = new_env) # Lists the variables in new_env
When you create a function in R, it gets associated with an environment. By default, this is the environment where the function was created. This is crucial for understanding how variable lookup works inside a function.
create_function <- function() { var <- "Hello from create_function!" inner_function <- function() { print(var) } return(inner_function) } func <- create_function() func() # Prints "Hello from create_function!"
In this example, inner_function
is defined inside create_function
, so its environment is the body of create_function
. When inner_function
looks for var
, it finds it in this enclosing environment.
Every R package has its own environment. When you load a package with library()
, you attach its environment to the search path. This is how functions from packages are made available.
You can view the sequence of environments using the search()
function. This shows the search path R uses to find objects.
search()
Environments in R are fundamental for understanding the scoping rules, especially when dealing with functions, packages, and namespaces. A clear understanding of environments will help you avoid potential pitfalls, especially when designing more complex functions or packages.
Environments in R programming:
# Creating a new environment my_env <- new.env() # Assigning a value in the environment my_env$x <- 42 # Accessing the value print(my_env$x)
Working with environments in R:
# Accessing values in an environment value <- my_env$x # Modifying values in the environment my_env$x <- 100 # Creating a nested environment nested_env <- new.env(parent = my_env)
Scope and visibility in R environments:
# Creating variables in different environments global_env <- globalenv() local_env <- new.env(parent = global_env) global_env$global_var <- "I'm global!" local_env$local_var <- "I'm local!" # Accessing variables from different environments print(global_var) print(local_var)
Global and local environments in R:
# Accessing the global environment print(ls()) # Creating a local environment local_env <- new.env() # Accessing the local environment with(local_env, print(ls()))
Creating and modifying environments in R:
# Creating a new environment new_env <- new.env() # Adding variables to the environment new_env$x <- 10 new_env$y <- 20 # Modifying a variable in the environment new_env$x <- new_env$x + 5 # Removing a variable from the environment rm("y", envir = new_env)
Lexical scoping and environments in R:
# Define a function with lexical scoping create_function <- function() { x <- 5 function() { return(x) } } # Create and call the function my_function <- create_function() print(my_function())
R search path and environments:
# Accessing the search path print(search())
Encapsulation with environments in R:
# Define an encapsulated environment create_encapsulation <- function() { private_env <- new.env() # Add private variables private_env$x <- 42 # Define methods get_x <- function() { return(private_env$x) } # Return a list of methods return(list(get_x = get_x)) } # Create and use the encapsulated environment my_encapsulation <- create_encapsulation() print(my_encapsulation$get_x())