Sunday, December 10, 2006

Making R packages under Windows

Building R package under Windows is easy but tricky. So here I summarize the procedure below step-by-step. For those who are interested in more advanced guidelines, please see the references below.


  1. Writing R Extensions
  2. Making R Packages Under Windows
  3. Build R package for Win2000/XP
  4. Building R for Windows
  5. Creating R Packages (the idiot's guide)
Getting Started: Installing Necessary Software

1. The R software
Install full package of R.

2. The minimal set of Unix utilities (R tools)
If the above link for R tools is dead. Try this one (R tools). Extract it, say in a directory C:\Rtools

3. Perl (A scripting language used by the R installer and checker)
Find the version for Windows. You need to install the most current version or the installation would fail.

4. The GNU compiler set (MinGW)
Choose full installation.

5. Microsoft html compiler

6. A TeX system (MikTex)
If you have another TeX system in your PC. You can ignore this step.

Change your PATH "environmental variable"

To set the path, right click on the "My Computer" icon on your desktop. Choose properties and click on the "advanced" tab. Click the "environmental variables" button and you should manually type in the following red Paths. The example Paths should look like the following:

C:\RTools\bin;C:\MinGW\bin;C:\Program Files\MiKTeX 2.5\miktex\bin;C:\Perl\bin\;C:\Program Files\R\R.2.4.0\bin;C:\Program Files\HTML Help Workshop;C:\WINDOWS\system32; C:\WINDOWS;C:\WINDOWS\System32\Wbem;

Make sure you have the 6 new PATHs added corresponding to the above 6 softwares. To see if you do everything properly, click "start" --> "run" and type "cmd." You will see a windows command console pumps out. Then you type followings:

gcc --help
perl --help
TeX --help
R CMD --help

For each, you should see a list of description. If not, something is wrong.

Compile Package

I will not state the details about how to write a package, please see Writing R Extensions instead.

Once you have everything ready. You need to compile the help files for your package first. Go to the windows command console and type (say your package is called test and is under c:\working\test):

cd test
cd man
R CMD Rd2txt lm.Rd
R CMD Rdconv -t=html -o=lm.html lm.Rd

You have to do the above compilation for every Rd file you write. Once you've done, go to your package main directory:

cd ..\..
R CMD check test

This will check your package. To create a pdf manual for your package, simply type:

R CMD Rd2dvi --pdf test

You will have a test.pdf. Put it under the doc directory (e.g., c:\working\test\inst\doc). Finally you can create the R package now.

R CMD build --binary --use-zip test

This will create a zip file You can install this package in R by choosing "install package(s) from local zip files".

[NOTE] If you want your package compiled by CRAN to be cross platforms, you need to check if your package depends on any of the packages that are not available for other platforms. For instance, if your package depends on the BRugs package which is only available for Windows, it will not be compiled for other platforms either by CRAN.


Anonymous said...

Thanks for wonderful information!

Do you know how to add functions and update manuals to a created package? Using the function package.skeleton does not allow to overwrite an existing packet, so I wonder if there is some kind of a function that allows to add information to an existing packet.

best regards,

Yu-Sung Su said...

I don't think there is a way to do what you really want. You can write a supplementary package to an existing one. But I don't think you can add function to the packaged one.

Watershed modeling said...

Thanks for your reply!

There is another thing about making a package that I have been considered. Perhaps you have the solution to my problem :)

When I write my functions I use the "source()" function to organize my code in subsections. E.g.

myfunc <- function(input){

I can easily run the function in R without problems, just parsing the code directly into R. BUT if I compile the function using the "package.skeleton()"-function and afterwards compiling the functions to a zipped R-packet, then the sourced files (in this example: file1.r & file2.r) are missing. This means that when compiling a packet the compilation exclude the sourced files and just takes the plain text (i.e. source(file1.r) and source(file2.r) without considering the content of the 2 sourced files.

Did you run into the same problem? Others have recommended me to write 2 functions - on each for the 2 sourced files, and this will work, but I still think it would be much more convinent and practical for me to use the source()- function in many cases.

Any help would me fantastic!

Yu-Sung Su said...


If you are writing a package, you don't need to source additional functions within a function. Just put it somewhere in your package, the R compiler will identify those additional functions.