How to Use Multiple Versions of the Same R Package

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

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.

How do different versions of R manage to seemingly automatically download different appropriate package versions from CRAN?

This isn't an answer, but it is too long for a comment. If I understood correctly, an user can tell you that he wants the package X and you have to provide it to him. I guess that you can download somewhere the correct version and transfer the package to the user's machine.

This seems hard to do in general, but maybe doable. However, it requires a little cooperation from the user.

As already said in the comments, the package archive exists and has all the old versions of each package. Keep in mind that the following line:

installed.packages()

gives you all the packages installed on a machine. I don't think that this could be too much to ask to the user.

I'd proceed as follow.

  • Get to the archive page of the desired package.
  • Download the newest package source, unfold it and check the DESCRIPTION file.
  • From it, see if the package is compatible with the user's R version. If not, download the second-new and so on.
  • Check the fields Depends and Import of the DESCRIPTION file, to meet the dependencies and see if they are installed by examining the object returned by installed.packages() from the user's machine.
  • If a package is missing, go recursively to meet all the dependencies.
  • If you are done, now you need to compile each downloaded package (since archives seems to have just the source code, you need to compile it to distribute it to the user's machine).

All the above steps can be done by writing a simple function. It might be boresom to deal with all the exceptions, but I guess that it shouldn't be so hard to do the above in R (or in other programming languages, depending on your expertise).

R versions and associated libraries

Based on the comments above and testing on my machine R seems to use the libraries associated with whatever R version is being used in the following directories (e.g. c:/Users/user1/Documents/R/win-library/4.1; c:/Program Files/R/R-4.1.2/library). As @r2evans has stated above this can be changed using the .libPaths() function.

Installing older version of R package

To install an older version of a package from source (within R):

packageurl <- "http://cran.r-project.org/src/contrib/Archive/ggplot2/ggplot2_0.9.1.tar.gz"
install.packages(packageurl, repos=NULL, type="source")

If this doesn't work for you and you're on Windows, the reason is probably the lack of an appropriate tool chain for building/compiling packages. Normally you would install a pre-compiled binary from CRAN but they only archive package sources, not binaries.[1] This means you need to install Rtools so that you can compile everything locally. (Note: Rtools is not an R package.)

@shadow's answer below also makes the case that you can use devtools::install_version(). That's also a good idea, but is also subject to needing Rtools on Windows.

As of September 18, 2015, a new package versions has appeared on CRAN. This relies on the Revolution Analytics MRAN server to install packages for specific versions or dates:

# install yesterday's version of checkpoint, by date
install.dates('checkpoint', Sys.Date() - 1)

# install earlier versions of checkpoint and devtools
install.versions(c('checkpoint', 'devtools'), c('0.3.3', '1.6.1'))

That has the advantage of not requiring Rtools to install binary packages on Windows, but only works going back to 2014-09-17 (when MRAN was launched).

To install an older version from the command line (outside of R):

You can also install a package by using R CMD INSTALL on the command line (Terminal, Command Prompt, etc.) once you have the package source ("tarball") locally on your machine, for example using wget (if you have it):

wget http://cran.r-project.org/src/contrib/Archive/ggplot2/ggplot2_0.9.1.tar.gz

or, if you're on Windows, an equivalent using PowerShell would be:

(new-object System.Net.WebClient).DownloadFile("http://cran.r-project.org/src/contrib/Archive/ggplot2/ggplot2_0.9.1.tar.gz", "./ggplot2_0.9.1.tar.gz")

or you can just download the source from the CRAN archive via your web browser.

To install from the local file, you can just do:

R CMD INSTALL ggplot2_0.9.1.tar.gz

That should work on any platform (with the same caveat - as above - about needing a tool chain for building packages).


[1]This is no longer entirely true. From March 2016, CRAN has started hosting a "CRAN Archive" server that contains Windows and Mac binaries for very old versions of R (> 5 years old). You can now install directly from this server using install.packages(). See new R FAQ 7.44 for some details.



Related Topics



Leave a reply



Submit