-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Functions.Rmd
1388 lines (995 loc) · 43.5 KB
/
Functions.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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Functions
```{r setup, include = FALSE}
source("common.R")
```
## Introduction
\index{functions}
\index{closures|see {functions}}
If you're reading this book, you've probably already created many R functions and know how to use them to reduce duplication in your code. In this chapter, you'll learn how to turn that informal, working knowledge into more rigorous, theoretical understanding. And while you'll see some interesting tricks and techniques along the way, keep in mind that what you'll learn here will be important for understanding the more advanced topics discussed later in the book.
### Quiz {-}
Answer the following questions to see if you can safely skip this chapter. You can find the answers in Section \@ref(function-answers).
1. What are the three components of a function?
1. What does the following code return?
```{r, eval = FALSE}
x <- 10
f1 <- function(x) {
function() {
x + 10
}
}
f1(1)()
```
1. How would you usually write this code?
```{r, eval = FALSE}
`+`(1, `*`(2, 3))
```
1. How could you make this call easier to read?
```{r, eval = FALSE}
mean(, TRUE, x = c(1:10, NA))
```
1. Does the following code throw an error when executed? Why or why not?
```{r, eval = FALSE}
f2 <- function(a, b) {
a * 10
}
f2(10, stop("This is an error!"))
```
1. What is an infix function? How do you write it? What's a replacement
function? How do you write it?
1. How do you ensure that cleanup action occurs regardless of how a function
exits?
### Outline {-}
* Section \@ref(function-fundamentals) describes the basics of creating a
function, the three main components of a function, and the exception
to many function rules: primitive functions (which are implemented in C, not
R).
* Section \@ref(function-composition) discusses the strengths and weaknesses
of the three forms of function composition commonly used in R code.
* Section \@ref(lexical-scoping) shows you how R finds the value associated
with a given name, i.e. the rules of lexical scoping.
* Section \@ref(lazy-evaluation) is devoted to an important property of
function arguments: they are only evaluated when used for the first time.
* Section \@ref(fun-dot-dot-dot) discusses the special `...` argument, which
allows you to pass on extra arguments to another function.
* Section \@ref(exiting-a-function) discusses the two primary ways that a
function can exit, and how to define an exit handler, code that is run on
exit, regardless of what triggers it.
* Section \@ref(function-forms) shows you the various ways in which R
disguises ordinary function calls, and how you can use the standard prefix
form to better understand what's going on.
## Function fundamentals
To understand functions in R you need to internalise two important ideas:
* Functions can be broken down into three components: arguments, body, and environment.
There are exceptions to every rule, and in this case, there is a small selection of "primitive" base functions that are implemented purely in C.
* Functions are objects, just as vectors are objects.
### Function components {#fun-components}
\index{functions!body}
\indexc{body()}
\index{functions!formals}
\index{arguments!formal}
\indexc{formals()}
\index{functions!environment}
\index{environments!of a function}
\indexc{srcref}
A function has three parts:
* The `formals()`, the list of arguments that control how you call the function.
* The `body()`, the code inside the function.
* The `environment()`, the data structure that determines how the function finds
the values associated with the names.
While the formals and body are specified explicitly when you create a function, the environment is specified implicitly, based on _where_ you defined the function. The function environment always exists, but it is only printed when the function isn't defined in the global environment.
```{r}
f02 <- function(x, y) {
# A comment
x + y
}
formals(f02)
body(f02)
environment(f02)
```
I'll draw functions as in the following diagram. The black dot on the left is the environment. The two blocks to the right are the function arguments. I won't draw the body, because it's usually large, and doesn't help you understand the shape of the function.
```{r, echo = FALSE, out.width = NULL}
knitr::include_graphics("diagrams/functions/components.png")
```
Like all objects in R, functions can also possess any number of additional `attributes()`. One attribute used by base R is `srcref`, short for source reference. It points to the source code used to create the function. The `srcref` is used for printing because, unlike `body()`, it contains code comments and other formatting.
```{r}
attr(f02, "srcref")
```
### Primitive functions
\index{primitive functions}
\index{functions!primitive}
\indexc{.Primitive()}
There is one exception to the rule that a function has three components. Primitive functions, like `sum()` and `[`, call C code directly.
```{r}
sum
`[`
```
They have either type `builtin` or type `special`.
```{r}
typeof(sum)
typeof(`[`)
```
These functions exist primarily in C, not R, so their `formals()`, `body()`, and `environment()` are all `NULL`:
```{r}
formals(sum)
body(sum)
environment(sum)
```
Primitive functions are only found in the base package. While they have certain performance advantages, this benefit comes at a price: they are harder to write. For this reason, R-core generally avoids creating them unless there is no other option.
<!-- HW: mention internal functions here too? Cross-reference to perf example -->
### First-class functions {#first-class-functions}
\index{functions!anonymous}
\index{anonymous functions}
It's very important to understand that R functions are objects in their own right, a language property often called "first-class functions". Unlike in many other languages, there is no special syntax for defining and naming a function: you simply create a function object (with `function`) and bind it to a name with `<-`:
```{r}
f01 <- function(x) {
sin(1 / x ^ 2)
}
```
```{r, echo = FALSE, out.width = NULL}
knitr::include_graphics("diagrams/functions/first-class.png")
```
While you almost always create a function and then bind it to a name, the binding step is not compulsory. If you choose not to give a function a name, you get an __anonymous function__. This is useful when it's not worth the effort to figure out a name:
```{r, eval = FALSE}
lapply(mtcars, function(x) length(unique(x)))
Filter(function(x) !is.numeric(x), mtcars)
integrate(function(x) sin(x) ^ 2, 0, pi)
```
A final option is to put functions in a list:
```{r}
funs <- list(
half = function(x) x / 2,
double = function(x) x * 2
)
funs$double(10)
```
In R, you'll often see functions called __closures__. This name reflects the fact that R functions capture, or enclose, their environments, which you'll learn more about in Section \@ref(function-environments).
### Invoking a function
\indexc{do.call()}
You normally call a function by placing its arguments, wrapped in parentheses, after its name: `mean(1:10, na.rm = TRUE)`. But what happens if you have the arguments already in a data structure?
```{r}
args <- list(1:10, na.rm = TRUE)
```
You can instead use `do.call()`: it has two arguments. The function to call, and a list containing the function arguments:
```{r}
do.call(mean, args)
```
We'll come back to this idea in Section \@ref(tidy-dots).
### Exercises
1. Given a name, like `"mean"`, `match.fun()` lets you find a function.
Given a function, can you find its name? Why doesn't that make sense in R?
1. It's possible (although typically not useful) to call an anonymous function.
Which of the two approaches below is correct? Why?
```{r, result = "hide"}
function(x) 3()
(function(x) 3)()
```
1. A good rule of thumb is that an anonymous function should fit on one line
and shouldn't need to use `{}`. Review your code. Where could you have
used an anonymous function instead of a named function? Where should you
have used a named function instead of an anonymous function?
1. What function allows you to tell if an object is a function? What function
allows you to tell if a function is a primitive function?
1. This code makes a list of all functions in the base package.
```{r}
objs <- mget(ls("package:base", all = TRUE), inherits = TRUE)
funs <- Filter(is.function, objs)
```
Use it to answer the following questions:
a. Which base function has the most arguments?
a. How many base functions have no arguments? What's special about those
functions?
a. How could you adapt the code to find all primitive functions?
1. What are the three important components of a function?
1. When does printing a function not show the environment it was created in?
## Function composition {#function-composition}
\index{functions!composition}
\indexc{\%>\%}
\index{magrittr|see {\texttt{\%>\%}}}
\index{piping|see {\texttt{\%>\%}}}
Base R provides two ways to compose multiple function calls. For example, imagine you want to compute the population standard deviation using `sqrt()` and `mean()` as building blocks:
```{r}
square <- function(x) x^2
deviation <- function(x) x - mean(x)
```
You either nest the function calls:
```{r}
x <- runif(100)
sqrt(mean(square(deviation(x))))
```
Or you save the intermediate results as variables:
```{r}
out <- deviation(x)
out <- square(out)
out <- mean(out)
out <- sqrt(out)
out
```
The magrittr package [@magrittr] provides a third option: the binary operator `%>%`, which is called the pipe and is pronounced as "and then".
```{r}
library(magrittr)
x %>%
deviation() %>%
square() %>%
mean() %>%
sqrt()
```
`x %>% f()` is equivalent to `f(x)`; `x %>% f(y)` is equivalent to `f(x, y)`. The pipe allows you to focus on the high-level composition of functions rather than the low-level flow of data; the focus is on what's being done (the verbs), rather than on what's being modified (the nouns). This style is common in Haskell and F#, the main inspiration for magrittr, and is the default style in stack based programming languages like Forth and Factor.
Each of the three options has its own strengths and weaknesses:
* Nesting, `f(g(x))`, is concise, and well suited for short sequences. But
longer sequences are hard to read because they are read inside out and
right to left. As a result, arguments can get spread out over long distances
creating the [Dagwood
sandwich](https://en.wikipedia.org/wiki/Dagwood_sandwich) problem.
* Intermediate objects, `y <- f(x); g(y)`, requires you to name intermediate
objects. This is a strength when objects are important, but a weakness when
values are truly intermediate.
* Piping, `x %>% f() %>% g()`, allows you to read code in straightforward
left-to-right fashion and doesn't require you to name intermediate objects.
But you can only use it with linear sequences of transformations of a single
object. It also requires an additional third party package and assumes that
the reader understands piping.
Most code will use a combination of all three styles. Piping is more common in data analysis code, as much of an analysis consists of a sequence of transformations of an object (like a data frame or plot). I tend to use piping infrequently in packages; not because it is a bad idea, but because it's often a less natural fit.
## Lexical scoping {#lexical-scoping}
\index{scoping!lexical}
In Chapter \@ref(names-values), we discussed assignment, the act of binding a name to a value. Here we'll discuss __scoping__, the act of finding the value associated with a name.
The basic rules of scoping are quite intuitive, and you've probably already internalised them, even if you never explicitly studied them. For example, what will the following code return, 10 or 20?[^answer1]
[^answer1]: I'll "hide" the answers to these challenges in the footnotes. Try solving them before looking at the answer; this will help you to better remember the correct answer. In this case, `g01()` will return `20`.
```{r, results = "hide"}
x <- 10
g01 <- function() {
x <- 20
x
}
g01()
```
In this section, you'll learn the formal rules of scoping as well as some of its more subtle details. A deeper understanding of scoping will help you to use more advanced functional programming tools, and eventually, even to write tools that translate R code into other languages.
R uses __lexical scoping__[^dyn-scope]: it looks up the values of names based on how a function is defined, not how it is called. "Lexical" here is not the English adjective that means relating to words or a vocabulary. It's a technical CS term that tells us that the scoping rules use a parse-time, rather than a run-time structure.
R's lexical scoping follows four primary rules:
* Name masking
* Functions versus variables
* A fresh start
* Dynamic lookup
[^dyn-scope]: Functions that automatically quote one or more arguments can override the default scoping rules to implement other varieties of scoping. You'll learn more about that in Chapter \@ref(evaluation).
### Name masking
\index{functions!scoping}
The basic principle of lexical scoping is that names defined inside a function mask names defined outside a function. This is illustrated in the following example.
```{r}
x <- 10
y <- 20
g02 <- function() {
x <- 1
y <- 2
c(x, y)
}
g02()
```
If a name isn't defined inside a function, R looks one level up.
```{r}
x <- 2
g03 <- function() {
y <- 1
c(x, y)
}
g03()
# And this doesn't change the previous value of y
y
```
The same rules apply if a function is defined inside another function. First, R looks inside the current function. Then, it looks where that function was defined (and so on, all the way up to the global environment). Finally, it looks in other loaded packages.
Run the following code in your head, then confirm the result by running the code.[^answer2]
[^answer2]: `g04()` returns `c(1, 2, 3)`.
```{r, results = "hide"}
x <- 1
g04 <- function() {
y <- 2
i <- function() {
z <- 3
c(x, y, z)
}
i()
}
g04()
```
The same rules also apply to functions created by other functions, which I call manufactured functions, the topic of Chapter \@ref(function-factories).
### Functions versus variables
In R, functions are ordinary objects. This means the scoping rules described above also apply to functions:
```{r}
g07 <- function(x) x + 1
g08 <- function() {
g07 <- function(x) x + 100
g07(10)
}
g08()
```
However, when a function and a non-function share the same name (they must, of course, reside in different environments), applying these rules gets a little more complicated. When you use a name in a function call, R ignores non-function objects when looking for that value. For example, in the code below, `g09` takes on two different values:
```{r}
g09 <- function(x) x + 100
g10 <- function() {
g09 <- 10
g09(g09)
}
g10()
```
For the record, using the same name for different things is confusing and best avoided!
### A fresh start {#fresh-start}
What happens to values between invocations of a function? Consider the example below. What will happen the first time you run this function? What will happen the second time?[^answer4] (If you haven't seen `exists()` before, it returns `TRUE` if there's a variable with that name and returns `FALSE` if not.)
[^answer4]: `g11()` returns `1` every time it's called.
```{r, results = "hide"}
g11 <- function() {
if (!exists("a")) {
a <- 1
} else {
a <- a + 1
}
a
}
g11()
g11()
```
You might be surprised that `g11()` always returns the same value. This happens because every time a function is called a new environment is created to host its execution. This means that a function has no way to tell what happened the last time it was run; each invocation is completely independent. We'll see some ways to get around this in Section \@ref(stateful-funs).
### Dynamic lookup
\indexc{findGlobals()}
Lexical scoping determines where, but not when to look for values. R looks for values when the function is run, not when the function is created. Together, these two properties tell us that the output of a function can differ depending on the objects outside the function's environment:
```{r}
g12 <- function() x + 1
x <- 15
g12()
x <- 20
g12()
```
This behaviour can be quite annoying. If you make a spelling mistake in your code, you won't get an error message when you create the function. And depending on the variables defined in the global environment, you might not even get an error message when you run the function.
To detect this problem, use `codetools::findGlobals()`. This function lists all the external dependencies (unbound symbols) within a function:
```{r}
codetools::findGlobals(g12)
```
To solve this problem, you can manually change the function's environment to the `emptyenv()`, an environment which contains nothing:
```{r, error = TRUE}
environment(g12) <- emptyenv()
g12()
```
The problem and its solution reveal why this seemingly undesirable behaviour exists: R relies on lexical scoping to find _everything_, from the obvious, like `mean()`, to the less obvious, like `+` or even `{`. This gives R's scoping rules a rather beautiful simplicity.
### Exercises
1. What does the following code return? Why? Describe how each of the three
`c`'s is interpreted.
```{r, eval = FALSE}
c <- 10
c(c = c)
```
1. What are the four principles that govern how R looks for values?
1. What does the following function return? Make a prediction before
running the code yourself.
```{r, results = "hide"}
f <- function(x) {
f <- function(x) {
f <- function() {
x ^ 2
}
f() + 1
}
f(x) * 2
}
f(10)
```
## Lazy evaluation {#lazy-evaluation}
\index{evaluation!lazy|see {lazy evaluation}}
\index{lazy evaluation}
\index{functions!lazy evaluation}
In R, function arguments are __lazily evaluated__: they're only evaluated if accessed. For example, this code doesn't generate an error because `x` is never used:
```{r}
h01 <- function(x) {
10
}
h01(stop("This is an error!"))
```
This is an important feature because it allows you to do things like include potentially expensive computations in function arguments that will only be evaluated if needed.
### Promises
\index{promises}
\index{thunks|see {promises}}
Lazy evaluation is powered by a data structure called a __promise__, or (less commonly) a thunk. It's one of the features that makes R such an interesting programming language (we'll return to promises again in Section \@ref(quosures)).
A promise has three components:
* An expression, like `x + y`, which gives rise to the delayed computation.
* An environment where the expression should be evaluated, i.e. the
environment where the function is called. This makes sure that the
following function returns 11, not 101:
```{r}
y <- 10
h02 <- function(x) {
y <- 100
x + 1
}
h02(y)
```
This also means that when you do assignment inside a call to a function,
the variable is bound outside of the function, not inside of it.
```{r}
h02(y <- 1000)
y
```
* A value, which is computed and cached the first time a promise is
accessed when the expression is evaluated in the specified environment.
This ensures that the promise is evaluated at most once, and is why you
only see "Calculating..." printed once in the following example.
```{r}
double <- function(x) {
message("Calculating...")
x * 2
}
h03 <- function(x) {
c(x, x)
}
h03(double(20))
```
You cannot manipulate promises with R code. Promises are like a quantum state: any attempt to inspect them with R code will force an immediate evaluation, making the promise disappear. Later, in Section \@ref(quosures), you'll learn about quosures, which convert promises into an R object where you can easily inspect the expression and the environment.
### Default arguments
\index{functions!default values}
Thanks to lazy evaluation, default values can be defined in terms of other arguments, or even in terms of variables defined later in the function:
```{r}
h04 <- function(x = 1, y = x * 2, z = a + b) {
a <- 10
b <- 100
c(x, y, z)
}
h04()
```
Many base R functions use this technique, but I don't recommend it. It makes the code harder to understand: to predict _what_ will be returned, you need to know the exact order in which default arguments are evaluated.
The evaluation environment is slightly different for default and user supplied arguments, as default arguments are evaluated inside the function. This means that seemingly identical calls can yield different results. It's easiest to see this with an extreme example:
```{r, eval = FALSE}
h05 <- function(x = ls()) {
a <- 1
x
}
# ls() evaluated inside h05:
h05()
#> [1] "a" "x"
# ls() evaluated in global environment:
h05(ls())
#> [1] "h05"
```
### Missing arguments
\index{missing arguments!missing@\texttt{missing()}}
\indexc{\%\textbar\textbar\%}
To determine if an argument's value comes from the user or from a default, you can use `missing()`:
```{r}
h06 <- function(x = 10) {
list(missing(x), x)
}
str(h06())
str(h06(10))
```
`missing()` is best used sparingly, however. Take `sample()`, for example. How many arguments are required?
```{r}
args(sample)
```
It looks like both `x` and `size` are required, but if `size` is not supplied, `sample()` uses `missing()` to provide a default. If I were to rewrite sample, I'd use an explicit `NULL` to indicate that `size` is not required but can be supplied:
```{r}
sample <- function(x, size = NULL, replace = FALSE, prob = NULL) {
if (is.null(size)) {
size <- length(x)
}
x[sample.int(length(x), size, replace = replace, prob = prob)]
}
```
With the binary pattern created by the `%||%` infix function, which uses the left side if it's not `NULL` and the right side otherwise, we can further simplify `sample()`:
\indexc{NULL}
```{r}
`%||%` <- function(lhs, rhs) {
if (!is.null(lhs)) {
lhs
} else {
rhs
}
}
sample <- function(x, size = NULL, replace = FALSE, prob = NULL) {
size <- size %||% length(x)
x[sample.int(length(x), size, replace = replace, prob = prob)]
}
```
Because of lazy evaluation, you don't need to worry about unnecessary computation: the right side of `%||%` will only be evaluated if the left side is `NULL`.
### Exercises
1. What important property of `&&` makes `x_ok()` work?
```{r}
x_ok <- function(x) {
!is.null(x) && length(x) == 1 && x > 0
}
x_ok(NULL)
x_ok(1)
x_ok(1:3)
```
What is different with this code? Why is this behaviour undesirable here?
```{r}
x_ok <- function(x) {
!is.null(x) & length(x) == 1 & x > 0
}
x_ok(NULL)
x_ok(1)
x_ok(1:3)
```
1. What does this function return? Why? Which principle does it illustrate?
```{r, results = "hide"}
f2 <- function(x = z) {
z <- 100
x
}
f2()
```
1. What does this function return? Why? Which principle does it illustrate?
```{r, results = "hide"}
y <- 10
f1 <- function(x = {y <- 1; 2}, y = 0) {
c(x, y)
}
f1()
y
```
1. In `hist()`, the default value of `xlim` is `range(breaks)`, the default
value for `breaks` is `"Sturges"`, and
```{r}
range("Sturges")
```
Explain how `hist()` works to get a correct `xlim` value.
1. Explain why this function works. Why is it confusing?
```{r}
show_time <- function(x = stop("Error!")) {
stop <- function(...) Sys.time()
print(x)
}
show_time()
```
1. How many arguments are required when calling `library()`?
## `...` (dot-dot-dot) {#fun-dot-dot-dot}
\indexc{...}
\index{functions!variadic|see {...}}
\index{ellipsis|see {...}}
\index{dot-dot-dot|see {...}}
Functions can have a special argument `...` (pronounced dot-dot-dot). With it, a function can take any number of additional arguments. In other programming languages, this type of argument is often called _varargs_ (short for variable arguments), and a function that uses it is said to be variadic.
You can also use `...` to pass those additional arguments on to another function.
```{r}
i01 <- function(y, z) {
list(y = y, z = z)
}
i02 <- function(x, ...) {
i01(...)
}
str(i02(x = 1, y = 2, z = 3))
```
Using a special form, `..N`, it's possible (but rarely useful) to refer to elements of `...` by position:
```{r}
i03 <- function(...) {
list(first = ..1, third = ..3)
}
str(i03(1, 2, 3))
```
More useful is `list(...)`, which evaluates the arguments and stores them in a list:
```{r}
i04 <- function(...) {
list(...)
}
str(i04(a = 1, b = 2))
```
(See also `rlang::list2()` to support splicing and to silently ignore trailing commas, and `rlang::enquos()` to capture unevaluated arguments, the topic of [quasiquotation].)
There are two primary uses of `...`, both of which we'll come back to later in the book:
* If your function takes a function as an argument, you want some way to
pass additional arguments to that function. In this example, `lapply()`
uses `...` to pass `na.rm` on to `mean()`:
```{r}
x <- list(c(1, 3, NA), c(4, NA, 6))
str(lapply(x, mean, na.rm = TRUE))
```
We'll come back to this technique in Section \@ref(passing-arguments).
* If your function is an S3 generic, you need some way to allow methods to
take arbitrary extra arguments. For example, take the `print()` function.
Because there are different options for printing depending on the type of
object, there's no way to pre-specify every possible argument and `...`
allows individual methods to have different arguments:
```{r, eval = FALSE}
print(factor(letters), max.levels = 4)
print(y ~ x, showEnv = TRUE)
```
We'll come back to this use of `...` in Section \@ref(s3-arguments).
Using `...` comes with two downsides:
* When you use it to pass arguments to another function, you have to
carefully explain to the user where those arguments go. This makes it
hard to understand what you can do with functions like `lapply()` and
`plot()`.
* A misspelled argument will not raise an error. This makes it easy for
typos to go unnoticed:
```{r}
sum(1, 2, NA, na_rm = TRUE)
```
### Exercises
1. Explain the following results:
```{r}
sum(1, 2, 3)
mean(1, 2, 3)
sum(1, 2, 3, na.omit = TRUE)
mean(1, 2, 3, na.omit = TRUE)
```
1. Explain how to find the documentation for the named arguments in the
following function call:
```{r, fig.asp = 1, small_mar = TRUE, fig.width = 3}
plot(1:10, col = "red", pch = 20, xlab = "x", col.lab = "blue")
```
1. Why does `plot(1:10, col = "red")` only colour the points, not the axes
or labels? Read the source code of `plot.default()` to find out.
## Exiting a function
Most functions exit in one of two ways[^esoterica]: they either return a value, indicating success, or they throw an error, indicating failure. This section describes return values (implicit versus explicit; visible versus invisible), briefly discusses errors, and introduces exit handlers, which allow you to run code when a function exits.
[^esoterica]: Functions can exit in other more esoteric ways like signalling a condition that is caught by an exit handler, invoking a restart, or pressing "Q" in an interactive browser.
### Implicit versus explicit returns
\index{functions!return value}
\indexc{return()}
There are two ways that a function can return a value:
* Implicitly, where the last evaluated expression is the return value:
```{r}
j01 <- function(x) {
if (x < 10) {
0
} else {
10
}
}
j01(5)
j01(15)
```
* Explicitly, by calling `return()`:
```{r}
j02 <- function(x) {
if (x < 10) {
return(0)
} else {
return(10)
}
}
```
### Invisible values {#invisible}
\indexc{invisible()}
\index{functions!invisible results}
\index{assignment}
Most functions return visibly: calling the function in an interactive context prints the result.
```{r}
j03 <- function() 1
j03()
```
However, you can prevent automatic printing by applying `invisible()` to the last value:
```{r}
j04 <- function() invisible(1)
j04()
```
To verify that this value does indeed exist, you can explicitly print it or wrap it in parentheses:
```{r}
print(j04())
(j04())
```
Alternatively, you can use `withVisible()` to return the value and a visibility flag:
```{r}
str(withVisible(j04()))
```
The most common function that returns invisibly is `<-`:
```{r}
a <- 2
(a <- 2)
```
This is what makes it possible to chain assignments:
```{r}
a <- b <- c <- d <- 2
```
In general, any function called primarily for a side effect (like `<-`, `print()`, or `plot()`) should return an invisible value (typically the value of the first argument).
### Errors
\indexc{stop()}
\index{errors}
If a function cannot complete its assigned task, it should throw an error with `stop()`, which immediately terminates the execution of the function.
```{r, error = TRUE}
j05 <- function() {
stop("I'm an error")
return(10)
}
j05()
```
An error indicates that something has gone wrong, and forces the user to deal with the problem. Some languages (like C, Go, and Rust) rely on special return values to indicate problems, but in R you should always throw an error. You'll learn more about errors, and how to handle them, in Chapter \@ref(conditions).
### Exit handlers {#on-exit}
\indexc{on.exit()}
\index{handlers!exit}
Sometimes a function needs to make temporary changes to the global state. But having to cleanup those changes can be painful (what happens if there's an error?). To ensure that these changes are undone and that the global state is restored no matter how a function exits, use `on.exit()` to set up an __exit handler__. The following simple example shows that the exit handler is run regardless of whether the function exits normally or with an error.
```{r, error = TRUE}
j06 <- function(x) {
cat("Hello\n")
on.exit(cat("Goodbye!\n"), add = TRUE)
if (x) {
return(10)
} else {
stop("Error")
}
}
j06(TRUE)
j06(FALSE)
```
::: sidebar
Always set `add = TRUE` when using `on.exit()`. If you don't, each call to `on.exit()` will overwrite the previous exit handler. Even when only registering a single handler, it's good practice to set `add = TRUE` so that you won't get any unpleasant surprises if you later add more exit handlers.
:::
`on.exit()` is useful because it allows you to place clean-up code directly next to the code that requires clean-up:
```{r}
cleanup <- function(dir, code) {
old_dir <- setwd(dir)
on.exit(setwd(old_dir), add = TRUE)
old_opt <- options(stringsAsFactors = FALSE)
on.exit(options(old_opt), add = TRUE)
}
```
Coupled with lazy evaluation, this creates a very useful pattern for running a block of code in an altered environment:
```{r}
with_dir <- function(dir, code) {
old <- setwd(dir)
on.exit(setwd(old), add = TRUE)
force(code)
}