Overview
Writing clean, and consistent code is crucial for effective collaboration and reproducibility. Following a standardized coding style not only makes your code easier to understand but also helps avoid errors. This guide outlines key best practices for R coding style and is divided into two sections: Syntax and Functions.
Familiarize yourself with the basics before diving into coding style:
Syntax
Syntax refers to the foundational rules that determine how your code should be written and structured. Following good syntax helps ensure that your code is not only function but also readable and maintainable.
Object names
- Variable and function names should consist only of letters, numbers, and underscores
(_). Avoid using dots in names to avoid confusion with base R function names. Bothsnake_caseandCamelCaseare also fine.
- Variable names should be as concise and meaningful as possible:
Spacing
Make code easier to read, place a space:
- After commas:
- After function arguments:
- Inside curly braces
{ }when using them to embrace operators:
- Surround infix operators
(=, +, -, <-, etc.)with spaces:
Function calls
- Omit argument names for frequently used data arguments unless it adds clarity.
- Avoid complex assignments within control flow statements:
# Good
x <- complicated_function()
if (nzchar(x) < 1) {
# do something
}
# Bad
if (nzchar(x <- complicated_function()) < 1) {
# do something
}
Control flow
Curly brackets { } define the hierarchy of your code, so it's important to use them clearly.
- Place `{` on the same line as the opening statement, as the last character.
- The code inside the brackets should be placed on the next line and indented by two spaces.
- The `}` should be the first character on the line. If it is the closing bracket, it should appear on its own line, without indentation.
If-else statements
- When using an
ifstatement, theelseshould be placed on the same line as the closing}of theifblock.
R
if (x > 0) {
print("Positive number")
} else {
print("Non-positive number")
}
- If the statement is straightforward, you can simplify the code by writing the entire statement on one line.
R
temperature <- if (x > 20) "nice" else "cold"
Functions
Functions are fundamental for encapsulating and reusing code, allowing you to structure your project more efficiently.
Naming functions
Functions should have descriptive names that indicate their actions. Try using verbs for function names. For variables, that represent data or state, nouns are more appropriate.
Explicit returns
Be explicit when returning values from functions. While R implicitly returns the last evaluated expression, using return() improves clarity, especially in complex functions.
Avoid using attach() as it increases the likelihood of creating errors dramatically, due to unintended variable masking or overwriting the workspace.
Further reading
In general, this guide follows the Google's R Style Guide, which is based (and thus, so are we) on the Tidyverse Style Guide. We align with those guides but have a few exceptions worth noting.
-
We do not follow their naming conventions:
- Do not use dots
- Underscores or CamelCase are fine.
-
Our conventions for line length differ. See our principles for good coding for more details.
-
We do not require that all functions have a comment block describing their uses. We encourage you to do so when the purpose of a function is not clear or whenever the function is used outside of the file in which it is defined.
Summary
Whether you are working solo or within a team, these best practices ensure that your R code is efficient and easy to understand.