Google's R Style Guide

Semillero de R
2016

Google's R Style Guide

We need to ensure that we write in the correct form in R.

escritura

All explanations were taken from this link, consult it for more information.

File names

File names should end in .R and, of course, be meaningful.

GOOD: predict_ad_revenue.R

BAD: foo.R

Identifiers

  • Don't use underscores ( _ ) or hyphens ( - ) in identifiers.

  • Identifiers should be named according to the following conventions.

  • The preferred form for variable names is all lower case letters and words separated with dots (variable.name), but variableName is also accepted; function names have initial capital letters and no dots (FunctionName); constants are named like functions but with an initial k.

Examples of identifiers

  • variable.name is preferred, variableName is accepted

GOOD: avg.clicks

OK: avgClicks

BAD: avg_Clicks

Examples of identifiers

  • FunctionName

GOOD: CalculateAvgClicks

BAD: calculate_avg_clicks , calculateAvgClicks

Make function names verbs.

Exception: When creating a classed object, the function name (constructor) and class should match (e.g., lm).

  • kConstantName

Line Length

The maximum line length is 80 characters.

80

Indentation

When indenting your code, use two spaces.

Never use tabs or mix tabs and spaces.

Exception: When a line break occurs inside parentheses, align the wrapped line with the first character inside the parenthesis.

BAD:

mod <- gamlss(earn ~ years + sex, data = countries)

Spacing

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

  • Exception: Spaces around ='s are optional when passing parameters in a function call.

  • Do not place a space before a comma, but always place one after a comma.

Examples of spacing

GOOD:

tab <- table(df[df$days.from < 0, "camp"])
total <- sum(x[, 1])
total <- sum(x[1, ])

Examples of spacing

BAD:

tab <- table(df[df$days.from.opt<0, "camp"])  
# Needs spaces around '<'
tab <- table(df[df$days.from.opt < 0,"camp"])  
# Needs a space after the comma
tab<- table(df[df$days.from.opt < 0, "camp"])  
# Needs a space before <-
tab<-table(df[df$days.from.opt < 0, "camp"])  
# Needs spaces around <-
total <- sum(x[,1])  
# Needs a space after the comma
total <- sum(x[ ,1])  
# Needs a space after the comma, not before

Examples of spacing

Place a space before left parenthesis, except in a function call.

GOOD:

if (debug)

BAD:

if(debug)

Examples of spacing

Extra spacing (i.e., more than one space in a row) is okay if it improves alignment of equals signs or arrows (<-).

plot(x    = x.coord,
     y    = data.mat[, 2],
     ylim = ylim,
     xlab = "dates",
     ylab = age,
     main = (paste(age, " eval ", sep = "")))

Examples of spacing

Do not place spaces around code in parentheses or square brackets. Exception: Always place a space after a comma.

GOOD:

if (debug)
x[1, ]

BAD:

if ( debug )  # No spaces around debug
x[1,]  # Needs a space after the comma 

Curly Braces

  • An opening curly brace should never go on its own line; a closing curly brace should always go on its own line.

  • You may omit curly braces when a block consists of a single statement; however, you must consistently either use or not use curly braces for single statement blocks.

GOOD:

if (is.null(ylim)) {
  ylim <- c(0, 0.06)
}
if (is.null(ylim))
  ylim <- c(0, 0.06)

Curly Braces

Always begin the body of a block on a new line.

BAD:

if (is.null(ylim)) ylim <- c(0, 0.06) 
if (is.null(ylim)) {ylim <- c(0, 0.06)}

Surround else with braces

An else statement should always be surrounded on the same line by curly braces.

GOOD:

if (condition) {
  one or more lines
} else {
  one or more lines
}

Surround else with braces

BAD:

if (condition) {
  one or more lines
}
else {
  one or more lines
}

BAD:

if (condition)
  one line
else
  one line

Assignment

Use <-, not =, for assignment.

GOOD:

x <- 5

BAD:

x = 5

Never use <<-, never.

Semicolons

Do not terminate your lines with semicolons or use semicolons to put more than one command on the same line. (Semicolons are not necessary, and are omitted for consistency with other Google style guides.)

semicolon

General Layout and Ordering

If everyone uses the same general ordering, we'll be able to read and understand each other's scripts faster and more easily.

  1. Copyright statement comment
  2. Author comment
  3. File description comment, including purpose of program, inputs, and outputs
  4. source() and library() statements
  5. Function definitions
  6. Executed statements, if applicable (e.g., print, plot)

Commenting Guidelines

Comment your code. Entire commented lines should begin with # and one space.

Short comments can be placed after code preceded by two spaces, #, and then one space.

# Create histogram of frequency of campaigns by pct budget spent.
hist(df$pct.spent,
     breaks = "scott",  # method for choosing number of buckets
     main   = "Histogram: fraction budget spent by campaignid",
     xlab   = "Fraction of budget spent",
     ylab   = "Frequency (count of campaignids)")

Attach

The possibilities for creating errors when using attach are numerous. Avoid it.

attach

Remember

  1. Use common sense and BE CONSISTENT.
  2. If you are editing code, take a few minutes to look at the code around you and determine its style.
  3. The point of having style guidelines is to have a common vocabulary.

Remember

“The only way to write good code is to write tons of shitty code first. Feeling shame about bad code stops you from getting to good code”.

Hadley Wickham

Let's go

programmer