16

Does R have a dependency management tool to facilitate project-specific dependencies? I'm looking for something akin to Java's maven, Ruby's bundler, Python's virtualenv, Node's npm, etc.

I'm aware of the "Depends" clause in the DESCRIPTION file, as well as the R_LIBS facility, but these don't seem to work in concert to provide a solution to some very common workflows.

I'd essentially like to be able to check out a project and run a single command to build and test the project. The command should install any required packages into a project-specific library without affecting the global R installation. E.g.:

my_project/.Rlibs/*
6
  • check out the R packages ProjectTemplate and devtools.
    – Ramnath
    Oct 11, 2011 at 17:54
  • ProjectTemplate doesn't support this. The docs say that the libraries must already be installed: "libraries: This is a comma separated list of all the R packages that the user wants to automatically load when load.project() is called. These packages must already be installed before calling load.project(). By default, the reshape, plyr, ggplot2, stringr and lubridate packages are included in this list."
    – yoni
    Oct 11, 2011 at 20:38
  • 1
    looks like devtools has some great stuff, but it also falls short on this.
    – yoni
    Oct 11, 2011 at 21:16
  • 2
    I think what you want is eminently doable in R, but no one has done it yet .
    – hadley
    Oct 12, 2011 at 13:43
  • 1
    If you want a devtools solution, dev_mode(); install_deps("path/to/package"); check() comes pretty close.
    – hadley
    Oct 12, 2011 at 13:45

4 Answers 4

7

Unfortunately, Depends: within the DESCRIPTION: file is all you get for the following reasons:

  • R itself is reasonably cross-platform, but that means we need this to work across platforms and OSs
  • Encoding Depends: beyond R packages requires encoding the Depends in a portable manner across operating systems---good luck encoding even something simple such as 'a PNG graphics library' in a way that can be resolved unambiguously across systems
  • Windows does not have a package manager
  • AFAIK OS X does not have a package manager that mixes what Apple ships and what other Open Source projects provide
  • Even among Linux distributions, you do not get consistency: just take RStudio as an example which comes in two packages (which all provide their dependencies!) for RedHat/Fedora and Debian/Ubuntu

This is a hard problem.

0
7

The packrat package is precisely meant to achieve the following:

install any required packages into a project-specific library without affecting the global R installation

It allows installing different versions of the same packages in different project-local package libraries.

I am adding this answer even though this question is 5 years old, because this solution apparently didn't exist yet at the time the question was asked (as far as I can tell, packrat first appeared on CRAN in 2014).

Update (November 2019)

The new R package renv replaced packrat.

3
  • Thanks Guillaume. Please see my response to a similar comment in the accepted answer. It's quite possible things have changed, so feel free to comment accordingly.
    – yoni
    Nov 28, 2018 at 18:16
  • Thank you, I missed that comment above before posting. But I suppose it's relevant to give packrat visibility with an answer (it's easy to overlook comments).
    – Guillaume
    Nov 28, 2018 at 20:06
  • Agreed. Thanks, Guillaume. :)
    – yoni
    Nov 29, 2018 at 22:09
5

As a stop-gap, I've written a new rbundler package. It installs project dependencies into a project-specific subdirectory (e.g. <PROJECT>/.Rbundle), allowing the user to avoid using global libraries.

We've been using rbundler at Opower for a few months now and have seen a huge improvement in developer workflow, testability, and maintainability of internal packages. Combined with our internal package repository, we have been able to stabilize development of a dozen or so packages for use in production applications.

A common workflow:

  • Check out a project from github
  • cd into the project directory
  • Fire up R
  • From the R console:

    library(rbundler)

    bundle('.')

All dependencies will be installed into ./.Rbundle, and an .Renviron file will be created with the following contents:

R_LIBS_USER='.Rbundle'

Any R operations run from within this project directory will adhere to the project-speciic library and package dependencies. Note that, while this method uses the package DESCRIPTION to define dependencies, it needn't have an actual package structure. Thus, rbundler becomes a general tool for managing an R project, whether it be a simple script or a full-blown package.

5
  • 2
    On first sight it seems to me that this is quite exactly what PackRat(rstudio.github.io/packrat) does, which has recently been published by the RStudio team. Am I wrong?
    – Eike P.
    Dec 2, 2014 at 16:51
  • 4
    Thanks for bringing that up, @jhin. I spoke with @hadley and the folks at RStudio about this around the time I started developing rbundler. The difference is that rbundler is focused on package development, and takes advantage of the explicit dependencies in your DESCRIPTION file, whereas PackRat focuses on general project development, and derives your dependencies are through reflection. PackRat has some additional features to snapshot your dependencies, in order to facilitate deployment and sharing. I believe that was their top priority as a solution to managing hosted projects.
    – yoni
    Dec 2, 2014 at 21:42
  • Ah, I see. I was just trying to understand the package deployment workflow with rbundler, but I'm not quite sure I get it right. Let's assume I have developed a package, and I bundle its dependencies with rbundler. What happens when somebody installs my package, e.g. using devtools 'install("mypack")' from a miniCRAN (assuming this is what you mean by "our internal package repository")? How are package dependencies resolved?
    – Eike P.
    Dec 3, 2014 at 12:53
  • Side question: How are packages stored with rbundler? I understand that PackRat mostly stores packages in source form, in order to make the dependencies portable to different platforms. How is this solved with rbundler? (Maybe you want to include this information on the project's GitHub / CRAN pages? At least I wasn't able to find any answers to these questions there.)
    – Eike P.
    Dec 3, 2014 at 12:57
  • rbundler doesn't do anything fancy to packages, it just manages a project-specific library, and installs packages into there. As mentioned in the answer here, the library is configured by overriding R's standard R_LIBS_USER. Perhaps I should link to R's library management configuration from the README.
    – yoni
    Dec 3, 2014 at 17:56
1

You could use the following workflow:

1) create a script file, which contains everything you want to setup and store it in your projectd directory as e.g. projectInit.R

2) source this script from your .Rprofile (or any other file executed by R at startup) with a try statement

try(source("./projectInit.R"), silent=TRUE)

This will guarantee that even when no projectInit.R is found, R starts without error message

3) if you start R in your project directory, the projectInit.R file will be sourced if present in the directory and you are ready to go

This is from a Linux perspective, but should work in the same way under windows and Mac as well.

1
  • Sorry - I haven't seen the "install" part. I have to go with Dirk here - this will have to be specific to a platform, but there comes e.g. a bash script handy, which could achieve possibly all you want (in concert with the sourcing of the above mentioned projectInit.R file.
    – Rainer
    Oct 11, 2011 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.