Building basic packages in GNU R


Whether you would like to share your code and data with other people or simply pack up your code in a concise way, the ability of building a custom package in GNU R may come useful to you. In this article we will outline as clearly as possible the process of building a basic package in R. This does not include more advanced knowledge on building R packages. This tutorial, however, will get you started. You may also find How to install and use packages in GNU R of help if you are not familiar with using R packages at all.

Creating a package structure

Every package consists of a set of functions that are programmed to apply with a common aim. Additionally, a sample data is often provided with the package in R. Let us now propose a simple example. Below we defined four R objects: two functions div() and pow() and two data sets in a form of two vectors data1 and data2.

> div<-function(a,b) a/b
> pow<-function(a,b) a^b
> data1<-c(1,4)
> data2<-c(7,2)

The structure (template) of a package in R is easily obtained by execution of the function package.skeleton() as indicated below. As arguments we pass the list of previously defined R objects and the name of the newly created package.

> package.skeleton(list=c("div","pow","data1","data2"), name="exampleRpackage")
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Further steps are described in './exampleRpackage/Read-and-delete-me'.

As a result a directory and file structure of an R package was created. All the respective files and directories where located in the directory called exampleRpackage, named after the specified package.

The two vector sample data was located into ./exampleRpackage/data/ directory, while the function definitions in ./exampleRpackage/R/. The ./exampleRpackage/man/ directory consists of the template manuals corresponding to the defined objects and the package in general and are self explanatory. The DESCRIPTION file was also created. This file includes all the important package information as the package version, the date of creation, etc. That is:

Package: exampleRpackage
Type: Package
Title: What the package does (short line)
Version: 1.0
Date: 2013-02-15
Author: Who wrote it
Maintainer: Who to complain to <>
Description: More about what it does (maybe more than one line)
License: What license is it under?

Checking and Building a package

Once the a basic package is created there is a need to check and build it. This can be done from the Linux command line using the R CMD check and R CMD build command. That is:

$ R CMD check exampleRpackage


$ R CMD build exampleRpackage

The build command returns the exampleRpackage_1.0.tar.gz ready for installation.


Creating and building a package in R can be much more complex than the simple example described in this tutorial. There is a number of issues and conventions that need to be addressed when creating a package. This tutorial will help you to create a package for your own use. In order to build and submit a package to CRAN it is necessary to study the R conventions in more detail. We refer to Writing R Extensions for a comprehensive guide for creating a package in R.

GNU R tutorial series:

Part I: GNU R Introductory Tutorials:

  1. Introduction to GNU R on Linux Operating System
  2. Running GNU R on Linux Operating System
  3. A quick GNU R tutorial to basic operations, functions and data structures
  4. A quick GNU R tutorial to statistical models and graphics
  5. How to install and use packages in GNU R
  6. Building basic packages in GNU R

Part II: GNU R Language:

  1. An overview of GNU R programming language