Semillero de R
2016
We need to ensure that we write in the correct form in R.
All explanations were taken from this link, consult it for more information.
File names should end in .R and, of course, be meaningful.
GOOD: predict_ad_revenue.R
BAD: foo.R
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.
GOOD: avg.clicks
OK: avgClicks
BAD: avg_Clicks
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
).
The maximum line length is 80 characters.
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)
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.
GOOD:
tab <- table(df[df$days.from < 0, "camp"])
total <- sum(x[, 1])
total <- sum(x[1, ])
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
Place a space before left parenthesis, except in a function call.
GOOD:
if (debug)
BAD:
if(debug)
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 = "")))
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
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)
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)}
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
}
BAD:
if (condition) {
one or more lines
}
else {
one or more lines
}
BAD:
if (condition)
one line
else
one line
Use <-, not =, for assignment.
GOOD:
x <- 5
BAD:
x = 5
Never use <<-, never.
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.)
If everyone uses the same general ordering, we'll be able to read and understand each other's scripts faster and more easily.
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)")
The possibilities for creating errors when using attach are numerous. Avoid it.
“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