Creating an R package in Windows

[This article was first published on G-Forge » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

A nice package can be both beautiful and functional. The image is CC by MIAD Communication Design.

Inspired by this post by Szilard Pafka I decided to do a similar adventure in a Windows environment and see what problems I run into.

Start by installing Eclipse & StatET, the installation can sometimes cause some annoyances. I’ve covered a lot of them in my previous post.

Documentation is everything

After years of programming I can tell you that having a simple way to document your code is vital. Your code should always try to be self-explanatory, but adding good documentation is vital. The R-group has recognized this, and you’re forced to produce examples and full documentation before you can publish anything on their CRAN-server. A simple and great tool for this is the Roxygen2 package.

Start by installing the Roxygen2 package:

?View Code RSPLUS

Unfortunately it seems Roxygen dropped its support for the convenient “R CMD Roxygen“. Fortunately I’ve found a handy work-around this problem:

  1. Go into the menu Run > Run Configurations….
  2. Here you choose the R Console and press the New launch button
  3. Name the console to something nice that you’ll remember, for instance “Roxygenise package”
  4. Set the Working directory to ${project_loc} (you can also find it in the [+] button’s insert variable list)
  5. Add as Options/Arguments:
  6. Now choose the R Console tab and add following to the R snippet run after startup:
    ?View Code RSPLUS
    roxygenise(getwd(), overwrite=TRUE)

  7. That’s it – now click on your project and choose the Roxygenise package configuration.

Creating the package

Choose create new project, File > New > Project… and select the StatET > R Package Project and click your way through the popups.

I use the following directory/file structure in my package:


The bold are the directories while the DESCRIPTION and LICENSE files are required files. The DESCRIPTION file looks like this:

Package: test
Version: 0.1.0
Date: 2012-11-20
Title: A test
Authors: c(person("Max Gordon",
    email = "...."))
Author: Max Gordon
Maintainer: Max Gordon <[email protected]>
Description: bla, blah..
License: GPL (>= 2)

I’ve added the Depends and Suggests just to give an idea of how you enter these. If you have more Depends then just add them by separating with a comma (,). The LICENSE file is just a basic GPL-copy:

Copyright (C) 2012 Max Gordon, 
orthopaedic surgeon and PhD-student at the Karolinska Institute

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

These functions are distributed in the hope that they will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

The text of the GNU General Public License, version 2, is available
as or by writing to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

Now create a file called hello_world.R under the \R directory. The file can contain one or more functions. It’s important that you have the proper Roxygen documentation:

?View Code RSPLUS
#' My awesome function
#' @param name The name that is to be printed
#' @return void
#' @examples
#' hello_world("Max")
#' @author max
#' @export
hello_world <- function(name){
  print(sprintf("Hello %s!", name))
#' My awesome function number 2
#' @param name The name that is to be printed
#' @return void
#' @example examples/hello_world_alt_example.R
#' @author max
#' @export
hello_world_alternative <- function(name){
  print(sprintf("Cool to meet you, %s!", name))

As you can see the second function has an external example file. I find this very convenient when I have more associated code, in this case it might be a little over-doing. Anyway, in the \examples directory create a file hello_world_alt_example.R that contains the following code (add an empty line at the end to avoid a warning message by Roxygen):

?View Code RSPLUS
# An example file
# Nice to have if you have
# longer examples that you
# want to put outside your main code
name <- "Max"

All you need to do now is just run the Roxygenise package! Do this by clicking on the project name:

and choosing the Roxygenise package:

This creates additional directories, click on the project and refresh by pressing F5. The new directory structure contains the additional directories \inst and \man, it has also created the NAMESPACE file.

Checking and building the package

Now you need to create a few R CMD commands. Make sure you’ve downloaded the Rtools package. First, open Run >External tools > External tools configurations … and create the following four R CMD commands:

Just keep adding by clicking the new icon until you have four. You can skip the Pkg install if you want but it’s convenient to have if you’re not interested in exporting your package. In the Options/Arguments text area you add options –build for the Pkg install build R CMD while you add –as-cran in the Pkg check to get the stricter check for the CRAN repository in case you want to upload you package there in the future.

Start by checking your package with the Pkg check –as-cran external tool. It checks all including the examples. Writing good, complete examples is an excellent way of checking that nothing in the packages suddenly breaks expected behavior – this is awesome when you have a multitude of functions, our hello_world project is probably less dependent on a thorough check.

Now run the Pkg install and viola, the package is available on your computer next time you restart R. To get versions that you need for exporting onto a CRAN server you run the Pkg build and the Pkg install –build. The zip file is the windows binary and the tar.gz file is the source file. These archives are created in the eclipse directory.

Note, if you get an error: “Variable references empty selection: ${resource_loc}” you need to refocus on the project.

Phew… it was a long post but hopefully it was somewhat helpful. I’ve also have a small post on creating your own CRAN repository.

To leave a comment for the author, please follow the link and comment on their blog: G-Forge » R. 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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)