Chapter 4 R Style Guide
4.1 Files
4.1.1 Names
File names should be meaningful and end in .R. Avoid using special characters in file names - stick with numbers, letters,
-
, and_
.If files should be run in a particular order, prefix them with numbers. If it seems likely you’ll have more than 10 files, left pad with zero:
Prefer file names that are all lower case, and never have names that differ only in their capitalization.
4.2 Naming Convention
Variable and function names should use only lowercase letters, numbers, and
_
. Use underscores (_
) (so called snake case) to separate words within a name.It’s better to reserve dots exclusively for the S3 object system. In S3, methods are given the name
function.class
.Generally, variable names should be nouns and function names should be verbs. Strive for names that are concise and meaningful (this is not easy!).
Avoid re-using names of common functions and variables. This will cause confusion for the readers of your code.
4.3 Spacing
4.3.1 Indentation
Use four spaces.
4.3.2 Commas
Always put a space after a comma, never before, just like in regular English.
4.3.3 Parentheses
Do not put spaces inside or outside parentheses for regular function calls.
Place a space before and after
()
when used withif
,for
, orwhile
.Place a space after
()
used for function arguments:
4.3.4 Embracing
The embracing operator, {{ }}
, should always have inner spaces to help
emphasise its special behaviour:
# Good
max_by <- function(data, var, by) {
data %>%
group_by({{ by }}) %>%
summarise(maximum = max({{ var }}, na.rm = TRUE))
}
# Bad
max_by <- function(data, var, by) {
data %>%
group_by({{by}}) %>%
summarise(maximum = max({{var}}, na.rm = TRUE))
}
4.3.5 Infix operators
Most infix operators (==
, +
, -
, <-
, etc.) should always be surrounded
by spaces:
# Good
height <- (feet * 12) + inches
mean(x, na.rm = TRUE)
# Bad
height<-feet*12+inches
mean(x, na.rm=TRUE)
There are a few exceptions, which should never be surrounded by spaces:
The operators with high precedence:
::
,:::
,$
,@
,[
,[[
,^
, unary-
, unary+
, and:
.Single-sided formulas when the right-hand side is a single identifier.
Note that single-sided formulas with a complex right-hand side do need a space:
When used in tidy evaluation
!!
(bang-bang) and!!!
(bang-bang-bang)
4.4 Function Calls
4.4.1 Named arguments
If you override the default value of an argument, use the full name.
You can omit the argument names for very common arguments
4.4.2 Assignment
Avoid assignment in function calls:
# Good
x <- complicated_function()
if (nzchar(x) < 1) {
# do something
}
# Bad
if (nzchar(x <- complicated_function()) < 1) {
# do something
}
The only exception is in functions that capture side-effects:
4.4.3 Long lines
There are two options if the function name and definition can’t fit on a single line:
Function-indent: place each argument on its own line, and indent to match the opening
(
of function:Double-indent: Place each argument of its own double indented line.
In both cases the trailing )
and leading {
should go on the same line as
the last argument.
Prefer function-indent style to double-indent style when it fits.
These styles are designed to clearly separate the function definition from its body.
# Bad
long_function_name <- function(a = "a long argument",
b = "another argument",
c = "another long argument") {
# Here it's hard to spot where the definition ends and the
# code begins, and to see all three function arguments
}
4.4.4 Return
Only use
return()
for early returns. Otherwise, rely on R to return the result of the last evaluated expression.Return statements should always be on their own line because they have important effects on the control flow
If your function is called primarily for its side-effects (like printing, plotting, or saving to disk), it should return the first argument invisibly. This makes it possible to use the function as part of a pipe. print methods should usually do this, like this example from httr:
4.5 Control flow
4.5.1 Code blocks
{
should be the last character on the line. Related code (e.g., anif
clause, a function declaration, a trailing comma, …) must be on the same line as the opening brace.The contents should be indented by four spaces.
}
should be the first character on the line.
4.5.2 If Statements
- If used,
else
should be on the same line as}
. &
and|
should never be used inside of anif
clause because they can return vectors. Always use&&
and||
instead.
4.5.3 Inline statement
You can write a simple
if
block on ane single lineFunction calls that affect control flow (like
return()
,stop()
orcontinue
) should always go in their own{}
block:
4.5.4 Implicit type coercion
Avoid implicit type coercion (e.g. from numeric to logical) in if
statements:
4.5.5 Switch statements
Avoid position-based
switch()
statements (i.e. prefer names).Each element should go on its own line.
Elements that fall through to the following element should have a space after
=
.Provide a fall-through error, unless you have previously validated the input.
4.6 Long lines
Strive to limit your code to 80 characters per line.
If a function call is too long to fit on a single line, use one line each for the function name, each argument, and the closing
)
. This makes the code easier to read and to change later.Short unnamed arguments can also go on the same line as the function name, even if the whole function call spans multiple lines.
You may also place several arguments on the same line if they are closely related to each other.
4.7 Semicolons
Don’t put ;
at the end of a line, and don’t use ;
to put multiple commands
on one line.
4.8 Assignment
Use <-
, not =
, for assignment.
4.9 Character vectors
Use "
, not '
, for quoting text. The only exception is when the text already
contains double quotes and no single quotes.
# Good
"Text"
'Text with "quotes"'
'<a href="http://style.tidyverse.org">A link</a>'
# Bad
'Text'
'Text with "double" and \'single\' quotes'
4.10 Logical vectors
Use TRUE
and FALSE
rather than T
and F
.
4.11 Comments
Each line of a comment should begin with the comment symbol #
and a single space.
In data analysis code, use comments to record important findings and analysis decisions. If you need comments to explain what your code is doing, consider rewriting your code to be clearer. If you discover that you have more comments than code, consider switching to R Markdown.
4.12 Resources
- styler: formatting your code according to the tidyverse style guide (or your custom style guide) so you can direct your attention to the content of your code. It helps to keep the coding style consistent across projects and facilitate collaboration.
- lintr: offering static code analysis for R. It checks adherence to a given style, syntax errors and possible semantic issues.
4.4.5 Comments
In code, use comments to explain the “why” not the “what” or “how”. Each line of a comment should begin with the comment symbol and a single space:
#
.Comments should be in sentence case, and only end with a full stop if they contain at least two sentences: