Every now and then you may need to use more than one version of a given R library. This can occur for several reasons, including comparing old and new code, checking compatibilities in a workflow, preference for one or another version, etc.
However, the R environment only allows a single version of a given package to be loaded at a time. This is reasonable as a method of conveniently controlling function names. If we load a second library, the standard behaviour of R is to overwrite an existing version when a new one is installed.
There is not much we can do about loading different versions of a library into the same R session. However, we do have options to facilitate installing and managing versions that can be loaded and used on demand.
In this tutorial we will explore three different approaches to do this:
- Installing each version in a different directory so they don’t overwrite each other.
- Managing several local environments as done by the
renv
package. - A unique workaround using
asreml
.
In the following examples, we will use two versions of VSNi’s asreml
R package: version 4.1 and version 4.2. These versions are essentially the same in terms of users interface and code, but many things have changed in the backend leading to performance improvements. Hence, we are interested in comparing them in terms of behaviour, speed and consistency.
Let’s have a close look at each method, then you can choose the one that best fits your needs.
Setup: installing dependencies
Before exploring the different approaches, you will first need to install the libraries that asreml
depends on.
install.packages(c("data.table", "ggplot2", "jsonlite"))
The downloaded packages can be found in:
/var/folders/pz/f8v457md7mdb53d7zm70y9fh0000gn/T//RtmpbXblB3/downloaded_packages
Approach 1. Using different library directories
This is probably the best option for most users as it follows a straightforward logic. Also, it uses less disk space (something that will become clearer later). Nevertheless, if the library for which we want multiple versions (asreml
in this case) also depends on different versions of another library (say, data.table
), this approach becomes less useful. This is because you will need to install both versions of the target library and any other dependency. Fortunately, this is not the case with asreml
versions 4.1 and 4.2.
Installing asreml
version 4.1
Normally, R libraries are installed in one of the paths that are easily accessed by calling: .libPaths()
. Note that more than one path might be returned; below we are reporting just the first one ([1]
).
.libPaths()[1]
/Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library
Now we can proceed to install the library asreml
, but we are not going to do this directly into the above path as both versions 4.1 and 4.2 will (conventionally) use the same directory name: “/asreml”. If we ignore this, the second installation will overwrite the first one.
So instead of installing it directly in the main path (as shown above) we are going to create a subdirectory called “/asreml41” (for asreml
version 4.1) so it is not overwritten by version 4.2 when we install it later on. To do this use the following command:
dir.create(paste0(.libPaths()[1], "/asreml41"))
Next, we proceed to install asreml
version 4.1 by specifiying where the installer is (in the home folder in this case) followed by the path to the subdirectory we just created above (“/asreml41”).
install.packages(
"~/asreml-4.1.0.189-macOS-11.7.6-R4.3.tar.gz", # Path to the installer (remember to update to your case)
repos = NULL,
lib = paste0(.libPaths(), "/asreml41")) # Path to install it.
Note that the call shown below to load the library will fail as we have not installed asreml
in a path listed in .libPaths()
. This is because R does not find it at first. But if you have a previous installation of asreml
, this call will work, but it will load your current instalation!
library(asreml)
Error in library(asreml): there is no package called ‘asreml’ Traceback:1. library(asreml) |
library(asreml, lib.loc = paste0(.libPaths()[1], "/asreml41"))
packageVersion("asreml")
Loading required packages: Matrix |
Offline License checked out Tue Jun 13 11:51:30 2023
[1] ‘4.1.0.189’
This has worked properly, hence, we can proceed now to install asreml version 4.2.
Installing asreml
version 4.2
After the procees above, it is good to re-start our R session to make sure all works fine and there is no interference of version 4.2 with version 4.1. All commands to install and verify are shown below.
# Create subdirectory. dir.create(paste0(.libPaths()[1], "/asreml42")) # Install asreml 4.2. install.packages( "~/asreml_4.2.0.257.tgz", # Remember to update. repos = NULL, lib = paste0(.libPaths(), "/asreml42")) # Load asreml 4.2. library(asreml, lib.loc = paste0(.libPaths()[1], "/asreml42")) # Check version. packageVersion("asreml")
Loading required packages: Matrix |
Offline License checked out Tue Jun 13 11:53:50 2023
Loading ASReml-R version 4.2 |
[1] ‘4.2.0.257’
As you can see it is all working properly, and we can now fit our linear mixed models. But, just remember: do not load both versions into the same session, as this will likely create issues.
Note that, in case you use one version more than the other, there is also the possibility of installing the most frequently used in .libPaths()[1]
(loaded with library(asreml)
) and the least used one in a subfolder as shown above (loaded with library(asreml, lib.loc = paste0(.libPaths()[1], "/subfolder"))
).
Approach 2. Managing environments
An alternative approach is to use the renv
library to keep installations of asreml
separated into different environments/projects (i.e., copies of R base and libraries). Each of these environments deals with separate R installs, and each will have a specific set of packages (and package versions). Managing different environments is preferred when reproducibility is important, but it will result in considerably larger hard drive usage as you will have several copies of the same libraries. For more details on how environments work in R, we recomend this article https://rstudio.github.io/renv/articles/renv.html.
To get started, first install the R library renv
.
install.packages("renv")
The downloaded binary packages can be found in:
/var/folders/pz/f8v457md7mdb53d7zm70y9fh0000gn/T//RtmpPE2GBP/downloaded_packages
Installing asreml
version 4.1
After loading the renv
library, we will initiate (create) a new environment that we will call “asreml41”. Note that this environment is located in the home folder (called “R” here for convenience). The renv
library will create the environment with a copy of R and its base libraries.
# Create a new environment for asreml 4.1.
renv::init("~/R/asreml41", restart = TRUE)
asreml
version 4.1. Note that asreml
will not be installed along with the other packages in the “global” .libPaths()
, it will only be installed in the active environment we just created.renv::install("~/asreml-4.1.0.189-macOS-11.7.6-R4.3.tar.gz") # Remember to update.
Installing asreml [4.1.0.189] …
OK [built from source in 21 seconds]
* Installed 1 package in 22 seconds.
Installing asreml
version 4.2
We can continue with asreml
version 4.2 in a similar way, but this is now installed into a new environment that we will call “asreml42” following very similar code as done above. After this, it is good practice to restart the R session to avoid future conflicts.
# Create a new environment for ASReml 4.2.
renv::init("~/R/asreml42", restart = TRUE)
# Install asreml 4.2.
renv::install("~/asreml_4.2.0.257.tgz") # Remember to update.
Installing asreml [4.2.0.257] …
OK [installed binary in 2.7 seconds]
* Installed 1 package in 3.2 seconds.
Navigating between environments
Everything has been installed, including specific companion R libraries associated with each of the versions of asreml
. We can now navigate between environments to access the different versions. You can do this using the activate
function within renv
. In the code below, we do this then we check which asreml
version is been loaded.
renv::activate(project = "~/R/asreml41")
packageVersion("asreml")
[1] ‘4.1.0.189’
renv::activate(project = "~/R/asreml42")
packageVersion("asreml")
[1] ‘4.2.0.257’
Approach 3. Selecting the asreml
core
This is a third (and even easier) approach to navigate through the different versions of asreml
that is unique to 4.2. This version actually includes the two cores, one for 4.1 and one for 4.2.
To do this, first restart the R session. Then tell R via the options
function which core to load. The default is 4.2, but I could also load 4.1, as shown in the code below.
options(asreml_required_versions = "4.1")
Then, we just load our installation of asreml
4.2.
library(asreml, lib.loc = paste0(.libPaths()[1], "/asreml42"))
Loading required packages: Matrix |
Offline License checked out Tue Jun 13 12:00:21 2023
Loading ASReml-R version 4.1 |
Note the message “Loading ASReml-R version 4.1” that informs us that we have loaded version 4.1. In this example we are using the previous installation from the dedicated directory (“/asreml42”). But, if asreml
is installed in the conventional path printed by .libPaths()
, then the simple command library(asreml)
is all we need.
It is important to note that if we identify the version with packageVersion
, this will show that we are using asreml
4.2, but we are effectivelly loading the the core of asreml
version 4.1.
packageVersion("asreml")
[1] ‘4.2.0.257’
We hope these instructions were useful and provided some clear options for running different versions of our library. If you need further assistance please contact us at: support@vsni.co.uk.