Site icon R-bloggers

Guidelines for writing good R code

[This article was first published on Philipp Probst, 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.

These guidelines are recommendations and are not meant to be obligatory. Many of the principles are useful and help working and collaborating more efficiently with R. Feel free to add your recommendations or remarks in the discussion section below.

< !--excerpt-->

General rules

Style

Principles

  1. Write good documentation:
    • For external persons the code should be readable and understandable
    • You can write documentation in the code
    • External documentation on how to use the code is also helpful
  2. Write readable code:
    • Sometimes it is better to write a for-Loop than to use the “apply”-function if the code is more readable (and runtime is not an important factor) → short code is not always automatically better
    • Use proper indentation (there is a button for automatic indentation in RStudio)
    • Use names instead of numbers for accessing columns in data
  3. Do not repeat any code (DRY principle):
    • Write functions
    • Write “for” loops
    • Write R-packages for functions that you use in many places
    • Use packages such as devtools and roxygen2 for easy creation and handling of R-packages
    • When prototyping you might harm this guideline but while code review you should think about it
  4. Do not write too long code
    • Put things (code, functions) into other files and “source” them (e.g. if you always use the same functions/libraries at the beginning)
    • Use a proper file system, where you can put files into subfolders
    • This can be supported by using “projects” in RStudio
  5. Do not write R-Code that takes too long
    • Are there faster functions for doing the task?
    • Can you rewrite the code (e.g. with apply, data.table)?
    • Are you really only doing things that are necessary?
    • Is it possible to run it on several CPU-cores?
    • Can you put the slow code in C++ to make it faster? (e.g. with Rcpp)
  6. Remove commented code unless you are sure you want to use it later. It makes code reading much harder and blows your code up. Put yourself a deadline date, from which on you will remove the commented code
  7. Write some tests (if necessary) to check your data:
    • Use stop(), stopifnot() or warning()

Version Control System

Use version control systems (VCS) such as git (or SVN) in combination with e.g. github/gitlab if it is a bigger project.

References

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

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.