R Code Style Standards
Standards for code style aren’t the same as syntax, although they are sort of related. It is usually wise to be careful about code style to maximize the readability of your code, making it easier for you and others to maintain.
In this book, I’ve tried to stick to Google’s R Style Guide, which is available at http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html. Here is a summary of its suggestions:
- Indentation
Indent lines with two spaces, not tabs. If code is inside parentheses, indent to the innermost parentheses.
- Spacing
Use only single spaces. Add spaces between binary operators and operands. Do not add spaces between a function name and the argument list. Add a single space between items in a list, after each comma.
- Blocks
Don’t place an opening brace (“{”) on its own line. Do place a closing brace (“}”) on its own line. Indent inner blocks (by two spaces).
- Semicolons
Omit semicolons at the end of lines when they are optional.
- Naming
Name objects with lowercase words, separated by periods. For function names, capitalize the name of each word that is joined together, with no periods. Try to make function names verbs.
- Assignment
Use
<-
, not=
for assignment statements.
Don’t be confused by the object names. You don’t have to name objects things like “field.goals” or “sanfrancisco.home.prices” or “top.bacon.searching.cities.” It’s just convention.
Get R in a Nutshell, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.