Question

I have multiple versions of R installed (2.15 and 3.0.1), which I often switch between. I want to make sure that when I install a package in one version, it will also be present in the other (where possible), so I have attempted to set up the following system:

  1. When a package is installed (in either version) write out a csv file, ~/.Rinstalled, that contains a list of all the installed packages
  2. When a new R session is opened, check if that file exists.
  3. If the file exists, compare that list with the packages installed in the current version of R being run.
  4. Attempt to install all packages which are missing.

To this end I have the following code in my .Rprofile:

mirrorSetup <- function() {
  cat("Recursive Statement?\n") 
  require(utils)
  if(file.exists("~/.Rinstalled")) {
    packages <- as.vector(read.csv("~/.Rinstalled")[,2])
    notInstalled <- packages[!(packages %in% rownames(installed.packages()))]
    # Remove file on exit if we're in a different version of R.
    if (length(notInstalled) > 0) {
      on.exit({
        unlink("~/.Rinstalled")
      })
    }

    for (i in seq_along(notInstalled)) {
      # Check if package installed via previous dependency in for loop
      updated <- rownames(installed.packages())
      if (notInstalled[i] %in% updated) {
        next
      }

      # Try to install via Cran first, then Bioconductor if that fails
      tryCatch({
        utils::install.packages(notInstalled[i])
      }, error = function(e) {
        try({
          source("http://bioconductor.org/biocLite.R")
          biocLite(notInstalled[i])
        }, silent = TRUE)
      })
    }
  }
}

mirrorSetup()

However, when this code runs, it recursively calls mirrorSetup() on utils::install.packages(notInstalled[i]), and I have no idea why.

Here's some sample output, showing that it is repeatedly trying to install the first package it finds (ade4)

Recursive Statement?
Loading required package: utils
Trying to install ade4 from Cran...
trying URL 'http://cran.ms.unimelb.edu.au/src/contrib/ade4_1.5-2.tar.gz'
Content type 'application/x-tar' length 1375680 bytes (1.3 Mb)
opened URL
==================================================
downloaded 1.3 Mb

Recursive Statement?
Loading required package: utils
Trying to install ade4 from Cran...
trying URL 'http://cran.ms.unimelb.edu.au/src/contrib/ade4_1.5-2.tar.gz'
Content type 'application/x-tar' length 1375680 bytes (1.3 Mb)
opened URL
==================================================
downloaded 1.3 Mb

Any ideas?

Was it helpful?

Solution

So I've had a play around and it won't be possible to do what I am trying. The function install.packages reloads your .Rprofile when it is called. For example if I do the following:

Create temporary .Rprofile:

cat(".Rprofile loaded!\n")

Load R:

R version 3.0.0 (2013-04-03) -- "Masked Marvel"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin12.3.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

.Rprofile loaded
> install.packages("ade4")
--- Please select a CRAN mirror for use in this session ---
trying URL 'http://cran.ms.unimelb.edu.au/src/contrib/ade4_1.5-2.tar.gz'
Content type 'application/x-tar' length 1375680 bytes (1.3 Mb)
opened URL
==================================================
downloaded 1.3 Mb

.Rprofile loaded

Showing that the .Rprofile is read in again at package installation.

While the process of package mirroring can't be automated in this way, the function can still be left in the .Rprofile, and called manually by the user.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top