empty environment r

# Step 1: Create an empty environment named 'r'
r <- new.env()

# Step 2: Display the contents of the 'r' environment
ls(environment(r))

# Step 3: Assign a value to a variable 'x' within the 'r' environment
assign("x", 10, envir = r)

# Step 4: Display the contents of the 'r' environment after assigning the value
ls(environment(r))

# Step 5: Retrieve the value of variable 'x' from the 'r' environment
x_value <- get("x", envir = r)
print(x_value)

In this code:

  1. An empty environment named 'r' is created using the new.env() function.
  2. The contents of the 'r' environment are displayed using the ls(environment(r)) function.
  3. A value of 10 is assigned to a variable 'x' within the 'r' environment using the assign() function.
  4. After assigning the value, the contents of the 'r' environment are displayed again to show the addition of the variable 'x'.
  5. The value of variable 'x' is retrieved from the 'r' environment using the get() function, and it is stored in the variable 'x_value'. The value is then printed.