-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-best_practices.R
139 lines (99 loc) · 2.92 KB
/
02-best_practices.R
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
#################################
### Intermediate & Advanced R ###
#################################
# Adam M Chekroud
# November 4th, 2016
###########################
##### Best Practices ######
###########################
# Setting the seed ensures reproducibility of random sequences
set.seed(1)
# You install packages with the install.packages() function
# I highly recommend the tidyverse package: https://blog.rstudio.org/2016/09/15/tidyverse-1-0-0/
# install.packages("tidyverse")
library(tidyverse)
# It is important to consider the readability of your code!
# Two extremely helpful resources:
# - https://google.github.io/styleguide/Rguide.xml
# - http://adv-r.had.co.nz/Style.html
# Brief examples from Hadley Wickham
# File names should be meaningful and end in .R.
# # Good
# fit-models.R
# utility-functions.R
#
# # Bad
# test.r
# stuff_V1.r
# # If files need to be run in sequence, prefix them with numbers:
# 0-download.R
# 1-preprocess.R
# 2-plotting.R
# Function names should be:
# - lowercase
# - separated_by_underscores
# - meaningful!
# # Good
# first_day
#
# # Bad
# dayOne
# FirstDayOfTheMonth
# dm1
# Syntax
# Place spaces around all operators (=, +, -, <-, etc.)
# Same applies when using = in function calls.
# Always put a space after a comma, and never before (just like in regular English).
# Good
average <- mean(feet / 12 + inches, na.rm = TRUE)
# Bad
average<-mean(feet/12+inches,na.rm=TRUE)
# Place a space before left parentheses, except in a function call.
# Good
if (debug) do(x)
plot(x, y)
# Bad
if(debug)do(x)
plot (x, y)
# Extra spacing (i.e., more than one space in a row) is ok if it improves alignment of assignments
list(
total = a + b + c,
mean = (a + b + c) / n
)
# Curly braces
# An opening curly brace should never go on its own line and should always be followed by a new line.
# A closing curly brace should always go on its own line, unless it's followed by else.
# Always indent the code inside curly braces.
# Good
if (y < 0 && debug) {
message("Y is negative")
}
if (y == 0) {
log(x)
} else {
y ^ x
}
# Bad
if (y < 0 && debug)
message("Y is negative")
if (y == 0) {
log(x)
}
else {
y ^ x
}
# It's ok to leave very short statements on the same line:
if (y < 0 && debug) message("Y is negative")
# Line length
# Strive to limit your code to 80 characters per line. (fits on a printed page)
# If you find yourself running out of room, this is a good indication
# that you should put some of the work into a separate function.
# Comments
# Make extensive use of comments!
# Comments should explain why, not how
# Always start with # and a space
# Consider commented lines of --- and === to divide sections
# ------------------
# There are loads of resources online to learn more advanced R programming!
# http://adv-r.had.co.nz is a helpful book
# These guidelines are especially important if you write a package, or work with collaborators