rcran

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


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


Solution

  • 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)