15

Suppose I have R version 3.x.x installed, and I upgrade to version 4.x.x, is there any quick/easy way to install all the new versions of the libraries I had installed?

Please assume all the packages are on CRAN

stevec
  • 41,291
  • 27
  • 223
  • 311
  • 1
    Does this answer your question? [Painless way to install a new version of R?](https://stackoverflow.com/questions/1401904/painless-way-to-install-a-new-version-of-r) – LMc Mar 03 '21 at 04:07
  • 6
    1) The call to `update.packages()` will always do that for you. 2) In the special case of an R major version change, you can set option `checkBuilt=TRUE` which triggers a reinstallation even if the package did not change at CRAN but because you moved R versions. – Dirk Eddelbuettel Mar 03 '21 at 04:11
  • @DirkEddelbuettel thanks Dirk. Should I provide a vector of package names to `update.packages()` or does it figure that out? – stevec Mar 03 '21 at 04:16
  • 1
    It takes the existing ones as given, see `help(update.packages())`. – Dirk Eddelbuettel Mar 03 '21 at 04:24

3 Answers3

14

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)
Phil
  • 7,287
  • 3
  • 36
  • 66
  • Thanks, this was quick and painless. I ended up using a lazier version of your approach, which I noted below for future reference – stevec Mar 03 '21 at 05:05
4

Step 1

Run this in the previous R installation:

# install.packages("pacman")
library(pacman)
dput(pacman::p_lib())

Copy the output to clipboard.

Step 2

Open your new R version, paste the output from the previous step in place of ***paste output here***:

vector_of_packages <- ***paste output here***
install.packages(vector_of_packages)

Notes:

  • This is just a lazier way of doing what @Phil suggests.
stevec
  • 41,291
  • 27
  • 223
  • 311
0

The latest versions of RStudio have an Update option next to the list of packages installed under the Packages tab.

Markm0705
  • 1,340
  • 1
  • 13
  • 31