Design patterns for R

I've been reading Steve McConnell's Code Complete and came across the section on design patterns (Table 5-1 page 104 2nd Edition).

Have you use design patterns in R? If so, which ones and why? And is it worth it? Any recommendations for further reading (R or FP in general) would be appreciated.

2 Likes

The classical kinds of design patterns, stuff like the flywheel or singleton, as I understand them, are patterns for class-based object-oriented programming. R has different semantics, so it favors a different set of design patterns.

The tidyverse is basically built around a collection of design patterns. There's the fluent function interface where the data to be manipulated is the first function argument, and nearly all functions return a similar kind of object as their input. The functions can be chained together using %>% pipelines, which is another design pattern. (An alternative pattern to piping would require intermediate variables or nested function calls.) The concept of tidy data and tidying R objects into dataframes are also design patterns. The idea of list columns and nested dataframes would be an example of a very recent design pattern for R too.

As for functional programming, Hadley’s Advanced R covers functional programming in R. I learned a lot of what I know about functional programming in R, surprisingly, by learning about functional programming idioms in JavaScript. The two have similar semantics, and a lot more has been written on JavaScript. Here is nice book about functional programming using JavaScript.

6 Likes