Building R packages for Windows
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
1. Installing the required tools
To build an R package in Windows, you will need to install some additional software tools. These are summarized at
1.1 Essential: Rtools
This is a collection of unix-like tools that can be run from the DOS command prompt. It also contains the MinGW compilers that are used for compiling Fortran and C code. Download using
You should download and run it, choosing the default “Package authoring installation” to build add-on packages.
1.2 Optional: MikTeX
MikTeX is used for producing the pdf help files. You can produce an R package without it, but the package will not contain pdf help files. Most of you will have this installed anyway. Download from
1.3 Essential: Setting PATH variable
The PATH variable tells Windows where to find the relevant programs. To add a directory to you PATH on Windows XP select
Control Panel -> System -> Advanced -> Environment Variables
The path variable may have already been fixed in step 1.1. In any case, you should check that it looks something like this:
C:Rtoolsbin;C:Rtoolsperlbin;C:RtoolsMinGWbin;C:Program filesRR-2.10.0bin;C:Program FilesHTML Help Workshop;<others>
- The precise directories will depend where you have installed the various tools. The above path should work if you have followed the default installation procedure. If the directory names contain spaces, put them in quotation marks.
- If you have not installed the HTML Help workshop, you will need to
set WINHELP=NO
inMkRules
(in the directory"C:Program filesRR-2.10.0bin"
). - The htmlhelp part can be omitted if you did not install the HTML help workshop.
- I have assumed R2.10.0. For later versions, simply change the above paths to the relevant R version. It will probably then still work.
- If there are problems, please read the Rtools.txt file carefully.
2 Creating the package
Information about creating packages is provided in the document “Writing R extensions” (available under the R Help menu) or at
The main items are summarized below to get you started, but you will almost certainly need to consult the above document if you are to successfully compile a package.
2.1 Use package.skeleton()
The simplest way to create a package is to first create an R workspace containing all the relevant functions and data sets that you want to include in the package. Delete anything from the workspace that you do not want to include in the package. Make sure the current directory is set to whereever you want create the package. Use
setwd("C:My DocumentsRpackages")
for example. Then, to create a package called "fred"
, use the R command
package.skeleton(name="fred")
This will generate a directory fred
and several sub-directories in the required structure.
2.2 Editing the files
A package consists of a directory containing a file ‘DESCRIPTION’ and usually has the subdirectories R
, data
and man
. The package directory should be given the same name as the package. The package.skeleton
command above will have created these files for you. You now need to edit them so they contain the right information.
2.3 DESCRIPTION file
The DESCRIPTION file contains basic information about the package in the following format:
Package: pkgname Version: 0.5 Date: 2007-06-05 Title: My first collection of functions Author: Joe Developer <Joe.Developer @some.domain.net>, with contributions from A. User <A.User @whereever.net>. Maintainer: Joe Developer <Joe.Developer @some.domain.net> Depends: R (>= 2.0.0), forecast Suggests: tseries Description: A short (one paragraph) description of what the package does and why it may be useful. License: GPL version 2 or newer URL: http://www.another.url |
2.4 Rd files
The help files for each function and data set are given in “R documentation” (Rd) files in the man
subdirectory. These are in a simple markup language closely resembling LaTeX, which can be processed into a variety of formats, including LaTeX, HTML and plain text. As an example, here is the file which documents the function seasadj
in the forecast
package.
name{seasadj} alias{seasadj} title{Seasonal adjustment} usage{ seasadj(object) } arguments{ item{object}{Object created by code{link[stats]{decompose}} or code{link[stats]{stl}}.} } description{Returns seasonally adjusted data constructed by removing the seasonal component.} value{Univariate time series.} seealso{code{link[stats]{stl}}, code{link[stats]{decompose}}} author{Rob J Hyndman} examples{ plot(AirPassengers) lines(seasadj(decompose(AirPassengers,"multiplicative")),col=4) } keyword{ts} |
Detailed instructions for writing R documentation are at
2.5 Including C or Fortran code
If your R code calls C or Fortran functions, the source code for these functions needs to be placed in the subdirectory src
under fred
.
2.6 Compiling the package for Windows
To compile the package into a zip file, go to a DOS prompt in the directory containing your package. (i.e., the directory "C:My DocumentsRpackages"
in the above example. Then type
Rcmd build --binary fred
This will compile all the necessary information and create a zip file which should be ready to load in R.
2.7 Checking the package
To check that the package satisfies the requirements for a CRAN package, use
Rcmd check fred
The checks are quite strict. A package will often work ok even if it doesn’t pass these tests. But it is good practice to build packages that do satisfy these tests as it may save problems later.
2.8 Building a package for other operating systems
To build a package for something other than a Windows computer, use
Rcmd build fred
This creates a tar.gz file which can then be installed on a non-Windows computer. It can also be uploaded to CRAN provided it satisfies the above tests.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.