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.

Tip

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. Both snake_case and CamelCase are also fine.

#Good
 school_type
 SchoolType

 #Bad
 school.type
  • Variable names should be as concise and meaningful as possible:

#Good
day_one

#Bad
first_day_of_the_month
d1mth

Spacing

Make code easier to read, place a space:

  • After commas:

x[, 1]
  • After function arguments:

# Good
function(x) {}

# Bad
function (x) {}
function(x){}
  • Inside curly braces { } when using them to embrace operators:

group_by({{ by }}) %>%
summarise(maximum = max({{ var }}, na.rm = TRUE))
  • Surround infix operators (=, +, -, <-, etc.) with spaces:

mean(x, na.rm = TRUE)

Function calls

  • Omit argument names for frequently used data arguments unless it adds clarity.

# Good
mean(1:10, na.rm = TRUE)

# Bad
mean(x = 1:10, , FALSE)
mean(, TRUE, x = c(1:10, NA))
  • 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 (y == 0) {
  if (x > 0) {
    log(x)
  } else {
    message("x is negative or zero")
  }
} else {
  y^x
}
Tip

If-else statements

  • When using an if statement, the else should be placed on the same line as the closing } of the if block.

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.


# Good
add_row()
permute()

# Bad
row_adder()
permutation()

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.


# Good
AddValues <- function(x, y) {
return(x + y)
}

# Bad
AddValues <- function(x, y) {
x + y
}
Tip

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.