CSC121H Winter 2018: R Style Guide

R Style Guide

Overview

The following are the R style guidelines we will use for CSC121. Please follow the guidelines when writing your code. Good style is important for code readability and management! Part of your grade will come from closely following the style guide.

R Script file

File Names

File names should use underscores and be lowercase

my_script_file.R
End file names in .R

Put a comment at the top of the file that explains what this script is for.

# This script contains the functions for calculating the area of a rectangle.
.
.
rest of file...
.

Newlines throughout file

Put a single newline character (Return/Enter key) between function definitions in a file.

Put a single newline character after a return statement that comes at the end of a function.

Put a single newline at the end of an R script file.

Naming

Variable Names

Variable names should be camel case - first letter lowercase, all others uppercase:

variableName
Don't use underscores ( _ ) or hyphens ( - ) in variable names.

Constants are named like functions, but with an initial k.
kConstantName

Function Names

Function names have capital letters for every word
FunctionName()

Comments and Docstrings

Comments

Comments can be put above code to explain what the next section of code is about.
Comments start with a '#' followed by a single space.

# Find the sum of a and b, and add it to the total
sum <- a + b
total <- total + sum

Docstrings

Every function should have a 'docstring' comment (on the line following the function header) that explains *what* the function does. It should NOT explain *how* the function works.
Use the docstring to explain what the point of the function is, and what it returns. The docstring can also contain any important or non-obvious preconditions.
Use good spelling and grammar!

Every argument name must be found in the docstring and briefly described, and should be surrounded by single quotes.

Just like comments, one space after the # symbol. Docstrings should be indented.

One newline character between the docstring and function body.

TriangleArea <- function(base, height) {
    # Returns the area of a triange with a base length of 'base', 
    # and a height length of 'height'.
    
    .
    # function body
    .
    .
}

Assignment Statements

Use <-, not =, for assignment statments.

Good:
year <- 2018
BAD:
year = 2018

Whitespace

Binary operators

Place spaces around all binary operators (=, +, -, <-, etc.).

4 * 5
a <- 14
The exponent operators (^, **) do not need a space around them.

2^4
2**4

Parentheses

For a function call or definition, there should be no space between the function name (or function) and the opening parentesis for the argument list.

Function(arguments)
Function <- function(arguments) {

For if-statments, for loops, and while loops, there should be a space between if, for, or while, and the opening parenthesis.

if (condition) {

for (e in v) {

while (condition) {

Curly Braces

For a function, an opening curly brace should never go on its own line; a closing curly brace should always go on its own line.

Good:

MyFunction <- function(argument) {

    # function body

}
BAD:
MyFunction <- function(argument) 
{

    # function body

}

For an if-statement, an opening curly brace should never go on its own line and a closing curly brace should always go on its own line, except for else if or else, in which case

else if
and
else
should be surrounded by curly brackets.

Good:
if (condition) {
    ..statements..
} else if {
    ..statements..
} else {
    ..statements
}
BAD:
if (condition) 
{
    ..statements..
} 
else if (condition) 
{
    ..statements..
} else 
{
    ..statements
}

Indenting Code

Code can be indented with tabs. For example, the "# function body" line above is indented with one tab.

Code inside of functions should be indented. Code inside of control flow sections like if-statments, for loops, and while loops should be indented. Nested if, for, and while should have their statement bodies indented by one tab for each level of nesting.

Commas

Do not place a space before a comma, but always place one after a comma.
For example, when separating arguments in a function call:

MyFunction <- function(arg1, arg2, arg3, arg4) {

    # function body

}

Line length

Each line of code should be a maximum of 80 characters long. Longer lines of code should be split up into multiple lines.