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

5

22

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

Chronometry answered 3/3, 2021 at 3:43 Comment(4)
Does this answer your question? Painless way to install a new version of R?Fillip
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.Rounders
@DirkEddelbuettel thanks Dirk. Should I provide a vector of package names to update.packages() or does it figure that out?Chronometry
It takes the existing ones as given, see help(update.packages()).Rounders
C
19

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)
Cocklebur answered 3/3, 2021 at 4:6 Comment(1)
Thanks, this was quick and painless. I ended up using a lazier version of your approach, which I noted below for future referenceChronometry
A
20

Following works for me:

update.packages(ask = FALSE, checkBuilt = TRUE)
Ashkhabad answered 8/10, 2023 at 19:57 Comment(1)
This is the correct answer: this needs nothing but base R functionality. (As my earlier comment from 2021 already mentioned.)Rounders
C
19

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)
Cocklebur answered 3/3, 2021 at 4:6 Comment(1)
Thanks, this was quick and painless. I ended up using a lazier version of your approach, which I noted below for future referenceChronometry
C
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.
Chronometry answered 3/3, 2021 at 5:4 Comment(0)
B
4

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

Burro answered 3/7, 2023 at 10:12 Comment(1)
That answer is somewhere between 'irrelevant' (as R itself always had update.packages() as a function) and 'misleading'.Rounders
T
2

Luckily,

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

works if you just mv or

cd oldlibdir
tar -cf . -| (cd newdir; tar -xf -) 

from the shell and then run it from the R console in the new version (usually). The problem with having to save a vector of installed R packages from within your previous version to feed to update.packages is that automatic software package upgrades (pkg, apr, rpm) will upgrade R before you get a chance 😄

I always pkg lock R so it only gets upgraded when I explicitly invoke an R software upgrade.

Trillion answered 18/11, 2023 at 1:29 Comment(2)
Hello, I recently upgraded R 4.3.3 on debian stable via backports. Now, if for instance I open an R session and I do library(ggplot2), I do not get a warning that the library was built under the previous R version (actually I do not get any such warning for any library). Two questions then: 1) do I still need to reinstall all the user-installed packages? 2) any way to do that with the pak library instead of base R and check.Built=T? I find pak nicer and more performant. Many thanks!Imagism
R packages will often continue to work as long as the R version change isn't breaking ABI compatibility with installed libraries and they are still able to be seen and loaded from a standard library directory location. In your case it sounds like the system upgrade didn't break anything.Trillion

© 2022 - 2024 — McMap. All rights reserved.