• Pack YouR Code by Gaston Sanchez.
  • Copyright 2018-2021. All rights reserved.

Full Edition

The full edition (in PDF and EPUB formats) can be purchased at leanpub:


Preview Edition

This is now a preview of the first four chapters:

  1. Let’s Toss a Coin
  2. Tossing Function
  3. Coin Objects
  4. Methods (part 1)
  5. Object toss
  6. Methods (part 2)
  7. Additional Methods
  8. Toy Package
  9. Filestructure
  10. Description File
  11. Namespace
  12. Vignettes
  13. Package States
  14. Packaging Flow
  15. Sharing


The ultimate goal of this book is to teach you how to create a relatively simple R package based on the so-called S3 classes.

The chapters of the book are organized in four major parts:

  • The first part involves describing a motivating example about using R to write code for tossing a coin. We begin tackling this problem from a classic programming style (as opposed to a more object-oriented style).

  • The second part is dedicated to implement code in a more object-oriented programming (OOP) fashion by using S3-class objects. Keep in mind that the S3 system is a very informal OOP approach. However, it is the most common system among the universe of R packages.

  • The third part discusses the anatomy of an R package, and how the various files are organized in a formal filestructure. The purpose of this part is to review the main type of directories, subdirectories, and files ina package, as well as some usual components that are worth including in a package.

  • The fourth part of the book has to do with the actual process of building a package. We describe the differrent possible states, showing an opinionated flow for building a package.

About this book

The main reason for me to write this book was the lack of a teaching resource that I could use with the students in my computational statistics courses at the University of California Berkeley (e.g. Stat 133, 159, 243, 259). Not only I saw the need, but also the opportunity, to refine a couple of tutorial documents that I had written for those courses. I hope that this book can help not only students in my courses but also many other useRs that are interested in creating R packages.

This book assumes a couple of things about you: familiarity with R in general, and more specifically with RStudio. You should have used .R (R script) and .Rmd (R markdwon) files before. You don’t need to be an expert useR but you do need to feel comfortable working with various data structures: vectors, factors, arrays, matrices, lists, data frames, etc. Also, I assume that you have some basic programming experience: for example, you know how to create simple functions, you know how to use conditional structures like if-then-else, as well as loop structures such as for() loops or while() loops. And that you also have some experience writing tests of functions (ideally using the package "testthat").

Other Resources

This is NOT a comprehensive text that covers every single aspect about creating R packages. Instead, I’ve written this book more in the spirit of an extended tutorial or guide document with a relatively simple working example.

If you are interested in the nitty gritty aspects about R packages, then you should read Hadley Wickham’s excellent book on R Packages. In fact, in various chapters of Pack YouR Code, I will be constantly referring to the book R Packages in the form of links like this: r-pkgs: topic; for example: r-pkgs: Introduction.

I also recommend reading Friedrich Leisch’s manuscript Creating R Packages: A Tutorial. Personally, this tutorial helped me a lot to successfully complete creating my first R package in the spring of 2009, after several (enormously frustrating) failed attempts during 2007 and 2008. It’s impressive to have witnessed how the package-creation process has been made more smooth since those years.

Another classic book with material about R packages is Software for Data Analysis by John Chamber, the “master mind” behind the S language, on which the R language is based on.

If R Packages is not enough for you, and you are still curious about more obscure and esoteric details, then you need to check the technical R manual Writing R Extensions (by the R Core Team). I should warn you: this is NOT a beginner friendly document.