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

if (debug)


d. Curly Braces

e. Assignment

f. Semicolons

1.3. Organization

a. General Layout and Ordering

b. Commenting Guidelines

c. Function Definitions and Calls

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% || TRUE %in% {
    stop(" Arguments x and y must not have missing values.")
  covariance <- var(x, y)
  if (verbose)
    cat("Covariance = ", round(covariance, 4), ".\n", sep = "")

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