Paul Hiemstra
Paul Hiemstra

Reputation: 60924

Efficiently getting older versions of R packages

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:

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.

Upvotes: 16

Views: 4769

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193497

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!

Upvotes: 5

Related Questions