Google's R Style Guide

google's_r_style_guide

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 10

DownloadGoogle's R Style Guide
Open PDF In BrowserView PDF
Google's R Style Guide

https://google.github.io/styleguide/Rguide.xml

Google's R Style Guide
R is a high-level programming language used primarily for statistical computing and graphics. The goal of the R Programming Style
Guide is to make our R code easier to read, share, and verify. The rules below were designed in collaboration with the entire R user
community at Google.

Summary: R Style Rules
1. File Names: end in .R
2. Identifiers: variable.name (or variableName), FunctionName, kConstantName
3. Line Length: maximum 80 characters
4. Indentation: two spaces, no tabs
5. Spacing
6. Curly Braces: first on same line, last on own line
7. else: Surround else with braces
8. Assignment: use <-, not =
9. Semicolons: don't use them
10. General Layout and Ordering
11. Commenting Guidelines: all comments begin with # followed by a space; inline comments need two spaces before the #
12. Function Definitions and Calls
13. Function Documentation
14. Example Function
15. TODO Style: TODO(username)

Summary: R Language Rules
1. attach: avoid using it
2. Functions: errors should be raised using stop()
3. Objects and Methods: avoid S4 objects and methods when possible; never mix S3 and S4

1 of 10

08.02.2017 22:24

Google's R Style Guide

https://google.github.io/styleguide/Rguide.xml

Notation and Naming
File Names
File names should end in .R and, of course, be meaningful.
GOOD: predict_ad_revenue.R
BAD: foo.R
Identifiers
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.
variable.name is preferred, variableName is accepted
GOOD: avg.clicks
OK: avgClicks
BAD: avg_Clicks
FunctionName
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).
kConstantName
Syntax
Line Length
The maximum line length is 80 characters.
Indentation

2 of 10

08.02.2017 22:24

Google's R Style Guide

https://google.github.io/styleguide/Rguide.xml

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.
Spacing
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.prior <- table(df[df$days.from.opt < 0, "campaign.id"])
total <- sum(x[, 1])
total <- sum(x[1, ])
BAD:
tab.prior <- table(df[df$days.from.opt<0, "campaign.id"]) # Needs spaces around '<'
tab.prior <- table(df[df$days.from.opt < 0,"campaign.id"]) # Needs a space after the comma
tab.prior<- table(df[df$days.from.opt < 0, "campaign.id"]) # Needs a space before 
Source Exif Data:
File Type                       : PDF
File Type Extension             : pdf
MIME Type                       : application/pdf
PDF Version                     : 1.5
Linearized                      : No
Page Count                      : 10
Creator                         : cairo 1.9.5 (http://cairographics.org)
Producer                        : cairo 1.9.5 (http://cairographics.org)
EXIF Metadata provided by EXIF.tools

Navigation menu