22 Creating a package

 

This chapter covers

  • Creating the functions for a package
  • Adding package documentation
  • Building the package and sharing it with others

In previous chapters, you completed most tasks by using functions that were provided by others. The functions came from packages in the base R installation or from contributed packages downloaded from CRAN.

Installing a new package extends R’s functionality. For example, installing the mice package provides you with new ways of dealing with missing data. Installing the ggplot2 packages provides you with new ways of visualizing data. Many of R’s most powerful capabilities come from contributed packages.

Technically, a package is simply a set of functions, documentation, and data saved in a standardized format. A package allows you to organize your functions in a well-defined and fully documented manner and facilitates sharing your programs with others.

There are several reasons why you might want to create a package:

  • To make a set of frequently used functions easily accessible, along with the documentation on how to use them.
  • To create a set of examples and datasets that can be distributed to students in a classroom.
  • To create a program (a set of interrelated functions) that can be used to solve a significant analytic problem (such as imputing missing values).
  • To promote research reproducibility by organizing study data, analysis code, and documentation into a portable and standardized format.

22.1 The edatools package

22.2 Creating a package

22.2.1 Installing development tools

22.2.2 Creating a package project

22.2.3 Writing the package functions

22.2.4 Adding function documentation

22.2.5 Adding a general help file (optional)

22.2.6 Adding sample data to the package (optional)

22.2.7 Adding a vignette (optional)

22.2.8 Editing the DESCRIPTION file