R Package Tutorial I Intro to making a basic package


Author: Matthew Wolak

Introduction

The basic idea behind R packages is to make units of code that extend the functionality in the base R program in a way that is both (i) reproducible across computers with different hardware and software configurations and (ii) fully explained with helpful documentation (including example code and maybe also example data).

Why

Definitions

From Leisch, F. 2009. Creating R Packages: A Tutorial. In Compstat 2008-Proceedings in Computational Statistics. Brito, P. ed. Physica Verlag, Heidelberg, Germany, 2008.

Types of packages


Approaches

There are numerous ways to develop, maintain, and share R packages. In the end, use whatever approaches and tools you find to be the most helpful for you. Here are four common tools in an increasing order of the amount each tool controls the entire process

package.skeleton()

roxygen2

devtools

RStudio

RStudio has created a complete environment with tools to help you build R packages. If you want one-stop shopping then see their help/overview on developing R packages using RStudio’s fancy buttons here


Overview of the tutorial

The approach used in the following tutorial will target the middle of the above four options. Namely, we will use a combination of devtools and roxygen2 to build a very simple package.

Part II. Dipping your toe into the water

To begin, we will create the basic directories and files needed for the outline of a package. We will then transform some simple code into a function and put this in the package directory.

The next step is to document the code, which will first be done using the basic .Rd structured files so that you get a sense of what different parts are required for the files that eventually are turned into R help manual pages. This will give you the basics to better understand what is going on in roxygen2, as discussed in the Part III tutorial.

Following this, we will run some basic checks on the package and then discuss how to host your package and interact with it as a GitHub repository. By the time this part is finished you should have a very basic R package that can be installed and loaded for your and others’ own use and convenience - Woohoo!

Part III. Efficient documentation and code

We will go over how to document your code using roxygen2 instead of manually editing the .Rd files. This will hopefully improve the efficiency of writing code that works and is well documented.

Next we will go through some of the basic checks to ensure your package is now CRAN worthy and how to use devtools to submit a package for hosting on CRAN. At the end of this, you could have a package hosted on CRAN and graduate to being an R wizard!

Finally we will focus on improving the package by briefly covering a few strategies for profiling code in an attempt to identify areas that could benefit from some attention.

Resources

The above approach is my own take on how to make R packages, but it is not the only way (and probably not the best way). I have tremendously benefitted from other people sharing their ideas and have found the following sources extremely helpful.


Preparation

Tools

A good overview of the software requirements for creating R packages (with installation instructions) on Windows, Mac, and Linux are provided here by the RStudio folks to get you setup before starting Part II. Basically, your computer needs something to compile C code and handle the construction of R help manuals from LaTeX code.

You will require a compiler for the C language. Whereas most Linux operating systems should include one automatically, Mac and Windows will not. If you have RStudio installed, you should be covered already. Here is what you should have before starting and note the way to check this at the end of the next section (“Handy packages”).

Handy packages

Below are the two basic R packages necessary for this tutorial. Please have these installed before proceeding with Part II.

install.packages(c("devtools", "roxygen2"))
library(devtools)
library(roxygen2)

devtools to the rescue! To check and make sure you have everything needed, run:

library(devtools)
has_devel()

and make sure it returns TRUE after some gobbledegook!