+ - 0:00:00
Notes for current slide
Notes for next slide

Advanced R

Chapter 7 Environments

Kevin Kent

1 / 21

r4ds

"f() binds the environment that binds the name f to the function. But that’s not always the case: in the following example g is bound in a new environment e, but g() binds the global environment."

r4ds

2 / 21

??????

r4ds

3 / 21

Quick Recap - Chapter 6

Environments power lexical scooping

Lexical scooping

  • Name masking
  • Functions versus variables
  • A fresh start
  • Dynamic lookup

r4ds

r4ds

4 / 21

What is an R environment?

"The job of an environment is to associate, or bind, a set of names to a set of values."

  • A data structure similar to a named list, except:
    • Every name must be unique.
    • The names in an environment are not ordered.
    • An environment has a parent.
    • Environments are not copied when modified.
  • R uses environments to find the objects and packages you call
  • They are hierarchical (parents and children)
  • Environments can contain themselves
library(rlang)
## Warning: package 'rlang' was built under R version 3.6.2
e1 <- env(
a = FALSE,
b = "a",
c = 2.3,
d = 1:3,
)

r4ds

5 / 21

Types of Environments

  • Current Environment
  • Global
  • Special types
    • Package
    • Function
    • Namespaces
    • Execution
6 / 21

Environments are hierarhical

Every environment has a parent (almost)

r4ds

7 / 21

Environments are hierarhical

Every environment has a parent (almost)

r4ds

r4ds

8 / 21

Looking for objects

r4ds

r4ds

9 / 21

Iteracting with Environments

Getting and Setting

e3 <- env(x = 1, y = 2)
e3$z <- 3
e3[["z"]]
## [1] 3
## Binding with Poke
env_poke(e3, "a", 100)
## Multiple bindings
env_bind(e3, a = 10, b = 20)
10 / 21

Special Environments

  • Package
  • Function
  • Namespaces
  • Execution
11 / 21

Special Environments

Package

r4ds

search()
## [1] ".GlobalEnv" "package:rlang"
## [3] "package:xaringanthemer" "package:stats"
## [5] "package:graphics" "package:grDevices"
## [7] "package:utils" "package:datasets"
## [9] "package:methods" "Autoloads"
## [11] "package:base"
12 / 21

Special Environments

Function

"A function binds the current environment when it is created. This is called the function environment, and is used for lexical scoping. Across computer languages, functions that capture (or enclose) their environments are called closures, which is why this term is often used interchangeably with function in R’s documentation."

13 / 21

Special Environments

Namespaces

"The goal of namespaces is to make sure that...every package works the same way regardless of what packages are attached by the user."

r4ds

14 / 21

Special Environments

Namespaces

r4ds

15 / 21

Special Environments

Execution

  • Each time a function is called, an execution environment is created
  • Parent is the function environment
  • Cleaned up by garbage collector after execution is completed (unless execution environment is explicitly returned)
16 / 21

Special Environments

Execution

rds:scale 5%

17 / 21

Call stacks

A call stack is a record of the functions that were executed in a particular order. Each element of a call stack (or step), is a frame.

f <- function(x) {
g(x = 2)
}
g <- function(x) {
h(x = 3)
}
h <- function(x) {
stop()
}
# f(x = 1)
# #> Error:
# traceback()
# #> 4: stop()
# #> 3: h(x = 3)
# #> 2: g(x = 2)
# #> 1: f(x = 1)
18 / 21

Call stacks

h <- function(x) {
lobstr::cst()
}
f(x = 1)
## █
## 1. └─global::f(x = 1)
## 2. └─global::g(x = 2)
## 3. └─global::h(x = 3)
## 4. └─lobstr::cst()
19 / 21

Questions?

20 / 21
21 / 21

r4ds

"f() binds the environment that binds the name f to the function. But that’s not always the case: in the following example g is bound in a new environment e, but g() binds the global environment."

r4ds

2 / 21
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow