2

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?

Scott Ritchie
  • 10,293
  • 3
  • 28
  • 64
  • Do you only get only two "`Recursive Statement?`" or more than that? If only two, I would try to replace `mirrorSetup()` with `invisible(mirrorSetup())`. – flodel Jul 30 '13 at 05:27
  • Rather than this, why not point both versions of R to a common library location so that you only have one installed set of packages? See suggestions in a couple of the answers [here](http://stackoverflow.com/questions/1401904/painless-way-to-install-a-new-version-of-r-on-windows). – Thomas Jul 30 '13 at 06:26
  • @flodel I get infinite, it keeps repeating until I kill the command with ^C. – Scott Ritchie Jul 30 '13 at 06:28
  • @Thomas I didn't think that installed libraries from one version of R would work in another? – Scott Ritchie Jul 30 '13 at 06:29
  • @Manetheran It probably depends on the package, with some giving you warnings about updating. You can experiment by swapping out your entire library directory in one version with that from another and see what happens. – Thomas Jul 30 '13 at 06:33
  • It looks like it will be impossible to automate, so I will just leave it as a function that I can call manually. – Scott Ritchie Jul 30 '13 at 06:50

1 Answers1

1

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.

Scott Ritchie
  • 10,293
  • 3
  • 28
  • 64
  • 1
    See if moving the code into the `.First` function works. `.First` is called automatically at startup, but at a different point (after the global workspace file `.RData` is loaded). – Hong Ooi Jul 30 '13 at 10:34
  • 1
    You could also `unlink("~/.Rinstalled")` as soon as it is read the first time and that should solve your infinite recursion. – flodel Jul 30 '13 at 11:47