-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyle-guide.Rmd
More file actions
313 lines (243 loc) · 7.11 KB
/
Copy pathstyle-guide.Rmd
File metadata and controls
313 lines (243 loc) · 7.11 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
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
---
title: "Data Challenge Lab style guide"
output:
github_document:
toc: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = FALSE)
```
This style guide will introduce you to the style used in the Data Challenge Lab. It expands on the style introduced in the [tidyverse style guide](https://style.tidyverse.org/index.html).
## Why does style matter?
Style guides can seem like a list of arbitrary, pointless rules, but code style does matter. Good, consistent code style makes your code easier to read.
You will rarely write code and then never look at it again. You will also rarely recall exactly what a chunk of code does without reading it again. Good, consistent code style makes it easier for Future You to read and understand code you write. It also makes it easier for others to read and understand your code.
If your code is easier to read, you will also have an easier time identifying and fixing bugs. We've found that people who improve their good style tend to have fewer bugs.
## Style helpers
RStudio includes several features that will help you practice good style. These include:
* 80-character margin
- Go to Tools > Global Options > Code > Display, check _Show Margin_, and make sure that _Column margin_ is set to 80. This turns on a line in all files that marks the 80-character cutoff. As you'll see, you never want a line of code to exceed 80-characters.
- The lower lefthand corner of the RStudio editor also displays the row:column numbers. The column number gives the _next_ position, so you're OK if it says 81.
* Style diagnostics
- Go to Tools > Global Options > Code > Diagnostics and check _Provide R style diagnostics_. This turns on a feature that will tell you if you've violated some simple style rules. This tool will underline your bad code with a blue squiggle and put a blue "i" icon in the lefthand margin. Hover over the "i" to see the issue.
* Indentation fixer
- Cmd/Ctrl + I fixes some indentation issues. Highlight the code and press Cmd/Ctrl + I.
## Spacing
* Put spaces after commas.
```{r}
# Bad
sum(1,2)
# Good
sum(1, 2)
```
* Put spaces before and after mathematical operators.
```{r}
# Bad
1+2
# Good
1 + 2
```
* Put spaces before and after assignment operators.
```{r}
# Bad
df %>%
mutate(c=a)
# Good
df %>%
mutate(c = a)
```
```{r}
# Bad
df<-data
# Good
df <- data
```
## Pipes
* Put a space before a pipe.
```{r}
# Bad
df%>%
verb()
# Good
df %>%
verb()
```
* Add a new line after each pipe and indent each verb.
```{r}
# Bad
df %>% verb1() %>% verb2()
# Bad
df %>%
verb1() %>% verb2()
# Good
df %>%
verb1() %>%
verb2()
```
* When assigning the result of a pipe to a variable, begin the pipe on a new, indented line.
```{r}
# Bad
data <- df %>% verb()
# Bad
data <- df %>%
verb()
# Good
data <-
df %>%
verb()
```
* Avoid assigning unnecessary variables. Try to use a single pipe when possible.
```{r}
# Bad
data1 <-
df %>%
verb1()
data2 <-
data1 %>%
verb2()
# Good
data <-
df %>%
verb1() %>%
verb2()
```
## Verb arguments
* Place short verb arguments on the same line as their verb.
```{r}
# Bad
df %>%
verb(
arg = something_simple
)
# Good
df %>%
verb(arg = something_simple)
```
* When changing, creating, or renaming _multiple_ variables with `mutate()`, `summarize()`, `select()`, etc. put each variable on a new line.
* The opening parenthesis stays with the verb.
* The closing parenthesis gets its own line, and is aligned with the start of the verb name (Cmd/Ctrl + I will correctly do this for you).
```{r}
# Bad
data %>%
mutate(
new_var_1 = something, new_var_2 = something
)
# Bad
data %>%
mutate(new_var_1 = something,
new_var_2 = something
)
# Good
data %>%
mutate(
new_var_1 = something,
new_var_2 = something
)
```
* Code should never extend beyond the 80-characters line. Sometimes, all you'll need to do is put the function argument on a new line and indent.
* Again, make sure that the first parenthesis stays with the verb, and the second is on its own line and is aligned with the verb name.
```{r}
# Bad
data %>%
verb(very_long_argument_name_1 = something_so_complicated_it_goes_past_the_line)
# Bad
data %>%
verb(
very_long_argument_name_1 = something_so_complicated_it_goes_past_the_line)
# Bad
data %>%
verb(
very_long_argument_name_1 = something_so_complicated_it_goes_past_the_line
)
# Good
data %>%
verb(
very_long_argument_name_1 = something_so_complicated_it_goes_past_the_line
)
```
* If the verb has multiple arguments and those arguments exceed 80 characters, put each argument on its own line.
```{r}
# Bad
data %>%
verb(very_long_argument_name_1 = something_complicated_1, very_long_argument_name_2 = something_complicated_2)
# Bad
data %>%
verb(
very_long_argument_name_1 = something_complicated_1, very_long_argument_name_2 = something_complicated_2
)
# Good
data %>%
verb(
very_long_argument_name_1 = something_complicated_1,
very_long_argument_name_2 = something_complicated_2
)
```
* Sometimes, just putting the arguments on their own lines won't solve the length problem. In these cases, a complicated indenting schemes is required. Here are some examples.
```{r}
# Bad
data %>%
mutate(var = fun(arg1 = "something", arg2 = "something else", arg3 = "another thing"))
# Good
data %>%
mutate(
var =
fun(
arg1 = "something",
arg2 = "something else",
arg3 = "another thing"
)
)
```
```{r}
# Bad
df %>%
ggplot(mapping = aes(x = a_long_variable_name, y = another_long_variable_name, fill = a_third_long_variable_name), position = "dodge") +
geom_col()
# Good
df %>%
ggplot(
mapping =
aes(
x = a_long_variable_name,
y = another_long_variable_name,
fill = a_third_long_variable_name
),
position = "dodge"
) +
geom_col()
```
## An example
Indentation can get confusing. Here, we'll walk through an example.
```{r}
# Bad
data %>%
mutate(var = fun(arg1 = "something", arg2 = "something else", arg3 = "another thing"))
```
Step 1: put the contents of the verb on its own line.
```{r}
data %>%
mutate(
var = fun(arg1 = "something", arg2 = "something else", arg3 = "another thing")
)
```
Sometimes, that will solve the problem. In this case, the code still exceeds 80 characters.
Step 2: add a newline after the "=".
```{r}
data %>%
mutate(
var =
fun(arg1 = "something", arg2 = "something else", arg3 = "another thing")
)
```
All lines of code are now under 80 characters, but there will be cases when the code still exceeds the margin. There are also cases in which you'll want to put each of `fun()`'s arguments on its own line. Functions that should usually have each argument on its own line include `if_else()`, `recode()`, and `case_when()`.
```{r}
data %>%
mutate(
var =
recode(
arg1 = "something",
arg2 = "something else",
arg3 = "another thing"
)
)
```
Always remember to line up the closing parenthesis with the start of the function name. You can always check your indenting by highlighting the code and doing Cmd/Ctrl + I.