This work is an adaptation of the tidyverse style guide by Hadley Wickham, based on 2017-11-17 and 2018-05-15 snapshots taken from GitHub [license].


This is the R style guide applied at daqana.

Most of what is typically regarded as a style guide can be found in section 2 Syntax. The first four sections of this guide deal with styling recommendations for scripting in R. The second part deals with R package development. The guide should not be seen as complete and will certainly see further enhancement. A best practices section for coding in R (do avoid these for loops 🙂) or our preferred settings when using RStudio might be added in the future.

Let’s start.

Good coding style is like correct punctuation: you can manage without it, butitsuremakesthingseasiertoread. Just as with punctuation, while there are many code styles to choose from, some are more reader-friendly than others. The style presented here is based on the style guide, which is used throughout the tidyverse, and which in turn is derived from Google’s R style guide and has evolved considerably over the years.

Some decisions genuinely do make code easier to use (especially matching indentation to programming structure) and we try to explain these choices, but many decisions are arbitrary or a matter of taste. The more important thing about a style guide is that it provides consistency if applied consistently.

# Good
Throughout this guide positive examples are shown on a green background.
# Bad
Negative examples are shown on a pink background. 

The colors were chosen from a diverging colorblind friendly scheme (and some transparency has been added).

Tips got a lightgrey background like any inline code. 

lintr and styler

Conformance with a good portion of this style guide can be checked in an automated manner thanks to the lintr package (development version at the time of writing). The availability of specific linters is mentioned in the corresponding sections. A unit test bundling the customized linters is given in the Unit Tests lintr section of this guide.

The styler package not only checks compliance with a specified style guide but also applies its rules to any discrepancies encountered. The package evolved from a Google Summer of Code project in 2017 and is under active development. It is tidyverse-oriented, i.e. its rules so far ensure compliance with aspects of the tidyverse style guide. Minor aspects are directly customizable, others would require their own rules to be written. We are planning to bundle the existing customized rules into a daqana_style() function and might extend it to widly cover this guides recommendations. The rules already covered (spaces, indention, line breaks and tokens) will be mentioned throughout the text.

RStudio add-ins exist for both packages with default settings. To customize the linter add-in (on a per project basis) add the daqana_linters.R file (detailed in section 6) and a .lintr config file to your RStudio project directory. The config file should simply look like the following (mind the newline):

linters: source("daqana_linters.R"); daqana_linters 

At daqana with our internal package dqutils(>=0.0.26) you can easily edit the .lintr config file to look like the following instead and ignore saving daqana_linters.R in your package directory.

linters: dqutils::daqana_linters

last modified: 2018-12-10