0Pricing
R Academy · Lesson

What Is an Environment in R?

Explore environments as named lists with parent pointers.

What is an Environment?

In R, an environment is a collection of named bindings (like a named list) plus a pointer to a parent environment. Every variable lives in some environment. When R looks up a name, it searches the current environment first, then walks up the parent chain.

# The global environment holds your session variables
x <- 42
cat('x is in globalenv:', exists('x', envir = globalenv()), '\n')
cat('globalenv name:', environmentName(globalenv()), '\n')

globalenv(), baseenv(), and emptyenv()

R has several built-in environments: globalenv() is your interactive workspace, baseenv() holds base R functions, and emptyenv() is the top of the chain with no parent. Every environment chain eventually terminates at emptyenv().

cat('global    :', environmentName(globalenv()),  '\n')
cat('base      :', environmentName(baseenv()),    '\n')
cat('empty     :', environmentName(emptyenv()),   '\n')
cat('parent of global:', environmentName(parent.env(globalenv())), '\n')

All lessons in this course

  1. What Is an Environment in R?
  2. Lexical Scoping Rules
  3. Global vs Local Scope
  4. Creating and Inspecting Environments
← Back to R Academy