ls()character(0)
The information on this page is largely optional. It’s good to have some awareness of R’s global environment and how R stores the objects you create, but you can safely progress to the next module without mastering these concepts.
Every R command is executed in a particular environment. Conceptually, you can think of R environments as collections of the objects that R currently has stored in working memory. For most day-to-day R usage, you only need to consider one of R’s several types of environment: the Global Environment.
The global environment is the highest-level environment that R maintains. This environment contains all of the objects you define in your R session. Whenever you create a new object (as you’ve been doing throughout this module), that object lives in the global environment.
Since the global environment is the one you’ll interact with most frequently, its often treated as the default environment when economizing language (i.e., being lazy) or picking software defaults.
We can use the ls() function to list the contents of the current environment.
In this case, the environment is empty because we haven’t yet defined any objects.
Do: Run the following code to add some objects to the current environment.
Do: Use the interactive editor below to run the ls() function. What do you see?
You should now see a listing of all the objects you just created. Remember: whenever we create a new object, we’re storing that object in the global environment.
The rm() function will remove an object from the environment.
Do: Run the following code.
See: What effect did rm(x) have?
The listing returned by ls() no longer shows x, because you removed x from the environment by calling rm(x).
ls() function to view the contents of the environment.rm() function to remove age from the environment.ls() to check your work.You’re ready to progress if you can:
If any of these concepts still feel shaky, that’s completely fine. Revisit the relevant section before proceeding.
Take the Knowledge Quiz to check your learning.