Classes & Environments

Taken from Advanced R programming by Hadley Wickham

Reference Classes

Reference classes (or RC for short) are the newest OO system in R. They were introduced in version 2.12. They are fundamentally different to S3 and S4 because: RC methods belong to objects, not functions RC objects are mutable: the usual R copy-on-modify semantics do not apply These properties make RC objects behave more like objects do in most other programming languages, e.g., Python, Ruby, Java and C#. ref

Account <- setRefClass("Account")  # a new class
my.account <- Account$new()  # a new object

Classes have attributes:

Account <- setRefClass("Account", fields = list(balance = "numeric"))

my.account <- Account$new(balance = 100)
my.account$balance
## [1] 100
my.account$balance <- 200
my.account$balance
## [1] 200

Unlike other R objects, RC objects are mutable and are not copied when modified:

b <- my.account
b$balance
## [1] 200
my.account$balance <- 0
b$balance
## [1] 0
# for a copy:
c <- my.account$copy()
my.account$balance <- 100
c$balance
## [1] 0

And we can add methods also:

Account <- setRefClass("Account", fields = list(balance = "numeric"), methods = list(withdraw = function(x) {
    balance <<- balance - x  # use <<- to modify attributes 
}, deposit = function(x) {
    balance <<- balance + x
}))

a <- Account$new(balance = 100)
a$deposit(100)
a$balance
## [1] 200

For subclassing use contains:

NoOverdraft <- setRefClass("NoOverdraft", contains = "Account", methods = list(withdraw = function(x) {
    # overwrites superclass 'withdraw'
    if (balance < x) stop("Not enough money")
    balance <<- balance - x
}))
accountJohn <- NoOverdraft$new(balance = 100)
accountJohn$deposit(50)
accountJohn$balance
## [1] 150
accountJohn$withdraw(200)
## Error: Not enough money

Environments

The job of an environment is to associate, or bind, a set of names to a set of values. Environments are the data structures that power scoping. An environment is very similar to a list, with three important exceptions:

1.Environments have reference semantics. So R's usual copy on modify rules do not apply. Whenever you modify an environment, you modify every copy. In the following code chunk, we create a new environment, create a “copy” and then modify the original environment. Notice that the copy also changes. If you change e to a list (or any other R datastructure), f will become a list. e and f are identical.

e <- new.env()
f <- e

e$a <- 10
f$a
## [1] 10

As well as powering scoping, environments can also be useful data structures because they have reference semantics and can work like a hashtable.

  1. Environments have parents. If an object is not found in an environment, then R will look at its parent (and so on). There is only one exception: the empty environment does not have a parent.
e <- new.env()
# the default parent provided by new.env() is environment from which it is
# called
parent.env(e)
## <environment: R_GlobalEnv>
  1. Every object in an environment must have a name. And, those names must be unique.

Technically, an environment is made up of a frame, a collection of named objects (like a list), and a reference to a parent environment. ref

e <- new.env()
identical(e, globalenv())
## [1] FALSE
e$a <- 1
ls(e)  # check contents of e (does not show names beginning with .)
## [1] "a"
e$a
## [1] 1
e$.b <- 0
ls(e)
## [1] "a"
ls(e, all = TRUE)
## [1] ".b" "a"
# we can coerce to a list:
as.list(e)
## $a
## [1] 1
str(as.list(e, all = TRUE))
## List of 2
##  $ a : num 1
##  $ .b: num 0

We can use $ or [[ to extract an element which looks only within the environment. Function get will also looks into its parents:

e$a
## [1] 1
e[["a"]]
## [1] 1
c <- 1  # global env
e$c
## NULL
get("c", e)
## [1] 1

To remove an element use rm:

e <- new.env()
e$a <- 1
e$a <- NULL
ls(e)
## [1] "a"
rm("a", envir = e)
ls(e)
## character(0)

Generally, when you create your own environment, you want to manually set the parent environment to the empty environment. This ensures you don't accidentally inherit objects from somewhere else:

x <- 1
e1 <- new.env()
get("x", e1)
## [1] 1
e2 <- new.env(parent = emptyenv())
get("x", e2)
## Error: object 'x' not found

You can determine if a binding exists in a environment with the exists() function. Like get(), the default is to follow regular scoping rules and look in parent environments. If you don't want this behavior, use inherits = FALSE:

exists("x", e1)
## [1] TRUE
exists("x", e1, inherits = FALSE)
## [1] FALSE

There are a few special environments that you can access directly:

search()  #  lists all environments between and including the global and base environments
##  [1] ".GlobalEnv"        "package:knitr"     "package:stats"    
##  [4] "package:graphics"  "package:grDevices" "package:utils"    
##  [7] "package:datasets"  "package:methods"   "Autoloads"        
## [10] "package:base"

Function environments

The environment where the function is created:

y <- 1
f <- function(x) x + y
environment(f)
## <environment: R_GlobalEnv>

To make an equivalent function that is safer (it throws an error if the input isn't a function), more consistent (it can take a function name as an argument not just a function), and more informative (it has a better name), we'll create funenv():

funenv <- function(f) {
    f <- match.fun(f)  #  extract the desired function object
    environment(f)
}

funenv(plot)
## <environment: namespace:graphics>
environment(plot)
## <environment: namespace:graphics>

funenv("plot")
## <environment: namespace:graphics>
environment("plot")
## NULL

The environment created when a function is run: each time a function is called, a new environment is created to host execution.

f <- function(x) {
    list(e = environment(), p = parent.env(environment()))
}
str(f())
## List of 2
##  $ e:<environment: 0x046c4c0c> 
##  $ p:<environment: R_GlobalEnv>
str(f())
## List of 2
##  $ e:<environment: 0x046d3458> 
##  $ p:<environment: R_GlobalEnv>
funenv("f")
## <environment: R_GlobalEnv>

The environment where the function is called:

f <- function() {
    x <- 10
    function() {
        x
    }
}
g <- f()
x <- 20
g()
## [1] 10

What value x is associated with in the environment where g() is called? x is 10 in the environment where g() is defined, but it is 20 in the environment where g() is called.

We can access this environment using the confusingly named parent.frame(). This function returns the environment where the function is called. We can also use this function to look up the value of names in that environment:

f2 <- function() {
    x <- 10
    function() {
        def <- get("x", environment())
        cll <- get("x", parent.frame())
        list(defined = def, called = cll)
    }
}
g2 <- f2()
x <- 20
str(g2())
## List of 2
##  $ defined: num 10
##  $ called : num 20

We can get a list of all calling environments using sys.frames():

x <- 0
y <- 10
f <- function(x) {
    x <- 1
    g(x)
}
g <- function(x) {
    x <- 2
    h(x)
}
h <- function(x) {
    x <- 3
    i(x)
}
i <- function(x) {
    x <- 4
    sys.frames()
}

es <- f()
es
## [[1]]
## <environment: 0x0338d758>
## 
## [[2]]
## <environment: 0x0390a534>
## 
## [[3]]
## <environment: 0x03c2e410>
## 
## [[4]]
## <environment: 0x03c2d7fc>
## 
## [[5]]
## <environment: 0x03c2d9f4>
## 
## [[6]]
## <environment: 0x03c2db44>
## 
## [[7]]
## <environment: 0x03be6a60>
## 
## [[8]]
## <environment: 0x042f9470>
## 
## [[9]]
## <environment: 0x042f9b28>
## 
## [[10]]
## <environment: 0x04068464>
## 
## [[11]]
## <environment: 0x03f1cb34>
## 
## [[12]]
## <environment: 0x03f17450>
## 
## [[13]]
## <environment: 0x03f1754c>
## 
## [[14]]
## <environment: 0x03f143f0>
## 
## [[15]]
## <environment: 0x03f14594>
## 
## [[16]]
## <environment: 0x03f13be8>
## 
## [[17]]
## <environment: 0x03f0da1c>
## 
## [[18]]
## <environment: 0x03f0e03c>
## 
## [[19]]
## <environment: 0x03f0e18c>
## 
## [[20]]
## <environment: R_GlobalEnv>
## 
## [[21]]
## <environment: 0x03f055a0>
## 
## [[22]]
## <environment: 0x03f0562c>
## 
## [[23]]
## <environment: 0x03effe4c>
## 
## [[24]]
## <environment: 0x03efff10>
sapply(es, function(e) get("x", e, inherits = TRUE))
## [[1]]
## [1] 0
## 
## [[2]]
## [1] 0
## 
## [[3]]
## [1] 0
## 
## [[4]]
## $params
## $params$label
## [1] "unnamed-chunk-19"
## 
## 
## $params.src
## [1] ""
## 
## attr(,"class")
## [1] "block"
## 
## [[5]]
## $params
## $params$label
## [1] "unnamed-chunk-19"
## 
## 
## $params.src
## [1] ""
## 
## attr(,"class")
## [1] "block"
## 
## [[6]]
## [1] 0
## 
## [[7]]
## [1] 0
## 
## [[8]]
## [1] 0
## 
## [[9]]
## [1] 0
## 
## [[10]]
## [1] 0
## 
## [[11]]
## [1] 0
## 
## [[12]]
## [1] 0
## 
## [[13]]
## [1] 0
## 
## [[14]]
## [1] 0
## 
## [[15]]
## [1] 0
## 
## [[16]]
## [1] 0
## 
## [[17]]
## [1] 0
## 
## [[18]]
## [[18]][[1]]
## <environment: 0x0338d758>
## 
## [[18]][[2]]
## <environment: 0x0390a534>
## 
## [[18]][[3]]
## <environment: 0x03c2e410>
## 
## [[18]][[4]]
## <environment: 0x03c2d7fc>
## 
## [[18]][[5]]
## <environment: 0x03c2d9f4>
## 
## [[18]][[6]]
## <environment: 0x03c2db44>
## 
## [[18]][[7]]
## <environment: 0x03be6a60>
## 
## [[18]][[8]]
## <environment: 0x042f9470>
## 
## [[18]][[9]]
## <environment: 0x042f9b28>
## 
## [[18]][[10]]
## <environment: 0x04068464>
## 
## [[18]][[11]]
## <environment: 0x03f1cb34>
## 
## [[18]][[12]]
## <environment: 0x03f17450>
## 
## [[18]][[13]]
## <environment: 0x03f1754c>
## 
## [[18]][[14]]
## <environment: 0x03f143f0>
## 
## [[18]][[15]]
## <environment: 0x03f14594>
## 
## [[18]][[16]]
## <environment: 0x03f13be8>
## 
## [[18]][[17]]
## <environment: 0x03f0da1c>
## 
## [[18]][[18]]
## <environment: 0x03f0e03c>
## 
## [[18]][[19]]
## <environment: 0x03f0e18c>
## 
## [[18]][[20]]
## <environment: R_GlobalEnv>
## 
## [[18]][[21]]
## <environment: 0x03f055a0>
## 
## [[18]][[22]]
## <environment: 0x03f0562c>
## 
## [[18]][[23]]
## <environment: 0x03effe4c>
## 
## [[18]][[24]]
## <environment: 0x03efff10>
## 
## 
## [[19]]
## [1] 0
## 
## [[20]]
## [1] 0
## 
## [[21]]
## [1] 1
## 
## [[22]]
## [1] 2
## 
## [[23]]
## [1] 3
## 
## [[24]]
## [1] 4
sapply(es, function(e) get("y", e, inherits = TRUE))
##  [1] 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
## [24] 10

Assignment

Assignment is the act of binding (or rebinding) a name to a value in an environment. It is the counterpart to scoping, the set of rules that determines how to find the value associated with a name.

There are four types of binding:

Regular Binding:

a <- 1
`a+b` <- 2  # a name can actually be any sequence of characters
a + `a+b`
## [1] 3
`:)` <- "smile"
paste0(`:)`, "!")
## [1] "smile!"

e <- new.env()
assign("a", 1, envir = e)  # same as e$a <- 1

e <- new.env()
eval(quote(a <- 2), e)  # Evaluate an R expression in a specified environment
# quote simply returns its argument, which is not evaluated then
e$a
## [1] 2

Constants:

x <- 10
lockBinding("x", globalenv())
x <- 15
## Error: cannot change value of locked binding for 'x'
rm(x)

<<-

The regular assignment arrow, <-, always creates a variable in the current environment. The special assignment arrow, <<-, never creates a variable in the current environment, but instead modifies an existing variable found by walking up the parent environments.

x <- 1
inc <- function() x <<- x + 1
inc()
x
## [1] 2
inc()
x
## [1] 3

Delayed bindings:

Another special type of assignment is a delayed binding: rather than assigning the result of an expression immediately, it creates and stores a promise to evaluate the expression when needed (much like the default lazy evaluation of arguments in R functions).



```r
makeActiveBinding("x1", (function() runif(1)), environment())
x1
## [1] 0.8858
x1
## [1] 0.8595

f <- local({
    x <- 1
    function(val) {
        # with 1 argument is a 'set', with 0 args is a 'get'
        if (missing(val)) 
            2^x else x <<- val
    }
})

makeActiveBinding("pow2", f, environment())
pow2
## [1] 2
pow2 <- 3
pow2
## [1] 8
pow2 <- 4
pow2
## [1] 16