Skip to content

R Style Guide

useR! logo

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.

[update] There is a research showing that underscore  allow programmers tracking the code easier. (link)

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”

2. Henrik Bengtsson’s R Coding Conventions

  • 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 [1] 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 [2][3], 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

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

“The formatR package was designed to reformat R code to improve readability; the main workhorse is the function tidy.source(). Features include:

  • 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)
  • an else statement 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)

More posts about R

10 Comments Post a comment
  1. greg #

    someone should write a real R linter that incorporates style guidelines… I know about the lint function in the svTools package (I think) but it does not incorporate style. I would also like to have this as a R independent script in Python or something so that an language can call the linter without having to load R. That would be really great for automatic linting in Textmate, Sublime Text 2 etc…

    June 6, 2012
    • I think it will coming soon. R and its applications have been enormously developed so quickly e.g., RStudio and new packages.

      June 7, 2012
  2. I have one in the works that I will release an alpha package in the next few days.

    June 7, 2012
    • Hi Andrew,

      That’s great, looking forward for it.
      Please let us know once you release it, then I will add it to the list above.

      June 8, 2012
  3. tony #

    I think someone should make coding style consistent for all the languages. I’m now using two-space indentation with python programming, the same as R coding.

    June 20, 2012
  4. Wishing for that as well However, we can begin with being consistent ourselves like you are.

    June 20, 2012
  5. Hello, just wanted to say, I enjoyed this post. It was inspiring.
    Keep on posting!

    louboutin pas chere femme

    March 31, 2014

Trackbacks & Pingbacks

  1. The 1st Year of and its future? | Pairach Piboonrungroj
  2. 2012 in review by The | Pairach Piboonrungroj
  3. google r style guideFree Tutorials and Guides | Free Tutorials and Guides

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: