Painless way to install a new version of R?
Solution 1:
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.
Solution 2:
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.
Solution 3:
Two quick suggestions:
Use Gabor's batchfiles which are said to comprise tools helping with e.g. this bulk library relocations. Caveat: I have not used them.
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"
Solution 4:
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):
- Install R-2.11.0
- Copy
R-2.10.0/library/*
toR-2.11.0/library/
- Answer "no" to the prompts asking you if it is okay to overwrite.
- Start R 2.11.0
- Run the R command
update.packages()
Solution 5:
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.