1. IntroductionWhether 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.
2. Creating a package structureEvery 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="" br="">> pow<-function a="" b="" br="">> data1<-c br="">> data2<-c pre=""> 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 ... Done. 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 < email@example.com> Description: More about what it does (maybe more than one line) License: What license is it under?
3. Checking and Building a packageOnce 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 exampleRpackageand
$ R CMD build exampleRpackageThe build command returns the exampleRpackage_1.0.tar.gz ready for installation.
4. ConclusionCreating 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.
Make sure you tune in to our RSS and Linux jobs portal to stay informed about the latest opportunities in the field. Also visit our Linux Forum if you want to share your Linux experiences with us or require additional help.