-
Notifications
You must be signed in to change notification settings - Fork 8
/
02-funprog.Rmd
673 lines (507 loc) · 14.5 KB
/
02-funprog.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
---
title: "Part II: Functional programming"
author: "Laurent Gatto"
---
## Content
- Functions
- Robust programming with functions
- Scoping
- Closures
- High-level functions
- Vectorisation
# Functions
Among the R's strong points, Hadley Whickham cites:
> [R has] a strong foundation in functional programming. The ideas of
> functional programming are well suited to solving many of the
> challenges of data analysis. R provides a powerful and flexible
> toolkit which allows you to write concise yet descriptive code.
Also
> To understand computations in R, two slogans are helpful:
> - Everything that exists is an object.
> - Everything that happens is a function call.
> John Chambers
![Messy code hides bugs](./figs/funs.png)
- Functions are a means of **abstraction**. A concept/computation is
encapsulated/isolated from the rest with a function.
- Functions should **do one thing**, and do it well (compute, or plot,
or save, ... not all in one go).
- **Side effects**: your functions should not have any (unless, of
course, that is the main point of that function - plotting, write to
disk, ...). Functions shouldn't make any changes in any
environment. The only return their output.
- **Do not use global variables**. Everything the function needs is
being passed as an argument. Function must be **self-contained**.
- Function streamline code and process
From the `R Inferno`:
Make your functions as simple as possible. Simple has many advantages:
- Simple functions are likely to be human efficient: they will be easy
to understand and to modify.
- Simple functions are likely to be computer efficient.
- Simple functions are less likely to be buggy, and bugs will be
easier to fix.
- (Perhaps ironically) simple functions may be more general—thinking
about the heart of the matter often broadens the application.
Functions can be
1. Correct.
2. An error occurs that is clearly identified.
3. An obscure error occurs.
4. An incorrect value is returned.
We like category 1. Category 2 is the right behavior if the inputs do
not make sense, but not if the inputs are sensible. Category 3 is an
unpleasant place for your users, and possibly for you if the users
have access to you. Category 4 is by far the worst place to be - the
user has no reason to believe that anything is wrong. Steer clear of
category 4.
Finally, functions are
- Easier to debug (part III)
- Easier to profile (part IV)
- Easier to parallelise (part IV)
Functions are an central part of robust R programming.
## Function parts
A function is made of
- a name
- some inputs (formal parameters)
- a single output (return value)
- a body
- an environment, the map of the location of the functions variable
```{r, eval=FALSE}
f <- function(x) {
y <- x + 1
return(x * y)
}
```
And these can be accessed and modified indivdually
```{r, eval=FALSE}
body(f)
args(f)
environment(f)
body(f) <- quote({
y <- x * y
return(x + y)
})
```
## Lexical scoping
- If a name is not found in a functions environment, it is looked up
in the parent (enclosing) from.
- If it is not found in the parent (enclosing) frame, it is looked up
in the parent's parent frame, and so on...
*Lexical scoping*: default behaviour, current environment, then
traversing *enclosing/parent environments*.
```{r, eval=FALSE}
f <- function(x) x + y
f(1)
environment(f)
y <- 2
f(1)
```
```{r, eval=FALSE}
e <- new.env()
environment(f) <- e
f(1)
e$y <- 10
f(1)
```
This is of course bad practice, we don't want to rely on global variables.
```{r, eval=FALSE}
codetools::findGlobals(f)
```
## Exercises
Start by mentally running the code chunks below - what do the functions return?
After testing new code chunks, don't forget to clean up your
workspace, to avoid unexpected results.
```{r, eval=FALSE}
f <- function() {
x <- 1
y <- 2
c(x, y)
}
f()
```
```{r, eval=FALSE}
x <- 2
g <- function(){
y <- 1
c(x, y)
}
g()
```
```{r, eval=FALSE}
x <- 1
h <- function() {
y <- 2
i <- function() {
z <- 3
c(x, y, z)
}
i()
}
h()
```
```{r, eval=FALSE}
j <- function(x) {
y <- 2
function(){
c(x, y)
}
}
k <- j(1)
k()
```
```{r, eval=FALSE}
j <- function() {
if (!exists("a")) {
a <- 1
} else {
a <- a + 1
}
print(a)
}
j() ## First call
j() ## Second call
```
<!-- ```{r, eval=FALSE} -->
<!-- f <- function() x -->
<!-- x <- 1 -->
<!-- f() -->
<!-- x <- 2 -->
<!-- f() -->
<!-- ``` -->
```{r, eval=FALSE}
f <- function(x) {
f <- function(x) {
f <- function(x) {
x^2
}
f(x) + 1
}
f(x) * 2
}
f(10)
```
## More about functions
- Argument matching by position or by names
- Calling a function with a list of arguments
```{r, eval=FALSE}
args <- list(x = 1:10, trim = 0.3)
do.call(mean, args)
```
- Default arguments
```{r, eval=FALSE}
f <- function(x = 1, y = 2) x * y
f <- function(x = 1, y = x + 2) x * y
```
- Missing arguments
```{r, eval=FALSE}
f <- function(x = 1, y) {
c(missing(x), missing(y))
}
f()
f(x = 1)
```
- Passing non-matched parameters `...` to an inner function
```{r, eval=FALSE}
plot2 <- function(...) {
message("Verbose plotting...")
plot(...)
}
f <- function(...) list(...)
```
- Return values: last statement, explicit `return`, make output
`invisible`
```{r, eval=FALSE}
f1 <- function() 1
f2 <- function() return(1)
f3 <- function() return(invisible(1))
```
- Explicit triggers before exiting. Useful to restore global state
(plotting parameters, cleaning temporary files, ...)
```{r, eval=FALSE}
f1 <- function(x) {
on.exit(print("!"))
x + 1
}
f2 <- function(x) {
on.exit(print("!"))
stop("Error")
}
```
```{r, eval=FALSE}
f3 <- function() {
on.exit(print("1"))
on.exit(print("2"))
invisible(TRUE)
}
f4 <- function() {
on.exit(print("1"))
on.exit(print("2"), add = TRUE)
invisible(TRUE)
}
```
- Anonymous functions, created on-the-flight and passed to `lapply` or
other high-level functions.
```{r, eval=FALSE}
function(x) x + y
body(function(x) x + y)
args(function(x) x + y)
environment(function(x) x + y)
```
## More about scoping
*Lexical scoping*: default behaviour, current environment, then
traversing *enclosing/parent environments*.
*Dynamic scoping*: looking up variables in the *calling environment*,
used in non-standard evaluation.
# Functional programming
**First-class functions** - a function is a value just like any other
variable. Functions can thus be used as arguments to other
functions. Functions are considered *first-class citizens*.
**Higher-order functions** - refers to functions that take functions
as parameters (input) or return functions (output).
## Closures
> "An object is data with functions. A closure is a function with
> data." - John D. Cool
Closures: functions written by functions. They enclose the envionment
of the parent function and can access all its variables.
```{r, eval=FALSE}
make.power <- function(n)
function(x) x^n
cube <- make.power(3)
square <- make.power(2)
cube(2)
square(2)
environment(cube)
environment(square)
```
Mutable state: a counter function
```{r, eval=FALSE}
new_counter <- function() {
i <- 0
function() {
i <<- i + 1
i
}
}
count1 <- new_counter()
count2 <- new_counter()
count1()
count1()
count2()
environment(count1)
environment(count2)
environment(count1)$i
environment(count2)$i
```
Questions:
- What happens of we place the code `i <- 0` and the function
definition outside of a function, i.e in the global environment?
- What happens if we use `<-` instead of `<<-`?
The `colorRampPallette`
```{r, eval=FALSE}
colramp <- colorRampPalette(c("blue", "yellow"))
colramp(5)
plot(1:10, col = colramp(10), pch = 19, cex = 2,
main = "colramp(10)")
```
## Functional
Take a function as input or return a function as output.
- `Reduce(f, x)` uses a binary function to successively combine the
elements of a given vector and a possibly given initial value.
```{r, eval=FALSE}
L <- replicate(3, matrix(rnorm(9), 3), simplify = FALSE)
Reduce("+", L)
try(sum(L))
```
```{r, eval=FALSE}
Reduce("+", list(1, 2, 3), init = 10)
Reduce("+", list(1, 2, 3), accumulate = TRUE)
Reduce("+", list(1, 2, 3), right = TRUE, accumulate = TRUE)
```
- `Filter(f, x)` extracts the elements of a vector for which a
predicate (logical) function gives true.
- `Negate(f)` creates the negation of a given function.
```{r, eval=FALSE}
even <- function(x) x %% 2 == 0
(y <- sample(100, 10))
Filter(even, y)
Filter(Negate(even), y)
```
- `Map(f, ...)` applies a function to the corresponding elements of
given vectors. Similar to `mapply` without any attempt to simplify.
```{r, eval=FALSE}
Map(even, 1:3)
```
- `Find(f, x)` and `Position(f, x)` give the first (or last elements)
and its position in the vector, for which a predicate (logical)
function gives true.
```{r, eval=FALSE}
Find(even, 10:15)
Find(even, 10:15, right = TRUE)
Position(Negate(even), 10:15)
Position(Negate(even), 10:15, right = TRUE)
```
## References
- R Gentleman, *R Programming for Bioinformatics*, CRC Press, 2008
- `?Map`, or any other of the higher order functions
- Blog post, *Higher Order Functions in R*, John Myles White
http://www.johnmyleswhite.com/notebook/2010/09/23/higher-order-functions-in-r/
# Vectorisation
> Many operations in R are vectorized, and understanding and using
> vectorization is an essential component of becoming a proficient
> programmer. - R Gentleman in *R Programming for Bioinformatics*.
A *vectorised computation* is one that, when applied to a vector (of
length greater than 1), automatically operates directly on all
elements of the input vector.
```{r, eval=FALSE}
(x <- 1:5)
(y <- 5:1)
x + y
```
## Recycling rule
What is `x` and `y` are of different length: the shorter vector is
replicate so that its length matches the longer ones.
```{r, eval=FALSE}
(x <- 1:6)
(y <- 1:2)
x+y
```
If the shorter vector is not an even multiple of the longer, a warning
is issued.
## Example
Compute difference between times of events, `e`. Given `n` events,
there will be `n-1` inter-event times. `interval[i] <- e[i+1] - e[i]`
Procedural implementation:
```{r, eval=FALSE}
diff1 <- function(e) {
n <- length(e)
interval <- rep(0, n - 1)
for (i in 1:(n - 1))
interval[i] <- e[i + 1] - e[i]
interval
}
e <- c(2, 5, 10.2, 12, 19)
diff1(e)
```
Vectorised implementation:
```{r, eval=FALSE}
diff2 <- function(e) {
n <- length(e)
e[-1] - e[-n]
}
e <- c(2, 5, 10.2, 12, 19)
diff2(e)
```
## `*apply` functions
How to apply a function, iteratively, on a set of elements?
`apply(X, MARGIN, FUN, ...)`
- `MARGIN` = 1 for row, 2 for cols.
- `FUN` = function to apply
- `...` = extra args to function.
- `simplify` = should the result be simplified if possible.
`*apply` functions are (generally) **NOT** faster than loops, but more
succint and thus clearer.
```{r, eval=FALSE}
v <- rnorm(1000) ## or a list
res <- numeric(length(v))
for (i in 1:length(v))
res[i] <- f(v[i])
res <- sapply(v, f)
## if f is vectorised
f(v)
```
function | use case
-------|---------------------------------------
apply | matrices, arrays, data.frames
lapply | lists, vectors
sapply | lists, vectors
vapply | with a pre-specified type of return value
tapply | atomic objects, typically vectors
by | similar to tapply
eapply | environments
mapply | multiple values
rapply | recursive version of lapply
esApply | `ExpressionSet`, defined in `Biobase`
See also the `BiocGenerics` package for `[l|m|s|t]apply` S4 generics,
as well as parallel versions in the `parallel` package (see
`Performance` section).
See also the `plyr` package, that offers its own flavour of `apply`
functions.
in/out | list | data frame | array
------------|---------|------------|---------
list | llply() | ldply() | laply()
data frame | dlply() | ddply() | daply()
array | alply() | adply() | aaply()
## Other functions
- `replicate` - repeated evaluation of an expression
- `aggregate` - compute summary statistics of data subsets
- `ave` - group averages over level combinations of factors
- `sweep` - sweep out array summaries
## Anonymous functions
A function defined/called without being assigned to an identifier and
generally passed as argument to other functions.
```{r, eval=FALSE}
M <- matrix(rnorm(100), 10)
apply(M, 1, function(Mrow) 'do something with Mrow')
apply(M, 2, function(Mcol) 'do something with Mcol')
```
## Use case: integration
```{r, echo=FALSE, eval=FALSE}
f <- function(x, a = 1) sin(x^2)/ (a + abs(x))
x <- seq(-7, 7, 0.02 )
x0 <- seq(-2, 2, 0.02)
y0 <- f(x0)
y0[y0 < 0] <- 0
plot(x, f(x), type = "l", main = expression(f(x) == frac(sin(x^2),(a + abs(x)))))
grid()
abline(v = c(-2, 2), lty = "dotted")
polygon(x0, y0, col = "#00000010")
```
![`sin(x^2)/ (a + abs(x))`](./figs/sinfun.png)
The `integrate` function approximates definite integrals by
adaptive quadrature.
```{r, eval=FALSE}
f <- function(x, a = 1) sin(x^2)/ (a + abs(x))
integrate(f, lower = -2, upper = 2)
```
It is not vectorised.
```{r, eval=FALSE}
lo <- c(-2, 0)
hi <- c(0, 2)
integrate(f, lower = lo, upper = hi)
```
## How to vectorise
- To vectorise a function, we can explicitly wrap it inside a helper
function that will take care of argument recycling (via `rep`), then
loop over the inputs and call the non-vectorised function.
- To vectorise a function, we can explicitate the vectorised
calculation using `mapply`.
```{r, eval=FALSE}
mapply(function(lo, hi) integrate(f, lo, hi)$value,
lo, hi)
```
- Create a vectorised form using `Vectorize`. It takes a function
(here, an anonymous function) as input and returns a function.
```{r, eval=FALSE}
Integrate <- Vectorize(
function(fn, lower, upper)
integrate(fn, lower, upper)$value,
vectorize.args=c("lower", "upper")
)
Integrate(f, lower=lo, upper=hi)
```
## **Efficient** apply-like functions
These functions combine high-level vectorised syntax for clarity
**and** efficient C-level vectorised imputation (see *Performance*
section).
- In `base`: rowSums, rowMeans, colSums, colMeans
- In `Biobase`: rowQ, rowMax, rowMin, rowMedias, ...
- In `genefilter`: rowttests, rowFtests, rowSds, rowVars, ...
Generalisable on other data structures, like `ExpressionSet`
instances.
## Parallelisation
Vectorised operations are natural candidats for parallel execution.
See later, *Parallel computation* topic.
## References
- R Gentleman, *R Programming for Bioinformatics*, CRC Press, 2008
- Ligges and Fox, *R Help Desk, How Can I Avoid This Loop or Make It
Faster?* R News, Vol 8/1. May 2008.
- Grouping functions: sapply vs. lapply vs. apply. vs. tapply
vs. by vs. aggregate ... http://stackoverflow.com/questions/3505701/