-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession4.Rmd
226 lines (161 loc) · 5.51 KB
/
session4.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
---
title: "Creating your own functions"
subtitle: "Learning the basics of R - Part 3"
author:
- "Ernest Guevarra"
date: '12 November 2024'
output:
xaringan::moon_reader:
css: xaringan-themer.css
nature:
slideNumberFormat: "%current%"
highlightStyle: github
highlightLines: true
ratio: 16:9
countIncrementalSlides: true
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(
fig.width=9, fig.height=3.5, fig.retina=3,
out.width = "100%",
cache = FALSE,
echo = TRUE,
message = FALSE,
warning = FALSE,
hiline = TRUE
)
if (!require(remotes)) install.packages("remotes")
if (!require(fontawesome)) remotes::install_github("rstudio/fontawesome")
if (!require(tweetrmd)) remotes::install_github("gadenbuie/tweetrmd")
if (!require(webshot2)) remotes::install_github("rstudio/webshot2")
```
```{r xaringan-themer, include=FALSE, warning=FALSE}
library(xaringanthemer)
style_mono_light(
base_color = "#002147",
title_slide_background_image = "",
title_slide_background_size = "cover",
header_font_google = google_font("Fira Sans"),
text_font_google = google_font("Fira Sans Condensed"),
text_font_size = "1.2em",
link_color = "#214700",
header_h1_font_size = "50px",
header_h2_font_size = "40px",
header_h3_font_size = "30px",
code_font_google = google_font("Fira Mono"),
text_slide_number_font_size = "0.5em",
footnote_font_size = "0.5em"
)
```
# Outline
* Why write functions
* When to write functions
* How to write functions
* Practical session
---
# Why write functions
* Allow automation of common tasks in a more powerful and general way than *copy-and-pasting*;
* You can give a function an evocative name that makes your code easier to understand;
* As requirements change, you only need to update code in one place, instead of many.
* You eliminate the chance of making incidental mistakes when you copy and paste (i.e. updating a variable name in one place, but not in another).
* Fewer global variables: When you run a function, the intermediate variables that it creates are not stored in your global environment. This saves memory and keeps your global environment cleaner.
* Better documentation: Well documented functions help the user understand the steps of your processing.
* Easier to maintain / edit: When you create a function for a repeated task, it is easy to edit that one function. Then every location in your code where that same task is performed is automatically updated.
---
# When to write functions
.pull-left[
You should consider writing a function whenever you’ve copied and pasted a block of code more than twice (i.e. you now have three copies of the same code).
For example, take a look at this code.
What does it do?
]
.pull-right[
```R
df <- data.frame(
a = c(1, 2, 1, 1, 1, 2, 1, 1, 2, 2),
b = c(2, 2, 2, 1, 1, 1, 1, 2, 1, 2),
c = c(1, 2, 1, 1, 2, 1, 2, 1, 2, 2)
)
df$a <- ifelse(df$a == 2, 0, df$a)
df$b <- ifelse(df$b == 2, 0, df$b)
df$c <- ifelse(df$c == 2, 0, df$c)
```
]
---
# When to write functions
.pull-left[
Original data:
```{r, echo = FALSE}
df <- data.frame(
a = c(1, 2, 1, 1, 1, 2, 1, 1, 2, 2),
b = c(2, 2, 2, 1, 1, 1, 1, 2, 1, 2),
c = c(1, 2, 1, 1, 2, 1, 2, 1, 2, 2)
)
df
```
]
.pull-right[
Recoded data:
```{r, echo = FALSE}
df$a <- ifelse(df$a == 2, 0, df$a)
df$b <- ifelse(df$b == 2, 0, df$b)
df$c <- ifelse(df$c == 2, 0, df$c)
df
```
]
--
This is a good example of when writing a function will be useful/beneficial.
---
# How to write functions
.pull-left[
We can create a function called `recode_values()`:
```R
recode_values <- function(x) {
ifelse(x == 2, 0, x)
}
```
And apply it to the same data as follows:
```R
df$a <- recode_values(df$a)
df$b <- recode_values(df$b)
df$c <- recode_values(df$c)
```
]
--
.pull-right[
We get:
```{r, echo = FALSE}
df <- data.frame(
a = c(1, 2, 1, 1, 1, 2, 1, 1, 2, 2),
b = c(2, 2, 2, 1, 1, 1, 1, 2, 1, 2),
c = c(1, 2, 1, 1, 2, 1, 2, 1, 2, 2)
)
recode_values <- function(x) {
ifelse(x == 2, 0, x)
}
df$a <- recode_values(df$a)
df$b <- recode_values(df$b)
df$c <- recode_values(df$c)
df
```
]
---
# How to write functions
* You need to pick a name for the function. In the example I used `recode_values` because this function recodes the values based on a specified rule (i.e., value of 2 is converted to 0).
* You list the inputs, or **arguments**, to the function inside `function`. Here we have just one argument. If we had more the call would look like `function(x, y, z)`.
* You place the code you have developed in body of the function, a `{` block that immediately follows `function(...)`.
???
Note the overall process: I only made the function after I’d figured out how to make it work with a simple input. It’s easier to start with working code and turn it into a function; it’s harder to create a function and then try to make it work.
---
class: inverse, center, middle
# Questions?
---
class: inverse, center, middle
# Practical session
We'll work through *Exercise 2 - Manipulating objects and creating new functions* in Practical R for Epidemiologists (https://practical-r.org/exercise2.html) as a GitHub Classroom assignment
---
class: inverse, center, middle
# Thank you!
Slides can be viewed at https://oxford-ihtm.io/open-reproducible-science/session4.html
PDF version of slides can be downloaded at https://oxford-ihtm.io/open-reproducible-science/pdf/session4-r-basics-part3.pdf
R scripts for slides available [here](https://github.com/OxfordIHTM/open-reproducible-science/blob/main/session4.Rmd)