An R package typically consists of a coherent collection of functions and data
structures that are suitable for addressing a particular problem. Packages
are easy to distribute and share with others, and in many ways writing a
package is the best and most eﬀective way to share your software and ideas
with others. Learning to move away from collections of scripts, or functions,
that get sourced to software organized in packages is very enabling. Writing
your ﬁrst package can be a challenge, but the second and third are simpler.
There are many advantages to writing a package, including the fact that
everyone you collaborate with can be using precisely the same code and all
can contribute to its quality and usability.
In many cases, developers will either have access to existing co de written
in some other language, or they may ﬁnd that they need to implement some
parts of their software in C, or some other compilable language, for eﬃciency.
This topic is covered in Chapter 6.
The many hundreds of contributed packages provide a great resource, and
for many problems you will simply want to ﬁnd, download, install and use
packages written by others. There are currently three main repositories for
R packages: CRAN, Bioconductor and Omegahat. CRAN contains the most
R packages (over 1000) while Bioconductor and Omegahat are smaller. The
number of packages is somewhat overwhelming and two related innovations,
CRAN Task Views (the ctv package) and the biocViews packages, provide
to ols that can be used to provide overviews of packages suitable for diﬀerent
subject areas. Both approaches use restricted sets of terms to help orga-
nize packages. We will discuss in Section 7.2.1 some of the ways in which
biocViews can be used and readers who visit the Bioconductor web site will
see it in action, as the biocViews terms are used to organize the Bioconductor
In this chapter we ﬁrst review some of the available functionality for using
packages and then discuss how to write your own packages. For most of the
topics we will discuss, the R Extensions Manual (R Development Core Team,
2007c) is a more comprehensive reference.