Author: Matthew Wolak
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).
From Leisch, F. 2009. Creating R Packages: A Tutorial. In Compstat 2008-Proceedings in Computational Statistics. Brito, P. ed. Physica Verlag, Heidelberg, Germany, 2008.
datasets
package in base RThere 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()
utils
base package. There are a number of related functions, but this one will take objects in R and create the basic structure for an R packageprompt()
to facilitate the generation of documentation filespromptData()
specifically facilitates generation of documentation for data setsroxygen2
devtools
build()
turns a directory containing an R package structure into a single package source
build_win()
check()
builds and checks a source package, particularly to assure compliance with CRAN policiesrelease()
runs a few tests and submits the package directly to CRAN for consideration to be added to the repositoryinstall_github()
, install_git()
, install_bitbucket()
, install_bioc()
, etc. will download and install packages from GitHub, a git repository, Bitbucket, or Bioconductor repositories, respectively.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
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.
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!
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.
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.
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”).
XCode
(free from the App Store with an Apple ID) or the Command Line Tools for XcodeR
, the development package will also need to be installed. For example, in Debian install the r-base-dev
package. (e.g., sudo apt-get install r-base-dev
)
r-base-core
package (e.g., sudo apt-get install r-base-core
)texlive-full
package (e.g., sudo apt-get install texlive-full
)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!