Skip to content
rivolli edited this page Nov 25, 2015 · 2 revisions

This guide is based on:

Rules

  1. File Names: end in .R and use _ if necessary
  2. Variables: variable.name
  3. Function Names: function_names
  4. Constants: UPPERCASE
  5. Line Length: maximum 80 characters
  6. Indentation: two spaces, no tabs Spacing
  7. Curly Braces: Always indent the code inside curly braces
  8. if: It’s ok to leave very short statements on the same line without braces
  9. else: Surround else with braces (and the if too)
  10. for: Always with braces
  11. Assignment: use <-, not =
  12. Semicolons: don't use them
  13. Space:
  • Place spaces around all binary operators (=, +, -, <-, etc.)
  • Spaces around ='s are optional when passing parameters in a function call or list name
  • Do not place a space before a comma, but always place one after a comma
  • Place a space before left parenthesis, except in a function call
  1. Errors: Errors should be raised using stop()
  2. Documentation: Omit the @title and @description

##Examples

IF

#Valid Examples
if (debug) do_something()

if (debug) {
  do_something()
}

if (debug) {
  do_something()
} 
else {
  do_nothing()
}

Functions

short_function <- function (a, b) {
  # Code
}

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

long_function_name <- function(a = "a long argument", 
                               b = "another argument",
                               c = "another long argument") {
  # Code
}

cat("one", "Two", "\n")

plot(x    = x.coord,
     y    = data.mat[, MakeColName(metric, ptiles[1], "roiOpt")],
     ylim = ylim,
     xlab = "dates",
     ylab = metric,
     main = (paste(metric, " for 3 samples ", sep = "")))

Braces

# Good Examples
diamonds[5, ]
total <- sum(x[, 1])
total <- sum(x[1, ])

Comments

# Load data ---------------------------

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

Clone this wiki locally