Repository navigation
R Code Style
rivolli edited this page Nov 25, 2015
·
2 revisions
This guide is based on:
- Google's R Style - https://google.github.io/styleguide/Rguide.xml
- R Packages book - http://r-pkgs.had.co.nz/r.html#style
- File Names: end in .R and use _ if necessary
- Variables: variable.name
- Function Names: function_names
- Constants: UPPERCASE
- Line Length: maximum 80 characters
- Indentation: two spaces, no tabs Spacing
- Curly Braces: Always indent the code inside curly braces
- if: It’s ok to leave very short statements on the same line without braces
- else: Surround else with braces (and the if too)
- for: Always with braces
- Assignment: use <-, not =
- Semicolons: don't use them
- 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
- Errors: Errors should be raised using stop()
- Documentation: Omit the @title and @description
##Examples
#Valid Examples
if (debug) do_something()
if (debug) {
do_something()
}
if (debug) {
do_something()
}
else {
do_nothing()
}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 = "")))# Good Examples
diamonds[5, ]
total <- sum(x[, 1])
total <- sum(x[1, ])
# 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)")