Jess
Jess

Reputation: 188

I need to switch to an old version of R to run one script

I am trying to run a script that was build on an older version of R. It runs on one computer, which is running R/3.4.4. It breaks on another, running R/3.6.3. The author has an issue checked out on his github to fix this (I think), but he's finishing his PhD so that could happen in two months, two years, or never.

I installed R/3.4.4 in my $HOME/.local path (the other is in /usr/bin). I just can't figure out how to temporarily point to the version in .local when I run this script. I tried setting R_HOME with export R_HOME=$HOME/.local but R ignores that.

I am sure this has been asked before, but I can't find anything that doesn't involve RStudio. I don't have RStudio on my WSL (running Ubuntu 20.04), so I need a terminal-based solution.

Can someone explain how to do this? I feel really dense and really exhausted. Thank you.

Upvotes: 2

Views: 4455

Answers (2)

r2evans
r2evans

Reputation: 161085

Changing your PATH might be useful in some ways, but since (I'm inferring) that you intend this to be the exception, then just running this with the full path to the executable should be sufficient. If your task is encapsulated in a script named script.R, then perhaps

$HOMD/.local/R_3.4.4/bin/x64/Rscript /path/to/script.R

should be sufficient.

This may bite you, though, depending on how the current R is installed. You said it's in /usr/bin/, which suggests that its library path might be something like this:

$ Rscript -e '.libPaths()'
[1] "/usr/local/lib/R/site-library"
[2] "/usr/lib/R/site-library"
[3] "/usr/lib/R/library"

(where Rscript here is the default/current version). Unfortunately, the packages in those directories are not stored as version-specific, so it is possible (even likely) that using a more-modern version of a package with R-3.4.4 will fail in some way.

Two strategies for preempting this possibility. The first works fine assuming that you have all packages for R-3.4.4 in a specific directory.

  1. One way is to set R_LIBS_USER (env-var) for the command. This can be on the same command-line, or it can be controlled via some .Renviron file or similar:

    $ mkdir -p .local/R-3.4.4/library
    $ R_LIBS_USER=~/.local/R-3.4.4/library Rscript -e '.libPaths()'
    [1] "/home/r2/.local/R-3.4.4/library"
    [2] "/usr/local/lib/R/site-library"
    [3] "/usr/lib/R/site-library"
    [4] "/usr/lib/R/library"
    

    With this, assuming all packages needed are found in that first path, it will never look in the others, so there should be no conflicts experienced. If your script is stable/unchanging and you don't break your instance of R-3.4.4 within .local, then ... all is happy.

  2. Modify the script itself to hard-control the library paths. By over-riding R_HOME (env var), you can override all of the defaults.

    echo "R_HOME=~/.local/R-3.4.4" > .Renviron # current working directory, regardless of script.R
    Rscript /path/to/script.R
    

    This assumes that .local/R_3.4.4 is a complete R installation ... it needs all of the base stuff as well as any other packages you may have installed.

Upvotes: 4

ameerosein
ameerosein

Reputation: 563

You have possibly added to the path the new directory AFTER the old one. You can check this using:

echo $PATH

If it's the case then you need to add it IN FRONT OF the path. Assuming that you install it in /usr/local/bin, then you should use the following command:

export PATH="/usr/local/bin:$PATH"

Upvotes: 0

Related Questions