Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What best practices do you use for programming in R? [closed]

Tags:

r

What are some good practices for programming in R?

Since R is a special-purpose language that I don't use all the time, I typically just hack together some quick scripts that do what I need.

But what are some tips for writing clean and efficient R code?

like image 394
Frank Avatar asked Feb 13 '10 15:02

Frank


People also ask

Which tool is used for R programming?

1) RStudio Most R users have probably heard of RStudio. It's by far one of the most popular R tools in existence and you probably already have it.


2 Answers

You already provide some hints by stating your approach is 'hack quick scripts'. If you want best practices and structure, simple follow the established best practices from CRAN:

  • create a package, this opens the door to running R CMD check which is very useful
  • as many people have stated, having a package helps you in the code writing stage too as you are somewhat forced to document the code; that is a Good Thing (TM)
  • once you have a package, add code in the \examples{} section of the documentation as this will be running during R CMD check and provides an easy entry to regression testing
  • once you get used to regression testing, start to use a package such as RUnit; that really is best practices
  • JD's pointer to the Google Style Guide is a good one too. That isn't the only style guide as e.g. Henrik's R Coding Convention precedes it by a few years; and there is also Hadley's riff on Google's style guide
  • Otherwise, the oldie-but-goldie 'do what your colleagues and coauthors do' also applies
like image 70
Dirk Eddelbuettel Avatar answered Sep 17 '22 19:09

Dirk Eddelbuettel


I recommend Josh Reich's Load, Clean, Func, Do workflow from this previous question.

In addition I recommend following coding guidelines such as Google's R Style Guide. Using a coding style guide makes reading the code later so much easier.

like image 22
JD Long Avatar answered Sep 21 '22 19:09

JD Long