-
Notifications
You must be signed in to change notification settings - Fork 28
Coding Guidelines and Style
Ben Bond-Lamberty edited this page Jan 4, 2019
·
6 revisions
Generally, we follow Hadley Wickham's R style guide.
Most importantly:
- Use
<-for assignment, not=. - Operators do get spacing, parentheses do not:
if (x + 1) print("yes") - Use short, clear, and consistent variable names.
- Clearly comment your code and data.
We're using a standard set of R packages to handle many tasks–mostly from what is known as the tidyverse. These include:
- dplyr for data manipulation and processing;
- tidyr for data reshaping;
- assertthat for assertions;
- tibble for better data frames;
- magrittr for building pipelines.
A word about pipelines: we aim to use these consistently and when it makes sense...but don't take things to extremes. Sometimes y <- f(g(x)) really is the clearest notation.
The package tests will raise an error for all of these:
- R's
match()function - The
reshapeorreshape2packages (i.e.meltorcast) - Loops (this is not an ironclad prohibition, but should be very rare)
- R's
ifelsefunction - if necessary usedplyr::if_elseinstead - Any of R's
applyfamily of functions,rowMeans, etc. (again, with rare exceptions) - Successive
mutate()calls. See Hints for speed