-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathRClasses.Rmd
357 lines (282 loc) · 9 KB
/
RClasses.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
Classes & Environments
========================================================
Taken from [Advanced R programming](http://adv-r.had.co.nz/) 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](http://adv-r.had.co.nz/OO-essentials.html#rc)
```{r}
Account <- setRefClass("Account") # a new class
my.account <- Account$new() # a new object
```
Classes have attributes:
```{r}
Account <- setRefClass("Account",
fields = list(balance = "numeric"))
my.account <- Account$new(balance = 100)
my.account$balance
my.account$balance <- 200
my.account$balance
```
Unlike other R objects, RC objects are mutable and are not copied when modified:
```{r}
b <- my.account
b$balance
my.account$balance <- 0
b$balance
# for a copy:
c <- my.account$copy()
my.account$balance <- 100
c$balance
```
And we can add methods also:
```{r}
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
```
For subclassing use contains:
```{r}
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
accountJohn$withdraw(200)
```
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.
```{r}
e <- new.env()
f <- e
e$a <- 10
f$a
```
> As well as powering scoping, environments can also be useful data structures because they have reference semantics and can work like a hashtable.
> 2. 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.
```{r}
e <- new.env()
# the default parent provided by new.env() is environment from which it is called
parent.env(e)
```
> 3. 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](http://adv-r.had.co.nz/Environments.html)
```{r}
e <- new.env()
identical(e, globalenv())
e$a <- 1
ls(e) # check contents of e (does not show names beginning with .)
e$a
e$.b <- 0
ls(e)
ls(e, all=TRUE)
# we can coerce to a list:
as.list(e)
str(as.list(e, all=TRUE))
```
We can use $ or [[ to extract an element which looks only within the environment. Function `get` will also looks into its parents:
```{r}
e$a
e[["a"]]
c <- 1 # global env
e$c
get("c",e)
```
To remove an element use `rm`:
```{r}
e <- new.env()
e$a <- 1
e$a <- NULL
ls(e)
rm("a", envir = e)
ls(e)
```
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:
```{r}
x <- 1
e1 <- new.env()
get("x", e1)
e2 <- new.env(parent = emptyenv())
get("x", e2)
```
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:
```{r}
exists("x", e1)
exists("x", e1, inherits = FALSE)
```
There are a few special environments that you can access directly:
+ globalenv(): the user's workspace
+ baseenv(): the environment of the base package
+ emptyenv(): the ultimate ancestor of all environments, the only environment without a parent.
```{r}
search() # lists all environments between and including the global and base environments
```
Function environments
-------------------
The environment where the function is created:
```{r}
y <- 1
f <- function(x) x + y
environment(f)
```
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():
```{r}
funenv <- function(f) {
f <- match.fun(f) # extract the desired function object
environment(f)
}
funenv(plot)
environment(plot)
funenv("plot")
environment("plot")
```
The environment created when a function is run: each time a function is called, a new environment is created to host execution.
```{r}
f <- function(x) {
list(
e = environment(),
p = parent.env(environment())
)
}
str(f())
str(f())
funenv("f")
```
The environment where the function is called:
```{r}
f <- function() {
x <- 10
function() {
x
}
}
g <- f()
x <- 20
g()
```
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:
```{r}
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())
```
We can get a list of all calling environments using sys.frames():
```{r}
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
sapply(es, function(e) get("x", e, inherits = TRUE))
sapply(es, function(e) get("y", e, inherits = TRUE))
```
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:
+ With the regular behaviour, name <- value, the name is immediately associated with the value in the current environment. assign("name", value) works similarly, but allows assignment in any environment.
+ The double arrow, name <<- value, assigns in a similar way to variable lookup, so that i <<- i + 1 modifies the binding of the original i, which is not necessarily in the current environment.
+ Lazy assignment, delayedAssign("name", expression), binds an expression that isn't evaluated until you look up the name.
+ Active assignment, makeActiveBinding("name", function, environment) binds the name to a function, so it is "active" and can return a different value each time the name is found.
Regular Binding:
```{r}
a <- 1
`a+b` <- 2 # a name can actually be any sequence of characters
a + `a+b`
`:)` <- "smile"
paste0(`:)`,"!")
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
```
Constants:
```{r}
x <- 10
lockBinding("x", globalenv())
x <- 15
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.
```{r}
x <- 1
inc <- function() x <<- x+1
inc()
x
inc()
x
```
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
x1
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
pow2 <- 3
pow2
pow2 <- 4
pow2
```