Reinstalling packages on new version of R

I've just installed the R 3.5 version and need to access all the hundreds of packages I currently have in the 3.4 folder

RStudio suggests
"On most single-user systems (Mac, Windows, and Linux), when you upgrade to a new minor version of R (like 3.3.0 to 3.4.0), R will not find the packages, you will need to reinstall your R packages. This is an inconvenience, but the problem is obvious and it is easy to fix. If you are using a system like this, you can just reinstall your packages after upgrading R. "

That sounds fine if you have one or two packages but is there a quick script that can automate this process for large numbers?
Or is there some package that I could use with the prior version of R to facilitate?

Also can someone also explain what is the issue with just copy and pasting the folders from the file system - which is obviously the quickest method

TIA

Copy-paste will (I think) work in most cases. But some packages need to be recompiled for the new R version and you can't avoid re-installing them. This last release necessitates a lot of this.

1 Like

Thanks
So I can copy and paste and then will just get an error when loading a library I need to recompile? It's all solo work so that would not be an issue

Important: don't do this on a production machine

You ask several questions, and I paraphrase:

What is the danger of copy-pasting packages to a new library location?

For individual work, the risk of doing this is quite low. As already mentioned, some packages will need recompilation due to the internal changes in R-3.5.0. This may lead to runtime errors - so this is the risk you take.

But one strategy is to copy-paste, then run

update.packages(ask = FALSE)

Your second question is:

How can I script the installation of all my packages?

Try something like this, where lib_loc points to the library of your previous release of R

lib_loc <- "C:/Users/apdev/Documents/R/win-library/3.3"
to_install <- unname(installed.packages(lib.loc = lib_loc)[, "Package"])
to_install
install.packages(pkgs = to_install)
12 Likes

Thanks very much for your code suggestions which seem to be what I was looking for

1 Like

On Windows you can also use installr package

1 Like

As @mishabalyasin suggested you can use installr package.

You have:

  • updateR function which updates R runtime to a new version and also its packages, but I believe this is not your case.
  • The function copy.packages.between.libraries to copy files from the old library to the new one. And it doesn't copy fundamental packages as MASS, methods, utils, ...
2 Likes

This is a quick script that I use. It either "require"s all my packages, or if it can't find them, it installs them.

list.of.packages <- c(
"tidyverse",
"timetk",
"tibbletime",
"tidyquant",
"xts",
"lubridate",
"R6",
"here",
"rprojroot")

new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages, repos='https://cran.rstudio.com/', dependencies = TRUE)

for (i.pack in 1:length(list.of.packages)){
suppressMessages(require(list.of.packages[i.pack], character.only = TRUE, quietly = TRUE, warn.conflicts = FALSE))
}

No need to copy and paste libraries to new folder, just point to the old library location using this
https://csgillespie.github.io/efficientR/3-3-r-startup.html#renviron

Then run

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

To byte-compile your packages again with R 3.5.0

6 Likes

Be careful.

This will update all the packages in this shared library location, which is perfect if you're only running R-3.5.0, but may cause problems if you ever want to run code on older versions of R.

If you're the only user, and this is not production code, this practise is probably fine.

But if you are running code in a shared environment, or a production environment, then do make sure you follow robust practise when updating R versions.

1 Like

I define two vectors in my .Rprofile, .cran_packages and .gh_packages (prefixed with . so they're hidden in the global environment), and keep a reasonably updated set of what I actually care about. Reinstalling takes a while, but I find it a better solution than copying/linking directories or messing with .libPaths, because if I decide I don't really need a package and take it off my lists, the next time its otherwise-unused dependencies will also disappear, unlike if I just directly remove it.

1 Like

I tried this approach but got

copy.packages.between.libraries(from =   'C:/Users/agcur/Documents/R/win-library/3.5/', to =  "C:/Users/agcur/Documents/R/win-library/3.6/", ask = FALSE, keep_old = TRUE,
+                                 do_NOT_override_packages_in_new_R = TRUE)

No packages to copy. Goodbye :slight_smile:
[1] FALSE

Also no luck without trailing slashes. There are many packages in the from folder