Coding conventions

Variables

  • Variable names: delimit words with full stops, e.g. variable.name

  • Variable assignment: Use assignment arrows (<-) rather than equals (=) signs

Functions

  • Function names: delimit words with full stops, e.g. function.name

  • Function assignment: Use assignment arrows (<-) rather than equals (=) signs for both global and local functions

  • Functions should use return() if they return a result, not rely on R returning the last calculation. If they don’t return anything then it should use invisible().

Formatting

  • Curly brackets: the opening curly bracket should be on the same line as the preceeding code. The closing curly bracket should have it’s own line. Curly brackets should always be used for clarity.

  • Indentation: Indentations should be 2 character spaces (not tabs!).

  • Spacing around symbols: Single spaces should be left either side of assignment arrows, equal signs and double equal signs.

  • For loops: Use the format for( i in seq(along=...))) rather than for( i in 1:length(...)), as this guards against two executions of the loop in the case that the variable has length 0.