Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiently getting older versions of R packages

Tags:

r

A recurring question on SO is that package xx is not available for R version 2.xx.xx. For example the gplots package requires the user to have R 3.0 installed in order for it to install. You can get older versions in the Archive of CRAN, but:

  • It is not easy to see which version of the package you need to get for a specific R version.
  • You need to build the package from source, which is primarily a (mild) challenge under Windows.

My question is the following: is there a more effective workflow in getting older package versions which match your older version of R? In the spirit of having different package repositories for different version of ubuntu.

I know one option would be to just get the latest version of R, but there might be some pressing reason to stick to a certain version of R. For example, one could be interested in repeating an old experiment which relies on an old version of R and support packages. Or one is limited by the system administration.

like image 405
Paul Hiemstra Avatar asked Apr 18 '13 19:04

Paul Hiemstra


People also ask

How do I get older versions of R packages?

If you're on Windows or OS X and looking for a package for an older version of R (R 2.1 or below), you can check the CRAN binary archive. Note that if you are installing from source, you'll need to make sure you have the necessary toolchain to build packages from source.

How do I run a specific version of an R package?

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.

Do I need to install R packages every time?

You only need to install packages the first time you use R (or after updating to a new version). **R Tip:** You can just type this into the command line of R to install each package. Once a package is installed, you don't have to install it again while using the version of R!


1 Answers

This is entirely untested (I'm running the latest version of R and have no time at the moment to install an old version of R to test it out), but perhaps one idea is to grab the dates from the "Archive" page for the package, compare that to the date for your R version, and progressively try installing the earlier versions, starting with the most recent version.

Something like this might be a starting point:

install_archive <- function(PackageName) {
  if(!require("XML"))
      install.packages("XML")
  if(!require("devtools"))
      install.packages("devtools")
  rVersionDate <- as.Date(paste(R.Version()[c("year", "month", "day")],
                                collapse = "-"))
  BaseURL <- "http://cran.r-project.org/src/contrib/Archive/"
  u <- htmlParse(paste(BaseURL, PackageName, sep = ""))
  doc <- readHTMLTable(u, skip.rows=1:2)[[1]][2:3]
  releaseDate <- as.Date(strptime(doc$`Last modified`, 
                                  format="%d-%b-%Y"))
  Closest <- which.min(rVersionDate - 
                         releaseDate[releaseDate <= rVersionDate])
  install_url(paste(BaseURL, doc$Name[Closest], sep = ""))
} 

install_archive("reshape")

From here, I would add at least the following things to the function:

  • I would first try to install the most current version (not from the "Archive"), and if that fails, then move ahead.
  • In moving ahead, I would change the which.min() line to rank(), and try rank == 1, rank == 2, and so on, perhaps setting a maximum rank at which to try.

Even so, this is a lot of "guess and check", only the software is doing the guessing and checking for you automatically. And, of course, the same advice holds that there is probably a good reason it's not on CRAN!

like image 72
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 14 '22 04:10

A5C1D2H2I1M1N2O1R2T1