How to Make an R Package

Making an R package is great for speeding up your work flow. If you’re like me, you are always rewriting certain functions over and over in order to start working on your script. If you make them into an R package, a quick library call can get you on to the task at hand. Here’s how it’s done:

1. Install packages

If you haven’t already, install devtools and roxygen2:

install.packages('devtools')
library(devtools)
devtools::install_github('klutometis/roxygen')
library(roxygen2)

2. Set your directory

Make the directory for your package:

setwd('desired_location')
create('package_name')

Note that this will make two folders inside desired_location, man and R. There will also be a description file. The description file, as you might guess, is where you will edit allof the information about the package (version number, R version requirement, etc.). It’s also where you might include some contact and copyright information.

3. Add documentation and functions

There are a number of categories that roxygen2 will allow you fill out in R documentation, such as inputs, values, examples, and more. For example:

#' Multivariate Normal Draws
#'
#' This function draws from a multivariate normal distribution of dimension p more efficiently than the rmvnorm function in the mvtnorm package.
#' @param n The number of draws to make from the multivariate normal distribution. Must be a positive integer.
#' @param mu The mean vector of length p
#' @param V The $p\times p$ covariance matrix
#' @keywords Gaussian, Normal
#' @export
#' @examples
#' rmvn(5,c(2,2),matrix(c(2,-1,-1,5),2,2))
#'           [,1]      [,2]      [,3]     [,4]     [,5]
#' [1,] 0.2962443 3.3602800 0.9445853 2.869058 1.860385
#' [2,] 7.5907838 0.4752729 1.7318177 1.648304 3.482479

rmvn <- function(n,mu=0,V=matrix(1)){
  p <- length(mu)
  if(any(is.na(match(dim(V),p)))){stop("Dimension Problem!")}
  D <- chol(V)
  t(matrix(rnorm(n*p),ncol=p)%*%D + rep(mu,rep(n,p)))
}

Save this as an R script with whatever name inside desired_location/R. For example, DanMVN.R. Note that you can have multiple functions in a single script, and you can give it whatever name you want.

4. Document processing

Now you can utilize the power of roxygen2 to create your R package documentation. Set your working directory to where you saved your script before running the document() command. Then, set your working directory all the way back out to desired_location to install the package that you’ve made:

setwd('R')
document()
setwd('../../')
install('package_name')
library(package_name)

5. Add to your package

Whenever you add more functions to your script, or add more scripts to your package, follow the steps to process your documentation and install your package again to update.

6. Share your package on GitHub

Using the devtools package, you can download any package that is publicly available on GitHub, making sharing a set of functions with a whole team super easy:

install_github("danieladamspencer/DanR/DanR")
library(DanR)