How to create R Package with RStudio

Creating a new R package with pretty simple with RStudio. In a nutshell, packages are the fundamental units of reproducible R code. Generally, packages include reusable R functions, the documentation that describes how to use them, and sample data. As of January 2016, there were over 7,800 packages available on the Comprehensive R Archive Network, or CRAN, the public clearing house for R packages.

Step 1: Create New R Package
RStudio R Package

RStudio R Package

RStudio R Package

Go to File>New Project>New Directory>R Package. Provide the name for your package and RStudio will create a template for R Package.

The extracted sources of an R package are simply a director on your hard drive. A file named DESCRIPTION with descriptions of the package, author, and license conditions in a structured text format that is readable by computers and by people.

Besides that, there is man/ subdirectory of documentation files, an R/ subdirectory of R code and a data/ subdirectory of datasets.

Step 2: Write the documentation and R Package
Create a new R file for your package which consists of all your R function codes. You may include all your documentation inside the R code. This will be displayed in the R documentation.

Sample of code (modified from this tutorial)

#  linEst.R
#' A simple function which computes the OLS estimate 
#' @param x a numeric design matrix for the model
#' @param y a numeric vector of responses
#' @param formula a symbolic description of the model to be fit
#' @return An object of class with list of elements
#' @author Kuan Hoong
#' @details function which computes the OLS estimate
#' @seealso \code{lm}
#' @export

linEst <- function(x, y)
{
  ## compute QR-decomposition of x
  qx <- qr(x)

  ## compute (x'x)^(-1) x'y
  coef <- solve.qr(qx, y)

  ## degrees of freedom and standard deviation of residuals
  df <- nrow(x)-ncol(x)
  sigma2 <- sum((y - x%*%coef)^2)/df

  ## compute sigma^2 * (x'x)^-1
  vcov <- sigma2 * chol2inv(qx$qr)
  colnames(vcov) <- rownames(vcov) <- colnames(x)
  list(coefficients = coef,
       vcov = vcov,
       sigma = sqrt(sigma2),
       df = df)
}

Sample of DESCRIPTION file:

Package: linEst
Type: Package
Title: Minimal R function for linear regression
Version: 1.0
Date: 2016-01-20
Author: Kuan Hoong
Maintainer: Kuan Hoong 
Description: This is a demo package

License: GPL-3
LazyData: TRUE

You need to edit the DESCRIPTION file to include all the related information related to your package.

Step 3: Configure Build Tools
RStudio R Package

05
Next step is to configure the Build Tools. Select “Generate Documentation with Roxygen” and in the configuration select “Build & Reload”.

Last time is to click the “Build & Reload” button to build your R Package.

RStudio R Package

Step 4: Build & Reload

Once your R Package has been successfully built, you may call your package by calling the library.

library(linEst)

#check the description
library(help=linEst)

#read the R Documentation
?linEst

Step 5: Run the library

For example you may run the library with the CATS dataset available from the MASS package.

#to run the package with cats data
data(cats, package="MASS")
linEst(cbind(1, cats$Bwt), cats$Hwt)

Step 6: Submitting to CRAN
Lastly, if you would like to “submit” a package to CRAN, check that your submission meets the CRAN Repository Policy and then use the web form.

Leave a comment