Painless Way to Install a New Version of R

Painless way to install a new version of R?

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.

How to update all packages for a new R version quickly and easily?

Don't know if this is quick and easy, but I think the pacman package can be useful.

  1. Under the previous version, use pacman::p_lib() to return a vector of your installed packages, and save them onto disk use saveRDS().

For instance,

mypks <- pacman::p_lib()
saveRDS(mypks, "~/mypks.rds")

  1. Update R.

  2. Import the vector from step 1 using readRDS() and run install.packages() with the object.

For instance,

mypks <- readRDS("~/mypks.rds")
install.packages(mypks)

Update R using RStudio

You install a new version of R from the official website.

RStudio should automatically start with the new version when you relaunch it.

In case you need to do it manually, in RStudio, go to :Tools -> options -> General.

Check @micstr's answer for a more detailed walkthrough.

How to upgrade R in linux?


Note: I now keep on GitHub (here) an up-to-date guide to upgrading R on Linux Mint or Ubuntu Linux, which also includes a bit of extra information about system dependencies for tidyverse, the popular set of data-wrangling packages, as well as devtools, the popular R package development... package.


The link provided by FedRo is a good resource, however a slight change would need to be made since you're using Linux Mint 18.1 which uses Xenial repositories rather than Trusty repositories (see here). I also typically use the approach here to deal with packages I've already installed when I upgrade R rather than the approach offered by FedRo. So, for completeness, these are all the steps you'd need to take:

Step 1

Go to CRAN's list of mirrors and find the URL of the mirror that is closest to you. The terminal commands below assume you choose http://cran.wustl.edu/

Step 2

Open a terminal and enter the following commands1 (replacing http://cran.wustl.edu/ with whichever CRAN mirror URL you chose in step 1):

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo echo "deb http://cran.wustl.edu/bin/linux/ubuntu xenial/" | sudo tee -a /etc/apt/sources.list
sudo apt update
sudo apt upgrade r-base r-base-dev
sudo apt update
sudo apt upgrade

Note also that I have put to upgrade r-base and r-base-dev, but I don't know if you have r-base-dev installed. If not, I highly recommend you install it via sudo apt install r-base-dev.

Step 3

Start a new R session and run the following:

update.packages(checkBuilt=TRUE, ask=FALSE)

Then you should be good to go.

Update: Linux Mint 19 and R 3.6.x

Since both Linux Mint and R have seen upgrades since I answered this question, I'm updating for those who come to this answer needing the info for Linux Mint 19 and R 3.6.x.

The only difference is that instead of the command

sudo echo "deb http://cran.wustl.edu/bin/linux/ubuntu xenial/" | sudo tee -a /etc/apt/sources.list

you need to use

sudo echo "deb http://cran.wustl.edu/bin/linux/ubuntu bionic-cran35/" | sudo tee -a /etc/apt/sources.list

(replacing http://cran.wustl.edu/ with whichever CRAN mirror URL you chose in step 1)


1 I put here the full key, though many other guides you may see will use only the "short key."
I have updated this guide to use the full key out of security concerns (see here, for example).

Clarification needed for different approaches to updating R on a mac (and keeping packages)

1) Yes.

2) The newly installed packages will be at the latest version, but there is no harm in running update.packages(ask=FALSE, checkBuilt=TRUE) afterward just be be sure everything is up to date.

3) I've not seen anyone suggest this particular approach, though a similar process is suggested in the R for Windows FAQ that consists of 1) installing the new version 2) copying the packages from the library folder of the old version to the library folder of the new version and 2) running update.packages(checkBuilt=TRUE, ask=FALSE). I don't think there are any clear advantages to doing it this way vs. the installed.packages()... way.

4) All methods under discussion are limited to CRAN packages, so things that you installed from e.g., bioconductor will not be covered. Also packages removed from CRAN will not be covered, you will either have to use an older version of R or build and install them manually.



Related Topics



Leave a reply



Submit