🖊 R Coding Style Guide

[This article was first published on Iegor Rudnytskyi, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Language is a tool that allows human beings to interact and communicate with each other. The clearer we express ourselves, the better the idea is transferred from our mind to the other. The same applies to programming languages: concise, clear and consistent codes are easier to read and edit. It is especially important, if you have collaborators, which depend on your code. However, even if you don’t, keep in mind that at some point in time, you might come back to your code, for example, to fix an error. And if you did not follow consistently your coding style, reviewing your code can take much longer, than expected. In this context, taking care of your audience means to make your code as readable as possible.

Good coding style is like using correct punctuation. You can manage without
it, but it sure makes things easier to read.
Hadley Wickham

There is no such thing as a “correct” coding style, as there is no such thing as the best color. At the end of the day, coding style is a set of developers’ preferences. If you are coding alone, sticking to your coding style and being consistent is more than enough. The story is a bit different if you are working in a team: it is crucial to agree on a convention beforehand and make sure that everyone follows it.

Even though there is no official style guide, R is mature and steady enough to have an “unofficial” convention. In this post, you will learn these “unofficial” rules, their deviations, and most common styles.

Naming

Naming files

The convention actually depends on whether you develop a file for a package, or as a part of data analysis process. There are, however, common rules:

  • File names should use .R extension.

      # Good
      read.R
    
      # Bad 
      read
    

  • File names should be meaningful.

      # Good 
      model.R
        
      # Bad
      Untitled1.R
    

  • File names should not contain / and spaces. Instead, a dash (-) or underscore (_) should be used.

      # Good 
      fir_regression.R
      fir-regression.R
        
      # Bad
      fit regression.R
    

  • If the file is a part of data analysis, then it makes sense to follow the following recommendations:

  • Use meaningful verbs for file names.

      # Good 
      validate-vbm.R
        
      # Bad
      regression.R
    

  • If files should be run in a particular order, then use ascending names.

      01-read.R
      02-clean.R
      02-plot.R
    

  • If the file is used in a package, then slightly different rules should be folowed:

    Naming variables

    Naming functions

    Many points of naming variables are similar for naming functions:

    Naming S4 classes

    Class names should be nouns in CamelCase with initial capital case letter.

    Syntax

    Line length

    The maximum length of lines is limited to 80 characters (thanks to IBM Punch Card).

    It is possible to display the margin in RStudio Source editor:

    Spacing

    Curly braces

    Indentation

    ALWAYS indent your code!

    Try a little exercise: paste the following code in your RStudio source editor, select it, and hit Command+I:

    for(i in 1:10) {
    if(i %% 2 == 0)
    print(paste(i, "is even"))
    }
    

    New line

    Comments

    Other recommendations

    If you have read until this moment, you deserve a treat. There is a magic key combination Command+Shift+A that reformats selected code: add spaces and indents it. Do not use it excessively though!

    References

    To leave a comment for the author, please follow the link and comment on their blog: Iegor Rudnytskyi.

    R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

    Never miss an update!
    Subscribe to R-bloggers to receive
    e-mails with the latest R posts.
    (You will not see this message again.)

    Click here to close (This popup will not appear again)