Table of Contents

R style guide

R code that is submitted to be included in any of the R tools developed at BSC should follow these style guidelines, based on the tidyverse style guide:

Examples

# Proper spacing, indentation spaces and text quotes:
NewFunction <- function(text = "default", uppercase = TRUE) {
  # Check uppercase parameter
  if (!is.logical(uppercase)) {
    stop("Parameter 'uppercase' should be TRUE or FALSE.")
  }
  # Only transform text if needed
  if (uppercase && is.character(text)) {
  text <- toupper(text)
  }
  return(text)
}
# How to format line breaks to avoid long lines:
my_strings <- list(one = "un",
                   two = "dos",
                   three = "tres",
                   four = "quatre")
# or:
my_strings <- list(
  one = "un",
  two = "dos",
  three = "tres",
  four = "quatre"
)

Other resources

For more tips on writing clean and easy to read code, check the tidyverse style guide. You can use the styler R package to automatically style your code, and it can even be integrated in RStudio!