Follow Hadley Wickham’s style guide, adapted from Google’s style guide (HW’s link to Google is broken). Some of the differences between Google and HW are:
HW prefers snake_case for identifiers, Google likes
kebab or camel (variable.name (or
variableName), FunctionName,
kConstantName)
HW uses Roxygen
See “Code Smells and Feels.”, Jenny Bryan (2018), for some more advanced suggestions.
Additions from BMB and JD:
stopifnot() for assertionst, c,
data, …)rm(list=ls()) at the top of your code (see commentary
by Jenny Bryan)TRUE and FALSE rather than
T/F (is this in the other style guides
already???)+,
%>%). For example,thing <- (thing %>%
mutate(foo=x^2)
)
rather than
thing <- thing %>%
mutate(foo=x^2)
thing <- (thing
%>% mutate(foo=x^2
, bar=x^3
, bletch=x^4
)
)
rather than
thing <- thing %>%
mutate(foo=x^2,
bar=x^3,
bletch=x^4)
print() statements rather than relying on
objects to self-printreturn() statements rather than relying on
R’s implicit “return value is the last statement in the function”
rule# for end-of-line comments,
## for comments on a line by themselvesc() (e.g. c(1:30)). Lean
toward seq() and seq_along(), but OK (?) to
use :