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:
- When a package is installed (in either version) write out a csv file, ~/.Rinstalled, that contains a list of all the installed packages
- When a new R session is opened, check if that file exists.
- If the file exists, compare that list with the packages installed in the current version of R being run.
- 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?
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:
Load R:
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.