208

I am trying to use Rpy2 and ggplot2 but I get an error. After some searching for the error online, I found that the error occurs because there are changes in the ggplot2 package that are not yet reflected in Rpy2 (for example, see this post (Edit: Link is now dead)).

So I now need to install an older version of ggplot2. Here is pseudo-code for what I want:

install.packages("ggplot2", version='0.9.1')

But install.packages does not have a version argument. How do I do it?

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
hirolau
  • 13,451
  • 8
  • 35
  • 47
  • try `checkBuilt`? http://stackoverflow.com/questions/16503554/r-3-0-0-update-has-left-loads-of-2-x-packages-incompatible – isomorphismes Feb 13 '14 at 14:36

8 Answers8

209

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.

Community
  • 1
  • 1
Thomas
  • 43,637
  • 12
  • 109
  • 140
  • 4
    Thank you, I guess it is the correct way. Too bad I now get: `Warning message: package ‘http://cran.r-project.org/src/contrib/Archive/ggplot2/ggplot2_0.9.1.tar.gz’ is not available (for R version 2.15.2)` – hirolau Jun 13 '13 at 14:17
  • 3
    Had a similar issue with Rcpp. This could be due to the R version dependency of the package (DEPENDS: ), or it might be that the running R session will not let you perform the update. What I found worked for me was to carry out the update in the manner that Mark Butler (below) suggested (R CMD INSTALL your_version.tar.gz). This was for R 2.15.3 in my case. – Robert Casey Jan 16 '14 at 19:09
  • @Antoine Are you on Windows? And, if so, do you have Rtools installed? – Thomas Jul 16 '15 at 09:43
  • @Thomas Thanks for responding. Yes, I am on Windows with Rtools "pseudo-installed" see this [thread](https://github.com/talgalili/installr/issues/36). – Antoine Jul 16 '15 at 10:32
  • @Thomas I am going to try to run my script on Amazon EC2 to avoid the obvious local problems on my machine (library paths, versions of R vs versions of packages, compiling issues etc.) – Antoine Jul 16 '15 at 10:35
  • The `install.versions` function does not exist. You might need to update your answer. – Luís de Sousa May 08 '18 at 10:35
  • Actually it does, in the **versions** package mentioned in the answer... – Thomas May 09 '18 at 04:54
  • Worked for me (I tried to install an older version of `glmnet`). There was an unsatisfied dependency (`foreach`) which I had to install in the usual manner before I installed `glmnet` from source. – András Aszódi Nov 19 '19 at 13:10
  • I am very sad to say that this answer must now be updated due to the retirement of MRAN. https://techcommunity.microsoft.com/t5/azure-sql-blog/microsoft-r-application-network-retirement/ba-p/3707161 – eric_kernfeld Aug 11 '23 at 14:43
148

The remotes package offers an install_version function that can do this directly.

require(remotes)
install_version("ggplot2", version = "0.9.1", repos = "http://cran.us.r-project.org")

Previously, this answer pointed to the devtools package, which also re-exports the install_version function. Thanks @MichaelChirico for pointing out that the remotes package is preferable.

shadow
  • 21,823
  • 4
  • 63
  • 77
  • can you keep multiple versions of the same package without having to ``install_version`` every time you wish to load a specific version? say, ``library(ggplot2-0.9.1)`` – PatrickT Nov 10 '18 at 07:43
  • 1
    @PatrickT You can do this by using a local library. For example `install_version('ggplot2', version = 0.9.1, lib = 'path_to_library_with_ggplot_0.9.1')` and `library(ggplot2, lib.loc = 'path_to_library_with_ggplot_0.9.1')` – shadow Nov 10 '18 at 14:30
  • 1
    you could also use .libPaths("/dir_for_custom_library_versions") before running install_version – tjjjohnson Nov 27 '18 at 04:14
12

Pure install.packages method

See this thread on the r-devel mailing list. In reply to Kurt Wheeler, Kurt Hornik reveals an undocumented feature of the CRAN website to specify the specific version of a package.

This method will work as long as you have all required dependencies already installed:

package = "https://cran.r-project.org/package=svglite&version=1.2.1"
utils::install.packages(pkgs = package, repos = NULL)

Note the URL structure above. This addresses the issue that CRAN has a different URL structure for the latest version than for archived versions:

# Latest version (not available at Archive/svglite)
https://cran.r-project.org/src/contrib/svglite_1.2.1.tar.gz
# Archived version
https://cran.r-project.org/src/contrib/Archive/svglite/svglite_1.2.0.tar.gz

remotes::install_version method

Another option is to use the remotes::install_version function. However, you will need to install the remotes package.

Daniel Himmelstein
  • 1,759
  • 1
  • 21
  • 26
8

You can download your appropriate version from the link below as a zip file.

http://cran.r-project.org/src/contrib/Archive/ggplot2/

In R Studio: Tools >> Install packages >> Install from: (select drop down)

Package Archive File(.zip, .tar.gz).

Choose your newly-downloaded-package-zip-file and install the package

user23899
  • 91
  • 1
  • 1
5

Using install.packages as described in another answer does not work for me.

The best alternative I found is to use function install_url from package devtools.

Another possibility that I have not explored further:

  1. Download the older .tar.gz source file from the package archives.
  2. Follow the steps documented on http://rtm.wustl.edu/writings/htrtargz.pdf to install it locally.
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
asieira
  • 3,513
  • 3
  • 23
  • 23
5

There is a versions package that simplifies this task considerably, for package versions released since 2014-09-17. It uses snapshots of the MRAN server at Revolution Analytics to:

  • show release dates and MRAN availability of any CRAN package (available.versions),

  • install specified versions of one or more packages(install.versions), or

  • install package versions available as of any specified date (install.dates). It does the installation from the MRAN server via the standard install.packages function, so available binary versions can be installed instead of having to compile from source, and package dependencies as of the specified date can be included.

There might of course be compatibility issues with combinations of package versions and R versions. For running different R versions, see for example this page.

EdM
  • 405
  • 4
  • 11
4

Found a good solution, which worked for me (the details are at the link).

Command in "repmis" library:

# Install old versions of the e1071 and gtools packages.

# Create vectors of the package names and versions to install
# Note the names and version numbers must be in the same order
Names <- c("e1071", "gtools")
Vers <- c("1.6", "2.6.1")

# Install old package versions into the default library
InstallOldPackages(pkgs = Names, versions = Vers)
llrs
  • 3,308
  • 35
  • 68
Aybek Khodiev
  • 596
  • 1
  • 4
  • 10
4

Another option is the {groundhog} package. It helps install an older package Version from CRAN by specifying a date. This is especially helpful when one doesn't remember the specific package version, but rather the time the script was still working. In case of {ggplot2} version 0.9.1 was loaded on CRAN in May 2012 so we can take a date from June.

library("groundhog")
groundhog.library("ggplot2", "2012-06-01")
TimTeaFan
  • 17,549
  • 4
  • 18
  • 39