views:

249

answers:

2

I'm working with a small team of analysts and statisticians on what will be a medium-sized body of R code. They're smart people, but they're not trained or experienced as programmers, per se. (I am.) They've written some R code, but for our project to be expandable, efficient, and maintainable, it needs to become well-structured, and rather more piratical. One of the better way to learn to be a better programmer is to study elegant existing code. Can anyone suggest some open source examples of R code (on CRAN or wherever) that you think are particularly clear, literate, and good examples? Functional is good, S3 objects are OK, deep magic is bad.

+4  A: 

My two favorite packages can both be browsed on R-Forge and are very well documented (although they may be too big for an introduction):

I think that the Google style guide does a great job of capturing the style of the Core team, although Hadley has his own style guide which can be read if you're looking at his packages. You can browse Hadley's packages on Github (and his homepage is full of useful content), in particular:

This article on the R-Wiki is also a good read for seeing ways to optimize code.

Shane
Well, I looked at caret and zelig, and for the most part, they're not well commented at all. It looks like one developer in caret likes comments, but that's about it. I'm pretty familiar with the guts of ggplot2, and although it's a great package to use, and the code is mostly elegant and relatively well commented, it uses obscure proto objects and dynamic creation of functions a lot, so I can't consider it literate at all. Optimization, beyond what falls out of good coding, is a separate issue. Will look at Hadley's other packages...
Harlan
Of my packages - the more recent the better. I'd recommend `plyr` and (very recent) `stringr`. In my opinion they are largely well written and elegant. The quality of my code really improved after I started teaching others how to write good code!
hadley
A: 

Not strictly related, but make sure you get them used to using Source Control (perforce, subversion, git, rcs, etc) as quickly as possible. That reduces the learning pains.

Toby Allen
Yep, good advice. They knew about and were using git before I joined the team...
Harlan