R Style Guide
Having a systematic and consistent guide is important for programming, so does in R.
Here are some recommended style guides for programming in R.
Pick one of them and strictly follow it.
1. Google’s R Style Guide
- Link to Google’s R Style Guide (official URL, pdf)
- Providing 14 R Style Rules including filenames, identifiers, indentation, spacing etc.
“These rules were designed in collaboration with the entire R user community at Google”
- Link to R Coding Conventions
- Developed since 2002. It’s version 0.9 (January 2009)
- Please read this message in the introduction part before applying this coding convention.
“ Please note that this document is under construction since mid October 2002 and should still be seen as a first rought draft. There is no well defined coding recommendations for the R language  and neither is there a de facto standard. This document will give some recommendations, which are very similar to the ones in the Java programming style , which have found to be helpful for both the developer as well as the end user of packages and functions written in R.“
3. Hadley Wickham’s Style Guide
- Link to his R style guide at GitHib wiki of devtool package and had.co.nz
- Mainly based on the Google’s style.
Here is what he mentions in the introduction.
“Good coding style is like using correct punctuation when writing: you can manage without it, but it sure makes things easier to read. As with punctuation, there are many possible variations, and the main thing is to be consistent. The following guide describes the style that I use – you don’t have to use it, but you need to have some consistent style that you do follow. My style is based on Google’s R style guide, with a few tweaks.Good style is important because while your code only has one author, it will usually have multiple readers, and when you know you will be working with multiple people on the same code, it’s a good idea to agree on a common style up-front.”
4. R Core’s coding standard
5. formatR package
- Links to the package’s CRAN , GitHub site
- One can also use Yihui’s formatR package to tidy R codes with function tidy.source().
- Package description is as follows
- long lines of code and comments are reorganized into appropriately shorter ones
- spaces and indent are added where necessary
- comments are preserved in most cases
- the number of spaces to indent the code (i.e. tab width) can be specified (default is 4)
elsestatement in a separate line without the leading
}will be moved one line back
=as an assignment operator can be replaced with
6. The State of Naming Conventions in R
by Rasmus Bååth in The R Journal (pdf)
(Added on 15 December 2012)