3.2 R Packages
The objectives of this section are:
- Recognize the basic structure and purpose of an R package
- Recognize the key directives in a NAMESPACE file
An R package is a mechanism for extending the basic functionality of R. It is the natural extension of writing functions that each do a specific thing well. In the previous chapter, we discussed how writing functions abstracts the behavior of a set of R expressions by providing a defined interface, with inputs (i.e. function arguments) and outputs (i.e. return values). The use of functions simplifies things for the user because the user no longer needs to be knowledgeable of the details of the underlying code. They only need to understand the inputs and outputs.
Once one has developed many functions, it becomes natural to group them in to collections of functions that are aimed at achieving an overall goal. This collection of functions can be assembled into an R package. R packages represent another level of abstraction, where the interface presented to the user is a set of user-facing functions. These functions provide access to the underlying functionality of the package and simplify the user experience because the one does not need to be concerned with the many other helper functions that are required.
R packages are a much better way to distribute code to others because they provide a clean and uniform user experience for people who want to interact with your code. R packages require documentation in a standardized format, and the various tools that come with R (and RStudio) help to check your packages so that they do not contain inconsistencies or errors. R users are already familiar with how to use R packages, and so they will be able to quickly adopt your code if is presented in this format.
This chapter highlights the key elements of building R packages. The fine details of building a package can be found in the Writing R Extensions manual.
3.2.1 Basic Structure of an R Package
An R package begins life as a directory on your computer. This directory has a specific layout with specific files and sub-directories. The two required sub-directories are
R, which contains all of your R code files
man, which contains your documentation files.
At the top level of your package directory you will have a
DESCRIPTION file and a
NAMESPACE file. This represents the minimal requirements for an R package. Other files and sub-directories can be added and will discuss how and why in the sections below.
While RStudio is not required to build R packages, it contains a number of convenient features that make the development process easier and faster. That said, in order to use RStudio for package development, you must setup the environment properly. Details of how to do this can be found in Roger’s RStudio package development pre-flight check list.
3.2.2 DESCRIPTION File
The DESCRIPTION file is an essential part of an R package because it contains key metadata for the package that is used by repositories like CRAN and by R itself. In particular, this file contains the package name, the version number, the author and maintainer contact information, the license information, as well as any dependencies on other packages.
As an example, here is the DESCRIPTION file for the
mvtsplot package on CRAN. This package provides a function for plotting multivariate time series data.
Package: mvtsplot Version: 1.0-3 Date: 2016-05-13 Depends: R (>= 3.0.0) Imports: splines, graphics, grDevices, stats, RColorBrewer Title: Multivariate Time Series Plot Author: Roger D. Peng <firstname.lastname@example.org> Maintainer: Roger D. Peng <email@example.com> Description: A function for plotting multivariate time series data. License: GPL (>= 2) URL: https://github.com/rdpeng/mvtsplot
3.2.3 NAMESPACE File
The NAMESPACE file specifies the interface to the package that is presented to the user. This is done via a series of
export() statements, which indicate which functions in the package are exported to the user. Functions that are not exported cannot be called directly by the user (although see below). In addition to exports, the NAMESPACE file also specifies what functions or packages are imported by the package. If your package depends on functions from another package, you must import them via the NAMESPACE file.
Below is the NAMESPACE file for the
mvtsplot package described above.
export("mvtsplot") import(splines) import(RColorBrewer) importFrom("grDevices", "colorRampPalette", "gray") importFrom("graphics", "abline", "axis", "box", "image", "layout", "lines", "par", "plot", "points", "segments", "strwidth", "text", "Axis") importFrom("stats", "complete.cases", "lm", "na.exclude", "predict", "quantile")
Here we can see that only a single function is exported from the package (the
mvtsplot() function). There are two types of import statements:
import(), simply takes a package name as an argument, and the interpretation is that all exported functions from that external package will be accessible to your package
importFrom(), takes a package and a series of function names as arguments. This directive allows you to specify exactly which function you need from an external package. For example, this package imports the
gray()functions from the
Generally speaking, it is better to use
importFrom() and to be specific about which function you need from an external package. However, in some cases when you truly need almost every function in a package, it may be more efficient to simply
import() the entire package.
With respect to exporting functions, it is important to think through carefully which functions you want to export. First and foremost, exported functions must be documented and supported. Users will generally expect exported functions to be there in subsequent iterations of the package. It’s usually best to limit the number of functions that you export (if possible). It’s always possible to export something later if it is needed, but removing an exported function once people have gotten used to having it available can result in upset users. Finally, exporting a long list of functions has the effect of cluttering a user’s namespace with function names that may conflict with functions from other packages. Minimizing the number of exports reduces the chances of a conflict with other packages (using more package-specific function names is another way).
220.127.116.11 Namespace Function Notation
As you start to use many packages in R, the likelihood of two functions having the same name increases. For example, the commonly used
dplyr package has a function named
filter(), which is also the name of a function in the
stats package. If one has both packages loaded (a more than likely scenario) how can one specific exactly which
filter() function they want to call?
In R, every function has a full name, which includes the package namespace as part of the name. This format is along the lines of
<package name>::<exported function name>
For example, the
filter() function from the
dplyr package can be referenced as
dplyr::filter(). This way, there is no confusion over which
filter() function we are calling. While in principle every function can be referenced in this way, it can be tiresome for interactive work. However, for programming, it is often safer to reference a function using the full name if there is even a chance that there might be confusion.
It is possible to call functions that are not exported by package by using the namespace notation. The
::: operator can be used for this purpose, as in
<package name>:::<unexported function name>. This can be useful for examining the code of an unexported function (e.g. for debugging purposes) or for temporarily accessing some unexported feature of a package. However, it’s not a good idea to make this a habit as such unexported functions may change or even be eliminated in future versions of the package. Furthermore, use of the
::: operator is not allowed for packages that reside on CRAN.
18.104.22.168 Loading and Attaching a Package Namespace
When dealing with R packages, it’s useful to understand the distinction between loading a package namespace and attaching it. When package
A imports the namespace of package
A loads the namespace of package
B in order to gain access to the exported functions of package
B. However, when the namespace of package
B is loaded, it is only available to package
A; it is not placed on the search list and is not visible to the user or to other packages.
Attaching a package namespace places that namespace on the search list, making it visible to the user and to other packages. Sometimes this is needed because certain functions need to be made visible to the user and not just to a given package.
3.2.4 The R Sub-directory
R sub-directory contains all of your R code, either in a single file, or in multiple files. For larger packages it’s usually best to split code up into multiple files that logically group functions together. The names of the R code files do not matter, but generally it’s not a good idea to have spaces in the file names.
3.2.5 The man Sub-directory
man sub-directory contains the documentation files for all of the exported objects of a package. With older versions of R one had to write the documentation of R objects directly into the
man directory using a LaTeX-style notation. However, with the development of the
roxygen2 package, we no longer need to do that and can write the documentation directly into the R code files. Therefore, you will likely have little interaction with the
man directory as all of the files in there will be auto-generated by the
R packages provide a convenient and standardized mechanism for distributing R code to a wide audience. As part of building an R package you design an interface to a collection of functions that users can access to make use of the functionality you provide. R packages are directories containing R code, documentation files, package metadata, and export/import information. Exported functions are functions that are accessible by the user; imported functions are functions in other packages that are used by your package.