-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab1_best_practices.Rmd
374 lines (256 loc) · 7.31 KB
/
Lab1_best_practices.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
---
title: "Best coding practices"
author: "Noora Sissala"
date: "2024-06-10"
output: html_document
editor_options:
markdown:
wrap: 72
---
# 1 Coding style
## 1.1 Valid variable names
Which of the following are valid/good variable names in R? What is wrong
with the ones that are invalid/bad?
- var1
- 3way_handshake
- .password
- **test**
- my-matrix-M
- three.dimensional.array
- 3D.distance
- .2objects
- wz3gei92
- next
- P
- Q
- R
- S
- T
- X
- is.larger?
## 1.2 Obscure code
```{r}
# The code below works, but can be improved. Do improve it!
myIterAtoR.max <- 5
second_iterator.max<-7
col.NUM= 10
row.cnt =10
fwzy45 <- matrix(rep(1, col.NUM*row.cnt),nrow=row.cnt)
for(haystack in (2-1):col.NUM){
for(needle in 1:row.cnt) {
if(haystack>=myIterAtoR.max){
fwzy45[haystack, needle]<-NA}
}}
```
```{r}
# Improved code
my_iterator_max <- 5
second_iterator_max <- 7
col_num <- 10
row_num <- 10
mat <- matrix(rep(1, col_num*row_num), nrow=row_num)
for(i in 1:col_num){
for(j in 1:row_num) {
if(i >= my_iterator_max){
mat[i, j] <- NA
}
}
}
```
## 1.3 Better formatting
```{r}
# Improve style and formatting of the following code:
simulate_genotype <- function( q, N=100 ) {
if( length(q)==1 ){
p <- (1 - q)
f_gt <- c(p^2, 2*p*q, q^2) # AA, AB, BB
}else{
f_gt<-q
}
tmp <- sample( c('AA','AB','BB'), size =N, prob=f_gt, replace=T )
return(tmp)
}
```
```{r}
# Improved code
simulate_genotype <- function(q, N = 100) {
if(length(q) == 1) {
p <- (1 - q)
f_gt <- c(p^2, (2 * p * q), q^2) # AA, AB, BB
} else {
f_gt <- q
}
tmp <- sample(c('AA', 'AB', 'BB'),
size = N,
prob = f_gt,
replace = TRUE)
return(tmp)
}
```
## 1.4 Hidden variable
```{r}
# ssign a vector of three last months (abbreviated in English) in a year to a hidden variable my_months
.my_months <- rev(rev(month.abb)[1:3])
```
## 1.5 Pipeline-friendly functions
```{r}
# Modify the function below so that it works with R pipes
my_filter <- function(threshold = 1, x, scalar = 5) {
x[x >= threshold] <- NA
x <- x * scalar
return(x)
}
```
```{r}
# Modified function
my_filter <- function(x, threshold = 1, scalar = 5) {
x[x >= threshold] <- NA
x * scalar
}
# Test:
c(-5, 5) %>% my_filter()
```
## 1.6 Untidy code
```{r}
# Is the code below correct? Can it be improved?
simulate_phenotype <- function(pop_params, gp_map, gtype) {
pop_mean <- pop_params[1]
pop_var <- pop_params[2]
pheno <- rnorm(n = N, mean = pop_mean, sd = sqrt(pop_var))
effect <- rep(0, times = length(N))
for (gt_iter in c('AA', 'AB', 'BB')) {
effect[gtype == gt_iter] <- rnorm(n = sum(gtype == gt_iter),
mean = gp_map[gt_iter, 'mean_eff'],
sd = sqrt(gp_map[gt_iter, 'var_eff']))
}
dat <- data.frame(gt = gtype, raw_pheno = pheno, effect = effect, pheno = pheno + effect)
return(dat)
}
# N is not initialized anywhere
```
# 2 Structuring code
## 2.1 Computing variance
Write a modular code (function or functions) that computes the sample
standard deviation given a vector of numbers. Decide how to logically
structure the code. Assume there are no built-in R functions for
computing mean and variance available.
```{r}
my_var <- function(x) {
m <- my_mean(x)
sd <- my_sd(x, m)
sd^2
}
my_sd <- function(x, m) {
sqrt(sum((x-m)^2)/(length(x)-1))
}
my_mean <- function(x) {
sum(x)/length(x)
}
my_var(c(1,2,3,4,5,6))
```
## 2.2 Writing a wrapper function
You found two functions in two different packages: the randomSampleInt
function that generates a random sample of integer numbers and the
randomSampleLetter function for generating a random sample of letters.
Unfortunately, the functions are called in different ways which you want
to unify in order to use them interchangeably in your code. Write a
wrapper function around the randomSampleLetter that will provide the
same interface to the function as the randomSampleInt. Also, the
randomSampleLetter cannot handle the seed. Can you add this feature to
your wrapper?
```{r}
# Original functions
randomSampleInt <- function(x, verbose, length, seed = 42) {
if (verbose) {
print(paste0('Generating random sample of ', length, ' integers using seed ', seed))
}
set.seed(seed)
sampleInt <- sample(x = x, size = length, replace = TRUE)
return(sampleInt)
}
randomSampleLetter <- function(N, silent=T, lett) {
if (!silent) {
print(paste0('Generating random sample of ', N, ' letters.'))
}
sample <- sample(x = lett, size = N, replace = TRUE)
return(sample)
}
```
```{r}
# Wrapper function for randomSampleLetter
randomSampleLetterWrapper <- function(x, verbose, length, seed = 42) {
set.seed(seed)
randomSampleLetter(N = length, silent = !verbose, lett = x)
}
```
```{r}
# Test
## Original function
set.seed(42)
randomSampleLetter(LETTERS, silent = FALSE, N = 5)
## Wrapper function
randomSampleLetterWrapper(LETTERS, verbose = TRUE, length = 5, seed = 42)
```
## 2.3 Customizing plot
Write a wrapper around the graphics::plot function that modifies its
default behavior so that it plots red crosses instead of black points.
Do it in a way that enables the user to modify other function arguments.
```{r}
# Wrappwr function for plot
my_plot <- function(x, col = 'red', pch = 4, ...) {
plot(x = x, col = col, pch = pch, ...)
}
```
```{r}
# Test
## Default function
plot(iris$Sepal.Length, xlab = 'Sepal length', ylab = 'Sepal width')
## Wrapper function
my_plot(iris$Sepal.Length, xlab = 'Sepal length', ylab = 'Sepal width')
```
## 2.4 Bonus task: Adding Arguments to a Function
What if you want to pass some additional parameters to a function and,
sadly, the authors forgot to add ... to the list of function arguments.
There is a way out – you can bind extra arguments supplied as alist
structure to the original function arguments retrieved by formals. Try
to fix the function below, so that the call red_plot(1, 1, col = 'red',
pch = 19) will result in points being represented by red circles. Do use
alist and formals and do not edit the red_plot itself!
**alist:** alist handles its arguments as if they described function
arguments. So the values are not evaluated, and tagged arguments with no
value are allowed whereas list simply ignores them. alist is most often
used in conjunction with formals.
**formals:** Get or set the formal arguments of a function.
- formals(fun = sys.function(sys.parent()), envir = parent.frame())
- formals(fun, envir = environment(fun)) \<- value
- For the first form, fun can also be a character string naming the
function to be manipulated, which is searched for in envir, by
default from the parent frame. If it is not specified, the function
calling formals is used.
```{r}
# Original function
red_plot <- function(x, y) {
plot(x, y, las=1, cex.axis=.8, ...)
}
# Test
red_plot(1, 1, col = 'red', pch = 19) # Does not work
```
```{r}
# Modify function
formals(red_plot) <- c(formals(red_plot), alist(... = ))
# Test
red_plot(1, 1, col = 'red', pch = 19)
```
## 2.5 Bonus task: Using options
Use options to change the default prompt in R to hello :-) >.
```{r}
# Check which options are available
head(.Options)
```
```{r}
options(prompt = 'hello :-) >')
```
```{r}
# Change back to default
options(prompt = "> ")
```