Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Painless way to install a new version of R?

People also ask

How do I install a newer version of R?

The easiest way to update R is to simply download the newest version. Install that, and it will overwrite your current version. There are also packages to do the updating: updateR for Mac, and installr for Windows.

Do I need to uninstall R before installing new version?

No need to uninstall. You can download and install the new version of R Studio with no issues.

Can I install RStudio without installing R?

RStudio requires an installation of R 3.0. 1 or higher. You can download the most recent version of R for your environment from CRAN.

How do I upgrade a specific version of R?

To start the updating process for your R installation, type updateR(). This function will check for newer versions of R and if available, it will guide you through the decisions you need to make. If your R installation is up-to-date, it will return FALSE. If you choose to download and install a newer version.


Just for completeness, there are some ways to prevent you from having this problem. As Dirk said, save your packages in another directory on your computer.

install.packages("thepackage",lib="/path/to/directory/with/libraries")

You can change the default .Library value using the function .libPaths too

.libPaths("/path/to/directory/with/libraries")

This will put this path as a first value in the .Library variable, and will make it the default.

If you want to automate this further, you can specify this in the Rprofile.site file, which you find in the /etc/ directory of your R build. Then it will load automatically every time R loads, and you don't have to worry about that any more. You can just install and load packages from the specified directory.

Finally, I have some small code included in my Rprofile.site allowing me to reinstall all packages when I install a new R version. You just have to list them up before you update to the new R version. I do that using an .RData file containing an updated list with all packages.

library(utils)

## Check necessary packages
load("G:\Setinfo\R\packagelist.RData") # includes a vector "pkgs"
installed <- pkgs %in% installed.packages()[, 'Package']
if (length(pkgs[!installed]) >=1){
  install.packages(pkgs[!installed])
}

I make the packagelist.RData by specifying .Last() in my Rprofile.site. This updates the package list if I installed some :

.Last <- function(){
  pkgs <- installed.packages()[,1]
  if (length(pkgs) > length(installed)){
    save(pkgs,file="G:\Setinfo\R\packagelist.RData")
  }
}

When I install a new R version, I just add the necessary elements to the Rprofile.site file and all packages are reinstalled. I have to adjust the Rprofile.site anyway (using sum contrasts, adding the extra code for Tinn-R, these things), so it's not really extra work. It just takes extra time installing all packages anew.

This last bit is equivalent to what is given in the original question as a solution. I just don't need to worry about getting the "installed" list first.

Again, this doesn't work flawless if you have packages that are not installed from CRAN. But this code is easily extendible to include those ones too.


If you are using Windows, you might want to use the installr package:

install.packages("installr")
require(installr)
updateR()

The best way of doing this is from the RGui system. All your packages will be transferred to the new folder and the old ones will be deleted or saved (you can pick either). Then once you open RStudio again, it immediately recognizes that you are using an updated version. For me this worked like a charm.

More info on installr here.


Two quick suggestions:

  1. Use Gabor's batchfiles which are said to comprise tools helping with e.g. this bulk library relocations. Caveat: I have not used them.

  2. Don't install libraries within the 'filetree' of the installed R version. On Windows, I may put R into C:/opt/R/R-$version but place all libraries into C:/opt/R/library/ using the following snippet as it alleviates the problem in the first place:

$ cat .Renviron         # this is using MSys/MinGW which looks like Cygwin  
## Example .Renviron on Windows    
R_LIBS="C:/opt/R/library"

The method suggested above will not completely work if you have packages that are not from CRAN. For example, a personal package or a package downloaded from a non-CRAN site.

My preferred method on Windows (upgrading 2.10.1 to 2.11.0):

  1. Install R-2.11.0
  2. Copy R-2.10.0/library/* to R-2.11.0/library/
  3. Answer "no" to the prompts asking you if it is okay to overwrite.
  4. Start R 2.11.0
  5. Run the R command update.packages()

With respect to the solution given in the question, it might not be easy to run your older version of R if you have already installed the new version. In this case, you can still reinstall all missing packages from the previous R version as follows.

# Get names of packages in previous R version
old.packages <- list.files("/Library/Frameworks/R.framework/Versions/3.2/Resources/library")

# Install packages in the previous version. 

# For each package p in previous version...
    for (p in old.packages) {
      # ... Only if p is not already installed
      if (!(p %in% installed.packages()[,"Package"])) {
        # Install p 
        install.packages(p) 
      }
    }

(Note that the argument to list.files() in the first line of code should be the path to the library directory for your previous R version, where all folders of packages in the previous version are. In my current case, this is "/Library/Frameworks/R.framework/Versions/3.2/Resources/library". This will be different if your previous R version is not 3.2, or if you're on Windows.)

The if statement makes sure that a package is not installed if

  • It's already installed in the new R version, or
  • Has been installed as a dependency from a package installed in a previous iteration of the for loop.

Following Dirk's suggestion, here is some R code to do it on windows: How to easily upgrade R on windows XP

Update (15.04.11): I wrote another post on the subject, explaining how to deal with common issues of upgrading R on windows 7