A safe way to use older versions of an R package under linux

157 views Asked by At

Sometimes I need to run old codes in R which have been written some time ago using versions of packages different from what I use now with my current R version. When I do not need to update and correct the script with the newer functions of the current packages, but only to ckeck the results of those scripts, what is the safer way to go? Till know I have found two possibilities.

  1. Installing multiple R versions with their associated 'old' packages and shifting from one version to another as needed if this is possible maybe from rstudio.
  2. Having only one R version (assuming this would be a relatively recent one) and using old packages when needed, in case this doesn't get into dependencies problems for using old packages with a newer version of R.

The first option was a total disaster for me as I tried to install other versions of R in different ways, also had to "tune" my debian distribution (bullseye) to accept the latest R version, it became unstable and finally I had to format my PC. Related questions here and here, but before giving the suggested solutions a try, I wanted to know if other options exist. So I would appreciate any insights about the easier and safer way of running old scripts with their old packages/functions.

1

There are 1 answers

3
Dirk is no longer here On BEST ANSWER

This works quite well by taking advantage of the ability of R to use different library paths. You can set these via .libPaths() easily.

As a concrete example, I happen to like using roxygen2 version 6.0.1. So I keep it in a directory my 'normal' R installation does not know about. In a helper script (as I also like to use this from the command-line) I then do

if (dir.exists("~/.R/cache/roxygen2")) {
    cat("** Using cached version 6.0.1 of roxygen2.\n")
    .libPaths("~/.R/cache")
}

## load roxygen
library(roxygen2)

and on a machine with that directory present, it will be prefixed to the library path and that older roxygen version is used in this session.

You can easily generalize this. We have done so for continuous integration and other use cases.

Another example is e.g. provided by my use of r-devel as it has its own library path (so that I can have package compiled by r-devel followed by the system one (so that I do not have to rebuild all packages):

$ RD -q -e '.libPaths()'
> .libPaths()
[1] "/usr/local/lib/R-devel/lib/R/library" 
[2] "/usr/local/lib/R/site-library"       
> 
$