How to configure RStudio package build to work across multiple machines

I came across an error like this just now (except I had previously been able to build the package without incident).

Turns out my error was caused by a poorly formed DESCRIPTION file. Fixing the syntax there allowed my package to build.


Following is an outline of how I've approached this for a package that we'll call mypack.

Update R and Rstudio

This should be the first thing you do. Update everything...

Create the package skeleton

Use the roxygen2 package to create the mypack package skeleton. From your IDE, try this:

library(roxygen2)
package.skeleton('mypack')

See http://cran.r-project.org/web/packages/roxygen2/roxygen2.pdf for more details.

Edit the description file

Edit the DESCRIPTION file using a reader that lets me see non-printing characters. It's worth checking to see if there are any odd characters hiding in your file. RStudio is a good editor to use.

Compare your DESCRIPTION file contents with someone else's github repositories, for example the plyr description file.

Also, there's a related question here with an answer that explains some more of how the DESCRIPTION file is used.

Building the package

I use a script (below) to build my package from within Rstudio, rather than using Rstudio's canned options. I don't like having to remember to switch between the usual IDE and some project.

You'll need to have [devtools][3] installed to make this work.

setwd('~/Documents/projects/Rcode')
# remove the old version
try(detach(name = "package:mypack",unload = TRUE))
# load the packages we need for building / documentation
library("roxygen2")  # could use "require"
# generate documentation
roxygenize(package.dir = "mypack")

# now we actually build and install the package
system("R CMD build mypack")
system("R CMD INSTALL --preclean mypack_1.00.tar.gz")
system("R CMD check mypack")
# check for old documentation
try(file.remove(file.path(getwd(),"mypack.pdf")))
# generate new documentation
system("R CMD Rd2pdf mypack")
# and finally, make it available.
library("mypack")

Disclaimer: I am fully aware that I may have some of the above in the wrong order, or be doing something daft. I'm just not sure what, and it works at the moment.

Creating package documentation

I have R-markdown in the headers of all of my functions, so the help files are created as ./man/*.Rd (where * is the function name) automagically when I execute this script. Again, have a look at the plyr github to get a feel for this.

I also wrote a vignette for this package. That's an .Rnw file, which can be edited in Rstudio or any other text editor. The .Rnw file is basically latex with embedded knitr chunks. Rstudio's editor has an option to compile this independently of the package itself. This seems to be a great solution, and the above script recompiles the vignettes whenever I update my code, which is a good check on everything.

Cross-platform installation

I know this script works on multiple macs, but have never had the misfortune to have to use a windows machine to compile. A colleague of mine is using the compiled package on windows without any trouble, though.

Resources

Useful resources for this include:

  • Hadley's guide to creating packages and function documentation at http://r-pkgs.had.co.nz/,
  • R-bloggers (e.g. http://www.r-bloggers.com/r-package-building-automation/)
  • Stackoverflow.

I recently had exactly the same problem - my package would not compile under RStudio with an error saying that the package directory does not contain the DESCRIPTION file. I solved the problem by changing encoding of the DESCRIPTION file to ANSI.