-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathR-basic-automation.Rmd
More file actions
254 lines (187 loc) · 5.95 KB
/
R-basic-automation.Rmd
File metadata and controls
254 lines (187 loc) · 5.95 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
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
# Basic Automation with R
## Introduction and goals
The library `dplyr` provides many useful functions like `select()`, `filter()`, and `mutate()` that cover the vast majority of use cases, but real data is messy. Sometimes you may what to do but not *how* to do it with existing functions. This tutorial will teach you some fundamental building blocks of scripting so that you can produce the "how" when you know the "what."
A warning though, if using `dplyr` is driving a car, then this tutorial is teaching you how to walk. While more locations are reachable by walking, you will always get there much faster by driving. *Try to find the tools first before hacking together your own.*
## Prerequisites
Please download the following:
- [numbers.csv](https://raw.githubusercontent.com/EDUCE-UBC/MICB425/main/data/numbers.csv)
- [brokenNumbers.csv](https://raw.githubusercontent.com/EDUCE-UBC/MICB425/main/data/brokenNumbers.csv)
- [dummyData.zip](https://raw.githubusercontent.com/EDUCE-UBC/MICB425/main/data/dummyData.zip)
R libraries
```{r}
library(tidyverse)
```
## Making a function
In math, we specify functions like so: $f(x) = 2x$, where the function $f$ takes the parameter $x$ and performs the action $2x$ before handing back the result. In R, the same would look like this...
```{r}
f <- function(x) {
return(2 * x)
}
```
...and it can be used like so.
```{r}
f(2)
```
## Decisions with conditionals
`if` allows selective execution based on a condition.
```{r}
doubleIfLessThan <- function(x, threshold) {
if (x < threshold) {
return(2 * x)
} else {
return(x) # if no condition satisfied
}
}
print(doubleIfLessThan(12, 100)) # 12 < 100, so doubled 12
print(doubleIfLessThan(12, 10)) # 12 ≮ 10, so just give back 12
```
More than one condition can be used...
```{r}
doubleOutsideRange <- function(x, lower, upper) {
if (x < lower) {
return( 2 *x)
} else if (x > upper) {
return( 2 *x)
} else {
return(x)
}
}
print(doubleOutsideRange(5, 10, 20))
print(doubleOutsideRange(5, 0, 2))
print(doubleOutsideRange(5, 0, 10))
```
...and conditions can be combined with logical operators
```{r}
doubleIfInRange <- function(x, lower, upper) {
if (x >= lower && x <= upper) { # && is the same as AND
return(2*x)
} else {
return(x)
}
}
print(doubleIfInRange(5, 10, 20))
print(doubleIfInRange(5, 0, 2))
print(doubleIfInRange(5, 0, 10))
```
| Operator | Description |
|:--------:|:------------------------------------------------------------------------------:|
| `&&` | AND, both sides must be `TRUE` |
| `||` | OR, at lease one side must be`TRUE` |
| `!` | NOT, inverts the following (`TRUE` becomes `FALSE` and `FALSE` becomes `TRUE`) |
| `==` | EQUALS, left must equal right |
Try the following
```{r}
1==1
1==1 && 2==1
!(2==1) || FALSE
```
## Iterating with loops
Suppose we wanted to double each number in a list.
```{r}
numbers <- read.csv(file="data/numbers.csv", header=TRUE)
numbers
```
We can use a `for` loop. This is how it behaves...
```{r}
for (i in 1:5) {
print(i)
}
```
...and this is how we to use it in our context
```{r}
for (rowNumber in 1:nrow(numbers)) {
n <- numbers[rowNumber, "n"] # get value from row "rowNumber", column "n"
print(2 * n)
}
```
If the number of repeats is unknown, we can use a `while` loop. A condition is used instead to tell it when to stop.
```{r}
n <- 1
while (n < 1000) {
n <- n * 2
}
n
```
Loops can repeat forever. Click the red octagon (🛑) at the top right of your RStudio console to force the script to stop.
```{r eval = FALSE}
while (TRUE) {
# loop forever
}
```
## Reading files
Files can be opened and read one line at a time like so.
```{r}
numbersFileConnection <- file("data/numbers.csv", "r") # r is for read
while (TRUE) {
line <- readLines(numbersFileConnection, n = 1) # read 1 line
if (length(line) == 0) { # if no more lines ...
break # ... exit loop
}
print(line)
}
close(numbersFileConnection)
```
The last parameter specifies the read/write permissions of the opened file.
```{r error = TRUE}
con <- file("data/numbers.csv", "r")
write("some new text", file = con)
```
Files can also be written to one line at a time like so.
```{r}
con <- file("data/myNumbers.csv", "w")
write("n", con) # header
for (i in 1:10) {
line <- toString(i+10)
write(line, con)
}
close(con)
```
Check your `data` folder for the new file.
Reading and writing `csvs` are done *much better* by `read.csv()` and `write.csv()`, but suppose you come across this file...
```{r}
con <- file("data/brokenNumbers.csv","r")
line <- readLines(con)
for (i in 1:10) {
print(line[i])
}
close(con)
```
...seems innocent enough, but when trying to load it as a csv...
```{r}
isOdd <- read.csv(file = "data/brokenNumbers.csv", header = TRUE)
isOdd %>% slice(50:60)
```
## Practice 1
...it's format is obviously broken. Investigate the file and see if you can fix it.\
[Documentation for strsplit()](https://stringr.tidyverse.org/reference/str_split.html)
```{r}
con <- file("data/brokenNumbers.csv", "r")
outCon <- file("data/fixedNumbers.csv", "w")
headers <- readLines(con, n = 1)
while (TRUE) {
row = readLines(con, n = 1)
if (length(row) == 0) {
break
}
fixedRow <- ""
# split row into cells
cells <- str_split(row, ", ")[[1]] # see str_split above
# look at cells by index like so
n = cells[1]
twoN = cells[length(cells)]
# ...
# join strings using paste()
fixedRow <- paste(n, ", ", twoN) # ...
write(fixedRow, outCon)
}
close(con)
close(outCon)
```
## Practice 2
TreeSAPP's `assign` function wants a single `fasta` file, but all your data is separated by bins from a previous taxonomic classifier! See if you can append all of them together into one file.
```{r}
for (f in list.files('data/dummyData')) {
print(f)
# append files
}
```