2  ESQlabs’ R Style Guide

At ESQlabs, we follow the tidyverse style guide with very few changes. The following sections outline the most important differences with the Tidyverse guide.

2.1 Syntax

2.1.1 Naming Conventions

For simplicity, users should use snake_case as recommended by the tidyverse style (this is not the case for developers that should follow the OSPS-R coding standards).

However the naming style depends on the type of object: 

  • Variable and function names should use only letters and numbers. Use snake_case to separate words within a name:

    # Good
    fit_model <- function() {
      # code
    }
    results_df <- data.frame()
  • True constant variables should use ALL_CAPS casing:

    # Good
    CONSTANT_VAR <- 1
  • Use short meaningful and understandable names. The code should read as a story and only some well-known abbreviations should be used:

    # Good
    pk_data <- read_excel("pkDataFile.xls")
    
    # Bad
    pharmacokinetics_data <- read_excel("pkDataFile.xls") # Too long

2.1.2 Spacing

Because it is not explicitly said in the tidyverse guideline and for improved code readability, use the following indentation settings:

  • Use spaces instead of tabs
  • Use indentation width of 2

You can check that these settings are correctly set in Tools > Global Options > Code > Editing:

2.2 Functions

2.2.1 return()

Prefer using return() for explicitly returning result, although you can rely on R to implicitly return the result of the last evaluated expression in a function.

2.3 Line length

Avoid having long lines. Restrict yourself to 120 characters (instead of the usual limit of 80 characters).