Over a million developers have joined DZone.

Building R packages for Windows

· Big Data Zone

Hortonworks DataFlow is an integrated platform that makes data ingestion fast, easy, and secure. Download the white paper now.  Brought to you in partnership with Hortonworks

1. Installing the required tools

To build an R pack­age in Win­dows, you will need to install some addi­tional soft­ware tools. These are sum­ma­rized at


1.1 Essen­tial: Rtools

This is a col­lec­tion of unix-​​like tools that can be run from the DOS com­mand prompt. It also con­tains the MinGW com­pil­ers that are used for com­pil­ing For­tran and C code. You should down­load and run the ver­sion cor­re­spond­ing to the flavour of R you are using. When asked, choose the default “Pack­age author­ing instal­la­tion” to build add-​​on packages.

1.2 Optional: MikTeX

Mik­TeX is used for pro­duc­ing the pdf help files. You can pro­duce an R pack­age with­out it, but the pack­age will not con­tain pdf help files. Most of you will have this installed any­way. Down­load from


1.3 Essen­tial: Set­ting PATH variable

The PATH vari­able tells Win­dows where to find the rel­e­vant pro­grams. To add a direc­tory to your PATH on Win­dows select

Control Panel -> System -> Advanced -> Environment Variables

The path vari­able may have already been fixed in step 1.1. In any case, you should check that it looks some­thing like this:

C:\Rtools\bin;C:\Program files\R\R-2.15.1\bin\x64;

The last direc­tory will depend on whether you have a 32-​​bit or 64-​​bit oper­at­ing sys­tem. It should be the direc­tory that con­tains the file Rcmd.exe

  • The pre­cise direc­to­ries will depend where you have installed the var­i­ous tools. The above path should work if you have fol­lowed the default instal­la­tion pro­ce­dure. If the direc­tory names con­tain spaces, put them in quo­ta­tion marks.
  • I have assumed R2.15.1. For later ver­sions, sim­ply change the above paths to the rel­e­vant R ver­sion. It will prob­a­bly then still work.
  • If there are prob­lems, please read the Rtools.txt file carefully.

2 Cre­at­ing the package

Infor­ma­tion about cre­at­ing pack­ages is pro­vided in the doc­u­ment “Writ­ing R exten­sions” (avail­able under the R Help menu) or at


The main items are sum­ma­rized below to get you started, but you will almost cer­tainly need to con­sult the above doc­u­ment if you are to suc­cess­fully com­pile a package. 2.1 Use package.skeleton()

The sim­plest way to cre­ate a pack­age is to first cre­ate an R work­space con­tain­ing all the rel­e­vant func­tions and data sets that you want to include in the pack­age. Delete any­thing from the work­space that you do not want to include in the pack­age. Make sure the cur­rent direc­tory is set to whereever you want cre­ate the pack­age. Use

    setwd("C:/My Documents/Rpackages")

for exam­ple. Then, to cre­ate a pack­age called "fred", use the R command


This will gen­er­ate a direc­tory fred and sev­eral sub-​​directories in the required structure.

2.2 Edit­ing the files

A pack­age con­sists of a direc­tory con­tain­ing a file ‘DESCRIPTION’ and usu­ally has the sub­di­rec­to­ries R, data and man. The pack­age direc­tory should be given the same name as the pack­age. The package.skeleton com­mand above will have cre­ated these files for you. You now need to edit them so they con­tain the right information.


The DESCRIPTION file con­tains basic infor­ma­tion about the pack­age in the fol­low­ing format:

Package: pkgname
Version: 0.5
Date: 2007-06-05
Title: My first collection of functions
Author: Joe Developer , with
contributions from A. User .
Maintainer: Joe Developer
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 func­tion and data set are given in “R doc­u­men­ta­tion” (Rd) files in the man sub­di­rec­tory. These are in a sim­ple markup lan­guage closely resem­bling LaTeX, which can be processed into a vari­ety of for­mats, includ­ing LaTeX, HTML and plain text. As an exam­ple, here is the file which doc­u­ments the func­tion seasadj in the forecast package.


\title{Seasonal adjustment}
\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}

Detailed instruc­tions for writ­ing R doc­u­men­ta­tion are at


2.5 Includ­ing C or For­tran code

If your R code calls C or For­tran func­tions, the source code for these func­tions needs to be placed in the sub­di­rec­tory src under fred.

2.6 Com­pil­ing the pack­age for Windows

To com­pile the pack­age into a zip file, go to a DOS prompt in the direc­tory con­tain­ing your pack­age. (i.e., the direc­tory "C:\My Documents\Rpackages" in the above exam­ple. Then type

Rcmd INSTALL --build fred

This will com­pile all the nec­es­sary infor­ma­tion and cre­ate a zip file which should be ready to load in R.

2.7 Check­ing the package

To check that the pack­age sat­is­fies the require­ments for a CRAN pack­age, use

Rcmd check fred

The checks are quite strict. A pack­age will often work ok even if it doesn’t pass these tests. But it is good prac­tice to build pack­ages that do sat­isfy these tests as it may save prob­lems later.

2.8 Build­ing a pack­age for other oper­at­ing systems

To build a pack­age for some­thing other than a Win­dows com­puter, use

Rcmd build fred

This cre­ates a tar.gz file which can then be installed on a non-​​Windows com­puter. It can also be uploaded to CRAN pro­vided it sat­is­fies the above tests.

Hortonworks Sandbox is a personal, portable Apache Hadoop® environment that comes with dozens of interactive Hadoop and it's ecosystem tutorials and the most exciting developments from the latest HDP distribution, brought to you in partnership with Hortonworks.


Published at DZone with permission of Rob J Hyndman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}