Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use multiple versions of the same R package?

In order to be able to compare two versions of a package, I need to able to choose which version of the package that I load. R's package system is set to by default to overwrite existing packages, so that you always have the latest version. How do I override this behaviour?

My thoughts so far are:

I could get the package sources, edit the descriptions to give different names and build, in effect, two different packages. I'd rather be able to work directly with the binaries though, as it is much less hassle.

I don't necessarily need to have both versions of the packages loaded at the same time (just installed somewhere at the same time). I could perhaps mess about with Sys.getenv('R_HOME') to change the place where R installs the packages, and then .libpaths() to change the place where R looks for them. This seems hacky though, so does anyone have any better ideas?

like image 518
Richie Cotton Avatar asked Jun 07 '10 10:06

Richie Cotton


People also ask

How do I use multiple versions of R?

You can select different versions of R by selecting it from the drop down list at the top of the browser window. The drop down menu will allow you to select the version of R you want to use. When you switch versions, the system will ask if you want to save your workspace before restarting your session.

How do I install different versions of R packages?

To install a specific version of a package, we need to install a package called “remotes” and then load it from the library. Afterwards we can use install_version() by specifying the package name and version needed as shown below.

Can I have two versions of R installed?

On windows, different version of R get installed on its own folder so you can choose which one to use, although you can't use two versions simultaneously on the same RStudio session.

What happens if you install a package twice in R?

I wonder, can I do any harm to my system (or the stability of R installation) if I install some package twice. Or when installing a new package it will simply overwrite the previous version. It shouldn't be an issue unless you install a package as an admin user, and again as a normal user.


1 Answers

You could selectively alter the library path. For complete transparency, keep both out of your usual path and then do

 library(foo, lib.loc="~/dev/foo/v1")    ## loads v1 

and

 library(foo, lib.loc="~/dev/foo/v2")    ## loads v2 

The same works for install.packages(), of course. All these commands have a number of arguments, so the hooks you aim for may already be present. So don't look at changing R_HOME, rather look at help(install.packages) (assuming you install from source).

But AFAIK you cannot load the same package twice under the same name.

like image 165
Dirk Eddelbuettel Avatar answered Sep 23 '22 15:09

Dirk Eddelbuettel