Update: This post has a follow-up for how to upgrade R on windows 7 explaining how to deal with permission issues.
Background – how I heard that there is more then one way to upgrade R
If you didn’t hear it by now – R 2.11.0 is out with a bunch of new features.
After Andrew Gelman recently lamented the lack of an easy upgrade process for R, a Stackoverflow thread (by JD Long) invited R users to share their strategies for easily upgrading R.
Upgrading strategy – moving to a global R library
In that thread, Dirk Eddelbuettel suggested another idea for upgrading R. His idea is of using a folder for R’s packages which is outside the standard directory tree of the installation (a different strategy then the one offered on the R FAQ).
The idea of this upgrading strategy is to save us steps in upgrading. So when you wish to upgrade R, instead of doing the following three steps:
- download new R and install
- copy the “library” content from the old R to the new R
- upgrade all of the packages (in the library folder) to the new version of R.
You could instead just have steps 1 and 3, and skip step 2 (thus, saving us time…).
For example, under windows XP, you might have R installed on:
C:Program FilesRR-2.11.0
But (in this alternative model for upgrading) you will have your packages library on a “global library folder” (global in the sense of independent of a specific R version):
C:Program FilesRlibrary
So in order to use this strategy, you will need to do the following steps (all of them are performed in an R code provided later in the post)-
- In the OLD R installation (in the first time you move to the new system of managing the upgrade):
- Create a new global library folder (if it doesn’t exist)
- Copy to the new “global library folder” all of your packages from the old R installation
- After you move to this system – the steps 1 and 2 would not need to be repeated. (hence the advantage)
- In the NEW R installation:
- Create a new global library folder (if it doesn’t exist – in case this is your first R installation)
- Premenantly point to the Global library folder whenever R starts
- (Optional) Delete from the “Global library folder” all the packages that already exist in the local library folder of the new R install (no need to have doubles)
- Update all packages. (notice that you picked a mirror where the packages are up-to-date, you sometimes need to choose another mirror)
Thanks to help from Dirk, David Winsemius and Uwe Ligges, I was able to write the following R code to perform all the tasks I described 🙂
So first you will need to run the following code:
Code for upgrading R – only two lines for each R installation (old/new)
The code to perform all of the steps described above is available here, and can be accessed from within R using “source”.
In order to move your R upgrade to the new (simpler) system (assuming you already use R, and that it is your first time moving to the new system), do the following:
1) Download and install the new version of R
2) Open your old R and run –
source("https://www.r-statistics.com/wp-content/uploads/2010/04/upgrading-R-on-windows.r.txt") Old.R.RunMe()
(wait until it finishes)
3) Open your new R and run
source("https://www.r-statistics.com/wp-content/uploads/2010/04/upgrading-R-on-windows.r.txt")
New.R.RunMe()
(wait until it finishes)
Once you do this, then from now on, whenever you will upgrade to a new R in the future, all you will need to do are the following TWO (instead of three) steps:
1) Download and install the new version of R
2) Open your new R and run
source("https://www.r-statistics.com/wp-content/uploads/2010/04/upgrading-R-on-windows.r.txt")
New.R.RunMe()
(wait until it finishes)
And that is it.
Update: This post has a follow-up for how to upgrade R on windows 7 explaining how to deal with permission issues.
That’s a good alternative to my upgrading.
I copy the packages from the old installation to the new one and then use the update.packages(repos=xxx, ask=FALSE).
Thanks Manos,
Up until recently I upgraded the same way as you described.
I am wondering if the strategy I presented here might be adopted by the R core team (as a part of the regular installation process).
I’ll soon send this post to the R mailing list for an opinion.
Nice idea to get around the tedious upgrade process. If I have followed the post correctly would your approach be useful for systems where people couldn’t use the internet to automatically download and install packages as required?
Hi Ralph,
In a sense – yes and no.
The advantage of this system (which is not “mine”, due credit was given in the post) is that once you move to using it, then whenever you upgrade to a new R version – you don’t need to run or copy anything from the old R version (because the package library is a “shared” one).
What that means is that you could install a new R version on a system, and not need to download all of it’s packages (or copy them from your old R version) – which could allow for a faster upgrade process.
However, it is important to note that you might still want to update your packages to their new version, since some of them might not be forward compatible (although I remember reading that is R is usually forward compatible with code and packages)
I hope my reply helped…
Tal
Thanks for the great script.
I made a new installation of R 2.11 (going from 32-bit ot 64-bit).
I ran your script which created the global directory and the Renviron.site file.
However, when I go to install packages R still asks me to create a folder in the Documents folder where the R working directory is.
How do I force it to install into this global library?
Hi Jared,
I am glad you found the script useful 🙂
Regarding your issue,
When I install a new package it fits it to my global library.
So please check the following:
1) What is your output for:
.libPaths()
2) Did you restart R prior to installing a new package? (it is not suppose to matter, but I wonder if you didn’t, then will restarting R will solve the issue)
Also, I am not fully sure what you describe as “R still asks me to create a folder in the Documents folder where the R working directory is”.
What is the exact massage/screenshot? (You can send me images through the contact me page on the top right of the blog)
Also, did you erase (manually) your library folder under R installation directory?
Cheers,
Tal
It turns out the “c:Program FilesRlibrary” folder was read-only and that’s why R kept asking me to create a new folder elsewhere.
After I changed the permissions on the folder, the new packages installed in there.
However, a lot of “basic” packages such as lattice, MASS, survival, nlme are all in “C:Program FilesRR-2.11.0-x64library.”
I’m assuming that’s supposed to be that way?
Hi Jared,
Thanks for updating about the problem you presented.
Regarding your last question – here is my reasoning (although it turned out a bit longer then I had hoped – so sorry about that, don’t feel compelled to read it)
I was debating what to do about that when I wrote the code.
The basic R installation always comes with some packages, and they are expected to be of a newer version then what the old R installation had. So the question was what to do with them.
The option I choose was to keep them in their directory (thus treating them as part of R, in a sense), and erasing the copy of those packages in the global library folder (so to avoid potential conflicts that I don’t know about).
The only downside of this is if a new distribution of R would come out without a package that was available in the older R version.
I decided, writing my code, to assume that this isn’t likely to occur. And in case it will, that they user would just need to download that package.
If you (or any one else) thinks the code should be designed differently, please let me know.
Best,
Tal
I think that is a good point. The base packages are barely noticed, do they even need to have a library() call to be used?
Moving the “other” packages is far more important, to me at least.
Tal:
For me it worked perfectly. Thanks a lot for taking the time to write the code for us.
Ernesto
Thanks for this extraordinary column. Here I am using the latest version of R i.e. R-2.10.1. My intention is to upgrade all my R installations for any future releasees using your method. Therefore I run following :
> source(“https://www.r-statistics.com/wp-content/uploads/2010/04/upgrading-R-on-windows.r.txt”)
> Old.R.RunMe()
[1] “The path: C:/Program Files/R/library Didn’t exist – and was now created.”
[1] “———————–”
[1] “I am now copying packages from old library folder to:”
[1] “C:/Program Files/R/library”
[1] “———————–”
Error in file.copy(from = list.of.dirs.in.lib, to = global.library.folder, :
more ‘from’ files than ‘to’ files
Which generated above error. How should I fix this problem?
Thanks,
Hello Bogaso,
Thank you for the supporting words, I am glad my work is of use to others in our community 🙂
Regarding the error you described, it is odd.
Could you please run the following line in your console and paste in the comments what the output is ?
paste( paste(R.home(), “\library\”, sep = “”),
list.files(paste(R.home(), “\library\”, sep = “”)),
sep = “”)
Hi Bogaso,
What operation system do you use?
After some recent checks I have made, I am guessing your issue is on either “windows vista” or “windows 7”.
The problem is probably since your regular user doesn’t have permission to write into the “program files” directory.
By the way, also notice that on those operation systems, R is usually installed on
C:Program Files (x86)R
So you should run the commands like this:
Old.R.RunMe(global.library.folder = “C:\Program Files (x86)\R\library”)
This way the directory will be created in the correct location.
In any case, the solution is run the code when using the administrator user.
Or temporarily change the writing permissions to this folder.
You can change permissions by following the steps here:
http://www.blogsdna.com/2159/how-to-take-ownership-grant-permissions-to-access-files-folder-in-windows-7.htm
Changing owner to you, and then giving you writing privileges.
If someone has a more elegant solution for this I’d be happy to know.
Tal
Hi Bogaso,
I updated my post to reflect a simpler solution to R’s permissions issue.
Hi Tal,
Your upgrade scripts worked perfectly for me. Many thanks!
Cheers,
Matt
Hi Tal!
I find your tips very useful for easening the tedium of updating R. Right now, I am having some problems when I try to extend your approach to try something a bit more complicated. Let me explain.
I first downloaded the excellent RAndFriends program from the statconn website :
http://rcom.univie.ac.at/
I find this “package” of R (and additional packages) to be quite useful for integrating R and MS excel (I work on a pc with win XP). Updating is quite easy because a new version of RAndFriends is provided with every new version of R. However, I use some packages not included in the RAndFriends package. I would like to keep these extra packages separate from the RAndFriends lot so that I need to exclusively update only these with a new version of R. It is here that I am having some trouble.
Your approach worked fine for me in the first step – in creating a global library folder with the command : old.R.RunMe. I now get
> Sys.getenv(“R_LIBS”)
[1] “C:/PROGRA~1/R/library”
I tried to create an extra library folder by including the following command in the /etc/Rprofile.site file :
.libPaths(new=”C:/PROGRA~1/R/library2″)
I then installed the ctv package with the following commands :
pkgs<- "ctv"
destDir<- "C:\Program Files\R\library3"
install.packages(pkgs, lib="C:\Program Files\R\library2",destdir = destDir,
dependencies = c("Depends", "Imports", "LinkingTo","Suggests", "Enhances"),
type = "win.binary")
This worked all right. The command library(ctv) responded positively. But the new problem was that I could not now load the packages from the global library folder. I would like to know how I can work with multiple library folders.
I did not succeed either in installing to the "R_LIBS_USER" folder. I got the following error message there :
'lib = "R_LIBS_USER"' is not writable
Error in install.packages(pkgs, lib = "R_LIBS_USER", destdir = destDir, :
unable to install packages
I would appreciate any help that I can get.
Thanks,
Ravi
Hi Ravi.
First, It seems like you might have permission errors. Please see my more updated post on: https://www.r-statistics.com/2011/04/how-to-upgrade-r-on-windows/
Second, You need to have two libraries defined. The way I did it in the code is to not use
/etc/Rprofile.site
But instead use:
/etc/Renviron.site
All you need to enter in it is the “global library folder” you use. In my case I used:
R_LIBS=’D:/R/library’
p.s: I believe using the code for the old and new R would do this for you…
I hope this helps.
Cheers,
Tal
The step that you mention early-on in your discussion:
“Permanently point to the Global library folder whenever R starts”.
How do you do the “permanently point” part??? I can find nothing that I can modify in order to do this.
As for your code–Am I correct in assuming that it is entered into the R console? (I can’t quite figure out its logic.) Some documentation that is purported to show how to do this is not “clearly outlined” as some folks in the “R” community have said to others with the same question. It may be documented–but not clearly.
I have already downloaded R 2.13.1, and, since I (earlier) installed R 2.12.2–transferring all of the Library elements ‘the hard way’, it was a simple matter to move the old Library to CProgram FilesRLibrary. So–in the sense of the file tree, the Library is in a good global position (I copied the new Library elements to the “old’ library–that is now in the new position–the ‘old’ Library is huge.) Now, I have to know how to ‘point to it’. (I can’t quite see the logic in the steps you offered, but am anxious to use them, or some of them, because I like your idea.
I hope that my confusion is clear…
Tim Paysen
Hi Tim,
1) Pointing to the global library is done through creating and adding a line of code to the Renviron.site file.
2) The process should be simply. Just run two lines of code in the R console of the old R installation, and then run the other two lines in the new R console.
3) Consider looking also at: https://www.r-statistics.com/2011/04/how-to-upgrade-r-on-windows/
I hope this helps,
Tal
I ran the supplied code in my original R package (2.7.2), and then downloaded and installed R 2.13.1. When I open 2.13.1, I get this error message:
“Fatal error: unable to restore saved data in .RData”
Is it likely that this is related to the Old.R.RunMe() script?
Hi Craig,
I suspect it relates to moving from a relative old R version to a newer one.
Question: Do you get this error when starting R? or after doing something?
Hi Tal,
Thanks for your quick response to my question. The error message comes up when I open the new R version, and before I do anything; R then shuts down. The old version of R still works fine.
One minor point I discovered when doing this by the first time today. If you do the steps above having already installed the new version of R. All runs well until you uninstall the older R version: the file association with .Rdata is lost and there is no simple means to recover it in Windows…
Interesting.
Hello. I followed all the steps mentioned here. When I tried to load a new package, this error appears in R window:
Warning in install.packages(NULL, .libPaths()[1L], dependencies = NA, type = type) :
‘lib = “C:/Program Files/R/library”‘ is not writable
Error in install.packages(NULL, .libPaths()[1L], dependencies = NA, type = type) :
unable to install packages
Any idea how to solve this? Thanks.
Yes, install R on the D hard drive.
In any case, look at the troubleshooting section here:
https://www.r-statistics.com/2011/04/how-to-upgrade-r-on-windows/