Likan Zhan

Google's R Style Guide

Likan · 2017-05-08

This post is retrieved from google

R is a high-level programming language used primarily for statistical computing and graphics. The goal of the R Programming Style Guide is to make our R code easier to read, share, and verify. The rules below were designed in collaboration with the entire R user community at Google.

1. R Style Rules

1.1. Notation and Naming

a. File Names

b. identifiers

1.2. Syntax

a. Line length

b. Indentation

c. Spacing

tab.prior <- table(df[df$days.from.opt < 0, "campaign.id"])
total <- sum(x[, 1])
total <- sum(x[1, ])
tab.prior <- table(df[df$days.from.opt<0, "campaign.id"])  # Needs spaces around '<'
tab.prior <- table(df[df$days.from.opt < 0,"campaign.id"])  # Needs a space after the comma
tab.prior<- table(df[df$days.from.opt < 0, "campaign.id"])  # Needs a space before <-
tab.prior<-table(df[df$days.from.opt < 0, "campaign.id"])  # 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

if (debug)

if(debug)

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

d. Curly Braces

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

e. Assignment

x <- 5
x = 5

f. Semicolons

1.3. Organization

a. General Layout and Ordering

b. Commenting Guidelines

# 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)")

c. Function Definitions and Calls

PredictCTR <- function(query, property, num.days,
                       show.plot = TRUE)
PredictCTR <- function(query, property, num.days, show.plot =
                       TRUE)

d. Function Documentation

e. Example Function

CalculateSampleCovariance <- function(x, y, verbose = TRUE) {
  # Computes the sample covariance between two vectors.
  #
  # Args:
  #   x: One of two vectors whose sample covariance is to be calculated.
  #   y: The other vector. x and y must have the same length, greater than one,
  #      with no missing values.
  #   verbose: If TRUE, prints sample covariance; if not, not. Default is TRUE.
  #
  # Returns:
  #   The sample covariance between x and y.
  n <- length(x)
  # Error handling
  if (n <= 1 || n != length(y)) {
    stop("Arguments x and y have different lengths: ",
         length(x), " and ", length(y), ".")
  }
  if (TRUE %in% is.na(x) || TRUE %in% is.na(y)) {
    stop(" Arguments x and y must not have missing values.")
  }
  covariance <- var(x, y)
  if (verbose)
    cat("Covariance = ", round(covariance, 4), ".\n", sep = "")
  return(covariance)
}

f. TODO Style

2. R language Rules

2.1. Attach

2.2. Functions

2.3. Objects and Methods

2.4. Exceptions

2.5. Parting Words

3. References