-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-background.Rmd
More file actions
126 lines (76 loc) · 2.42 KB
/
Copy path01-background.Rmd
File metadata and controls
126 lines (76 loc) · 2.42 KB
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
---
output: html_document
editor_options:
chunk_output_type: console
---
class: inverse
# Poll
Have you ever heard of tidy evaluation?
* No
* I've heard of it, but I'm not sure what it is
* I know what it is, but I'm not comfortable using it
* Yes, I feel comfortable using it
---
# Goals for Today
Be able to write a function that utilizes `{tidyverse}` functions (e.g.,
`mutate`, `select`, `filter`, etc.) by using tidy evaluation
---
# Outline
* Function Basics
* Nuances of writing functions with the tidyverse
- data-masking
- tidy-select
* Writing functions that use data-masking tidyverse functions
* Writing functions that use tidy-select tidyverse functions
* Practice
* Resources & Additional Material
---
# Functions in R
**Rule of thumb**: If you're copy/pasting more than twice, write a function!
Functions:
* Prevent inconsistencies because they force multiple computations to follow a
single recipe
* Emphasize what varies (arguments) and what is constant (everything else)
* Make updates easier since you only modify one place
* Make code clearer, especially if you give function and arguments informative names
If you're a `{tidyverse}` user, you'll likely want to use the `{tidyverse}` for
your functions.
However, because of some technical features of the `{tidyverse}` (*data masking*),
there are a few nuances to writing tidyverse-style functions.
---
# Anatomy of a Function
```{r, eval=FALSE}
function_name <- function(argument1, argument2){
# body of the function
# code to repeat
}
# the function call
function_name(argument1 = "arg1",
argument2 = "arg2")
```
---
# What's Returned by a Function
* By default, only the last known operation is returned by the function
* This is *different* from SAS macros
* Can use a named list to return multiple objects
```{r, eval=FALSE}
example_return_list <- function(arg1){
# function body
# create object 1
# create object 2
return(list("object1" = object1,
"object2" = object2))
}
# store the function call
call_return_list <- example_return_list(arg1)
# will return
call_return_list$object1
call_return_list$object2
```
---
# Steps for Writing a Function
1. Write and test your code in R, outside of a function
2. Enclose in `function(){[working code here]}` with an informatively named
function and arguments
3. Adapt code to replace variable names and/or values with function arguments,
as needed