/
r_style_guide
107 lines (65 loc) · 1.64 KB
/
r_style_guide
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
R STYLE GUIDE
-------------
GENERAL
-------
Use 2-space indentation.
Prefer the S3 object system.
Avoid shadowing.
When overloading, do not "fake" the dispatch by calling into a different
package:
# do NOT do this
apply <- function(X, MARGIN, FUN, ...) {
if( !is(X, "MyObj") ) {
return(base::apply(X, MARGIN, FUN, ...)) # calls into another package (base)
}
}
Use left-arrow assignment:
x <- 5
Instead of
x = 5
Don't use trailing braces everywhere (if, else, functions,
structures, typedefs, class definitions, etc.)
if( x )
foo
Connect opening parenthesis to control statements (if, while, switch,
for, etc.) and pad parenthesized expressions with spaces
if( x ) {
}
Instead of
if (x) {
}
Function names are lower case:
foo();
Use camelCase for multi-word function names:
doThis();
Instead of
dont_do_this();
Use a '.' to hide functions:
.myHiddenFun <- function() {
}
Avoid creating functions that rely on the ellipsis argument.
Function arguments and variables start lower case:
x
Use camel-case for multi-word variables:
myVariable;
Supply sensible defaults for function arguments; use match.arg
when a function can take on a variety of constants:
x <- function(arg1=c("foo", "bar", "baz")) {
arg1 <- match.arg(arg1)
}
Class names start with an upper case character and are in camel case:
s <- structure(new.env(parent = emptyenv()), class="Foo")
Use reserved words for logical values:
TRUE, FALSE
Do not use
T, F
Use precise specification for integer values:
if( length(x) == 0L ) {
}
Instead of
if( length(x) == 0 ) {
}
And
x[[1L]]
Instead of
x[[1]]