runr
runr

Reputation: 1146

RStudio: using different package versions for each .Rproj

I have a few older R projects I'm working with, which are dependent on several currently deprecated (or heavily modified) packages. In order for everything to work smoothly I use older versions of those packages, which I have saved in another folder and load up manually to %userprofile%\documents\R\win-library\3.3 when necessary. However, this is not convenient, especially if I want to run multiple projects simultaneously, some of which requires the new and updated versions of the packages.

My question - is there a way to specify custom directories for each .Rproj from which it would take and load the libraries?

Upvotes: 0

Views: 919

Answers (2)

Dirk is no longer here
Dirk is no longer here

Reputation: 368181

You can solve this much simpler:

  1. Have a top-level directory for each project, call projA, projB, ...
  2. Within each of these, create a directory libs/, say.
  3. And within each of these directories have a file .Rprofile with a single assignment such as .libPaths("./libs")

Now when you start R in the different project directories, each will a separate library directory preceding the path, allowing you to place per-projects overrides there.

In an nutshell, the approach outlines here allows you to keep the local and modified packages around as you please. (You can even assign common directories via .libPaths() if you so choose.)

The nice things is that this will

  1. work with any R invocation, batch or GUI or RStudio or shiny or ...
  2. does not depend on any other tools, and hence
  3. does not rely on RStudio or .Rprof files -- though you are free to use RStudio as well.

As so often, Base R is there for you.

Upvotes: 7

Hack-R
Hack-R

Reputation: 23217

One option is to use the checkpoint package by Revolution Analytics.

You can indicate for each main R file in a project the date for which you which you wish to load a set of packages. You can read a bit more about it here.

enter image description here

To pull snapshotted packages from a given date from the mirror use getValidSnapshots(mranRootUrl = mranUrl()).

To create a checkpoint:

# Create temporary project and set working directory

example_project <- paste0("~/checkpoint_example_project_", Sys.Date())

dir.create(example_project, recursive = TRUE)
oldwd <- setwd(example_project)


# Write dummy code file to project

cat("library(MASS)", "library(foreach)",
    sep="\n",
    file="checkpoint_example_code.R")

# Create a checkpoint by specifying a snapshot date

library(checkpoint)
checkpoint("2014-09-17")

# Check that CRAN mirror is set to MRAN snapshot
getOption("repos")

# Check that library path is set to ~/.checkpoint
.libPaths()

# Check which packages are installed in checkpoint library
installed.packages()

# cleanup
unlink(example_project, recursive = TRUE)
setwd(oldwd)

Upvotes: 2

Related Questions