Skip to content

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.

Standard R packages

We're using a standard set of R packages to handle many tasks–mostly from what is known as the tidyverse. These include:

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.

Things NOT to use

The package tests will raise an error for all of these:

  • R's match() function
  • The reshape or reshape2 packages (i.e. melt or cast)
  • Loops (this is not an ironclad prohibition, but should be very rare)
  • R's ifelse function - if necessary use dplyr::if_else instead
  • Any of R's apply family of functions, rowMeans, etc. (again, with rare exceptions)
  • Successive mutate() calls. See Hints for speed

Clone this wiki locally