forked from swcarpentry/r-novice-inflammation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-func-R.Rmd
463 lines (384 loc) · 16.3 KB
/
02-func-R.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
---
layout: page
title: Programming with R
subtitle: Creating functions
minutes: 30
---
```{r, include = FALSE}
source("tools/chunk-options.R")
opts_chunk$set(fig.path = "fig/02-func-R-")
```
> ## Learning Objectives {.objectives}
>
> * Define a function that takes arguments.
> * Return a value from a function.
> * Test a function.
> * Set default values for function arguments.
> * Explain why we should divide programs into small, single-purpose functions.
If we only had one data set to analyze, it would probably be faster to load the file into a spreadsheet and use that to plot some simple statistics.
But we have twelve files to check, and may have more in the future.
In this lesson, we'll learn how to write a function so that we can repeat several operations with a single command.
### Defining a Function
Let's start by defining a function `fahr_to_kelvin` that converts temperatures from Fahrenheit to Kelvin:
```{r}
fahr_to_kelvin <- function(temp) {
kelvin <- ((temp - 32) * (5 / 9)) + 273.15
return(kelvin)
}
```
We define `fahr_to_kelvin` by assigning it to the output of `function`.
The list of argument names are containted within parentheses.
Next, the [body](reference.html#function-body) of the function--the statements that are executed when it runs--is contained within curly braces (`{}`).
The statements in the body are indented by two spaces, which makes the code easier to read but does not affect how the code operates.
When we call the function, the values we pass to it are assigned to those variables so that we can use them inside the function.
Inside the function, we use a [return statement](reference.html#return-statement) to send a result back to whoever asked for it.
> ## Tip {.callout}
>
> In R, it is not necessary to include the return statement.
> R automatically returns whichever variable is on the last line of the body
> of the function. Since we are just learning, we will explicitly define the
> return statement.
Let's try running our function.
Calling our own function is no different from calling any other function:
```{r}
# freezing point of water
fahr_to_kelvin(32)
# boiling point of water
fahr_to_kelvin(212)
```
We've successfully called the function that we defined, and we have access to the value that we returned.
### Composing Functions
Now that we've seen how to turn Fahrenheit into Kelvin, it's easy to turn Kelvin into Celsius:
```{r}
kelvin_to_celsius <- function(temp) {
celsius <- temp - 273.15
return(celsius)
}
#absolute zero in Celsius
kelvin_to_celsius(0)
```
What about converting Fahrenheit to Celsius?
We could write out the formula, but we don't need to.
Instead, we can [compose](reference.html#function-composition) the two functions we have already created:
```{r}
fahr_to_celsius <- function(temp) {
temp_k <- fahr_to_kelvin(temp)
result <- kelvin_to_celsius(temp_k)
return(result)
}
# freezing point of water in Celsius
fahr_to_celsius(32.0)
```
This is our first taste of how larger programs are built: we define basic
operations, then combine them in ever-larger chunks to get the effect we want.
Real-life functions will usually be larger than the ones shown here--typically half a dozen to a few dozen lines--but they shouldn't ever be much longer than that, or the next person who reads it won't be able to understand what's going on.
> ## Tip {.callout}
>
> This example showed the output of `fahr_to_kelvin` assigned to `temp_k`, which
> is then passed to `kelvin_to_celsius` to get the final result. It is also possible
> to perform this calculation in one line of code, by "chaining" functions
> together, like so:
>
> ```{r chained-example}
> # freezing point of water in Celsius
> kelvin_to_celsius(fahr_to_kelvin(32.0))
> ```
> ## Create a function {.challenge}
>
> + In the last lesson, we learned to **c**oncatenate elements into a vector using the `c` function, e.g. `x <- c("A", "B", "C")` creates a vector `x` with three elements.
> Furthermore, we can extend that vector again using `c`, e.g. `y <- c(x, "D")` creates a vector `y` with four elements.
> Write a function called `fence` that takes two vectors as arguments, called
>`original` and `wrapper`, and returns a new vector that has the wrapper vector
>at the beginning and end of the original:
>
> ```{r, echo=-1}
> fence <- function(original, wrapper) {
> answer <- c(wrapper, original, wrapper)
> return(answer)
> }
> best_practice <- c("Write", "programs", "for", "people", "not", "computers")
> asterisk <- "***" # R interprets a variable with a single value as a vector
> # with one element.
> fence(best_practice, asterisk)
> ```
> + If the variable `v` refers to a vector, then `v[1]` is the vector's first element and `v[length(v)]` is its last (the function `length` returns the number of elements in a vector).
> Write a function called `outside` that returns a vector made up of just the first and last elements of its input:
> ```{r, echo=-1}
> outside <- function(v) {
> first <- v[1]
> last <- v[length(v)]
> answer <- c(first, last)
> return(answer)
> }
> dry_principle <- c("Don't", "repeat", "yourself", "or", "others")
> outside(dry_principle)
> ```
> ## The call stack {.callout}
>
> For a deeper understanding of how functions work, you'll need to learn how they create their own environments and call other functions. Function calls are managed via the call stack. For more details on the call stack, have a look at the [supplementary material](02-supp-call-stack.html).
> ## Named variables and the scope of variables {.challenge}
>
> + Functions can accept arguments explicitly assigned to a variable name in
> in the function call `functionName(variable = value)`, as well as arguments by
> order:
> ```{r}
> input_1 = 20
> mySum <- function(input_1, input_2 = 10) {
> output <- input_1 + input_2
> return(output)
> }
> ```
> Q1 Given the above code was run, which value does `mySum(input_1 = 1, 3)` produce?
>
> 1) 4
> 2) 11
> 3) 23
> 4) 30
>
> Q2. If mySum(3) == 13, why does mySum(b=3) return an error?
### Testing and Documenting
Once we start putting things in functions so that we can re-use them, we need to start testing that those functions are working correctly.
To see how to do this, let's write a function to center a dataset around a particular value:
```{r}
center <- function(data, desired) {
new_data <- (data - mean(data)) + desired
return(new_data)
}
```
We could test this on our actual data, but since we don't know what the values ought to be, it will be hard to tell if the result was correct.
Instead, let's create a vector of 0s and then center that around 3.
This will make it simple to see if our function is working as expected:
```{r, }
z <- c(0, 0, 0, 0)
z
center(z, 3)
```
That looks right, so let's try center on our real data. We'll center the inflammation data from day 4 around 0:
```{r}
dat <- read.csv(file = "data/inflammation-01.csv", header = FALSE)
centered <- center(dat[, 4], 0)
head(centered)
```
It's hard to tell from the default output whether the result is correct, but there are a few simple tests that will reassure us:
```{r}
# original min
min(dat[, 4])
# original mean
mean(dat[, 4])
# original max
max(dat[, 4])
# centered min
min(centered)
# centered mean
mean(centered)
# centered max
max(centered)
```
That seems almost right: the original mean was about `r round(mean(dat[, 4]), 2)`, so the lower bound from zero is now about `r -round(mean(dat[, 4]), 2)`.
The mean of the centered data is `r mean(centered)`.
We can even go further and check that the standard deviation hasn't changed:
```{r}
# original standard deviation
sd(dat[, 4])
# centered standard deviation
sd(centered)
```
Those values look the same, but we probably wouldn't notice if they were different in the sixth decimal place.
Let's do this instead:
```{r}
# difference in standard deviations before and after
sd(dat[, 4]) - sd(centered)
```
Sometimes, a very small difference can be detected due to rounding at very low decimal places.
R has a useful function for comparing two objects allowing for rounding errors, `all.equal`:
```{r}
all.equal(sd(dat[, 4]), sd(centered))
```
It's still possible that our function is wrong, but it seems unlikely enough that we should probably get back to doing our analysis.
We have one more task first, though: we should write some [documentation](reference.html#documentation) for our function to remind ourselves later what it's for and how to use it.
A common way to put documentation in software is to add [comments](reference.html#comment) like this:
```{r}
center <- function(data, desired) {
# return a new vector containing the original data centered around the
# desired value.
# Example: center(c(1, 2, 3), 0) => c(-1, 0, 1)
new_data <- (data - mean(data)) + desired
return(new_data)
}
```
> ## Tip {.callout}
>
> Formal documentation for R functions is written in separate `.Rd` using a
> markup language similar to [LaTeX][]. You see the result of this documentation
> when you look at the help file for a given function, e.g. `?read.csv`.
> The [roxygen2][] package allows R coders to write documentation alongside
> the function code and then process it into the appropriate `.Rd` files.
> You will want to switch to this more formal method of writing documentation
> when you start writing more complicated R projects.
[LaTeX]: http://www.latex-project.org/
[roxygen2]: http://cran.r-project.org/web/packages/roxygen2/vignettes/rd.html
```{r challenge-more-advanced-function-analyze, eval=FALSE, include=FALSE}
analyze <- function(filename) {
# Plots the average, min, and max inflammation over time.
# Input is character string of a csv file.
dat <- read.csv(file = filename, header = FALSE)
avg_day_inflammation <- apply(dat, 2, mean)
plot(avg_day_inflammation)
max_day_inflammation <- apply(dat, 2, max)
plot(max_day_inflammation)
min_day_inflammation <- apply(dat, 2, min)
plot(min_day_inflammation)
}
```
```{r challenge-more-advanced-function-rescale, include=FALSE}
rescale <- function(v) {
# Rescales a vector, v, to lie in the range 0 to 1.
L <- min(v)
H <- max(v)
result <- (v - L) / (H - L)
return(result)
}
```
> ## A more advanced function {.challenge}
>
> + Write a function called `analyze` that takes a filename as a argument and displays the three graphs produced in the [previous lesson][01] (average, min and max inflammation over time).
> `analyze("data/inflammation-01.csv")` should produce the graphs already shown, while `analyze("data/inflammation-02.csv")` should produce corresponding graphs for the second data set. Be sure to document your function with comments.
> + Write a function `rescale` that takes a vector as input and returns a corresponding vector of values scaled to lie in the range 0 to 1.
> (If $L$ and $H$ are the lowest and highest values in the original vector, then the replacement for a value $v$ should be $(v-L) / (H-L)$.)
> Be sure to document your function with comments.
> + Test that your `rescale` function is working properly using `min`, `max`, and `plot`.
[01]: 01-starting-with-data.html
```{r rescale-test, include=FALSE}
answer <- rescale(dat[, 4])
min(answer)
max(answer)
plot(answer)
plot(dat[, 4], answer) # This hasn't been introduced yet, but it may be
# useful to show when explaining the answer.
```
### Defining Defaults
We have passed arguments to functions in two ways: directly, as in `dim(dat)`, and by name, as in `read.csv(file = "data/inflammation-01.csv", header = FALSE)`.
In fact, we can pass the arguments to `read.csv` without naming them:
```{r}
dat <- read.csv("data/inflammation-01.csv", FALSE)
```
However, the position of the arguments matters if they are not named.
```{r, error = TRUE}
dat <- read.csv(header = FALSE, file = "data/inflammation-01.csv")
dat <- read.csv(FALSE, "data/inflammation-01.csv")
```
To understand what's going on, and make our own functions easier to use, let's re-define our `center` function like this:
```{r}
center <- function(data, desired = 0) {
# return a new vector containing the original data centered around the
# desired value (0 by default).
# Example: center(c(1, 2, 3), 0) => c(-1, 0, 1)
new_data <- (data - mean(data)) + desired
return(new_data)
}
```
The key change is that the second argument is now written `desired = 0` instead of just `desired`.
If we call the function with two arguments, it works as it did before:
```{r}
test_data <- c(0, 0, 0, 0)
center(test_data, 3)
```
But we can also now call `center()` with just one argument, in which case `desired` is automatically assigned the default value of `0`:
```{r}
more_data <- 5 + test_data
more_data
center(more_data)
```
This is handy: if we usually want a function to work one way, but occasionally need it to do something else, we can allow people to pass an argument when they need to but provide a default to make the normal case easier.
The example below shows how R matches values to arguments
```{r}
display <- function(a = 1, b = 2, c = 3) {
result <- c(a, b, c)
names(result) <- c("a", "b", "c") # This names each element of the vector
return(result)
}
# no arguments
display()
# one argument
display(55)
# two arguments
display(55, 66)
# three arguments
display (55, 66, 77)
```
As this example shows, arguments are matched from left to right, and any that haven't been given a value explicitly get their default value.
We can override this behavior by naming the value as we pass it in:
```{r}
# only setting the value of c
display(c = 77)
```
> ## Tip {.callout}
>
> To be precise, R has three ways that arguments are supplied
>
> by you are matched to the *formal arguments* of the function definition
>
> 1. by complete name,
> 2. by partial name (matching on initial *n* characters of the argument name), and
> 3. by position.
>
> Arguments are matched in the manner outlined above in *that order*: by
> complete name, then by partial matching of names, and finally by position.
With that in hand, let's look at the help for `read.csv()`:
```{r, eval=FALSE}
?read.csv
```
There's a lot of information there, but the most important part is the first couple of lines:
```{r, eval=FALSE}
read.csv(file, header = TRUE, sep = ",", quote = "\"",
dec = ".", fill = TRUE, comment.char = "", ...)
```
This tells us that `read.csv()` has one argument, `file`, that doesn't have a default value, and six others that do.
Now we understand why the following gives an error:
```{r, results="hide", error = TRUE}
dat <- read.csv(FALSE, "data/inflammation-01.csv")
```
It fails because `FALSE` is assigned to `file` and the filename is assigned to the argument `header`.
> ## A function with default argument values {.challenge}
>
> + Rewrite the `rescale` function so that it scales a vector to lie between 0 and 1 by default, but will allow the caller to specify lower and upper bounds if they want.
> Compare your implementation to your neighbor's: do the two functions always behave the same way?
```{r, include=FALSE}
rescale <- function(v, lower = 0, upper = 1) {
# Rescales a vector, v, to lie in the range lower to upper.
L <- min(v)
H <- max(v)
result <- (v - L) / (H - L) * (upper - lower) + lower
return(result)
}
answer <- rescale(dat[, 4], lower = 2, upper = 5)
min(answer)
max(answer)
answer <- rescale(dat[, 4], lower = -5, upper = -2)
min(answer)
max(answer)
```
> ## Key Points {.callout}
>
> * Define a function using `name <- function(...args...) {...body...}`.
> * Call a function using `name(...values...)`.
> * R looks for variables in the current stack frame before looking for them at the top level.
> * Use `help(thing)` to view help for something.
> * Put comments at the beginning of functions to provide help for that function.
> * Annotate your code!
> * Specify default values for arguments when defining a function using `name = value` in the argument list.
> * Arguments can be passed by matching based on name, by position, or by omitting them (in which case the default value is used).
> ## Next Steps {.callout}
>
> We now have a function called analyze to visualize a single data set.
> We could use it to explore all 12 of our current data sets like this:
>
> ```{r, eval=FALSE}
> analyze("data/inflammation-01.csv")
> analyze("data/inflammation-02.csv")
> #...
> analyze("data/inflammation-12.csv")
> ```
>
> but the chances of us typing all 12 filenames correctly aren't great, and we'll be even worse off if we get another hundred files.
> What we need is a way to tell R to do something once for each file, and that will be the subject of the next lesson.