Chapter 17. Making Packages

R’s success lies in its community. While the R Core Team does a fantastic job, it’s important to realize that most R code is written by users. In this chapter, you’re going to learn how to create your own packages to share your code with your colleagues, friends, and the wider world. Even if you’re a lone-working hermit who doesn’t like sharing, packages are a great way to organize code for your own use.

Chapter Goals

After reading this chapter, you should:

  • Be able to create a package
  • Know how to document its functions and datasets
  • Be able to release the package to CRAN

Why Create Packages?

The natural way to share R code and make it reusable by others (or even just yourself) is to package it up. In my experience, a lot of R users delay learning about how to create their own packages, perceiving it to be an advanced topic. In reality, it’s a simple task—as long as you follow the prescribed rules. These rules are laid out in the “Writing R Extensions” manual that ships with R. If things go wrong, the answer is invariably buried within that document.

Prerequisites

Building packages requires a bunch of tools that are standard under Linux and other Unix derivatives, but not on Windows. All the tools have been collected together in a single download, available at http://cran.r-project.org/bin/windows/Rtools (or the bin/windows/Rtools directory of your nearest CRAN mirror). For even easier installation, use install.Rtools in the installr package.

While you’re installing ...

Get Learning R now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.