Reinstalling all R pkgs

When installing a new version of R you may want to reinstall al previously installed packages for the new R version. New packages will be installed a new directory.

Andrea Spano andreaspano.github.io
2021-11-18

Assuming that ol packages have been installed in

~/R/x86_64-pc-linux-gnu-library/4.0/

and that you want to install new packages in

~/R/x86_64-pc-linux-gnu-library/4.1/

Define installation directories

ld_loc <- "~/R/x86_64-pc-linux-gnu-library/4.0/"
new_loc <- "~/R/x86_64-pc-linux-gnu-library/4.1/"

If new path does not exists, create it

if ( !dir.exists(new_loc)) {dir.create(new_loc)}

Grab all old installed packages

pkgs <- installed.packages(lib.loc = old_loc)[, "Package"]

Install new packages in the location

install.packages(pkgs, 
                 lib = new_loc, 
                 ask = FALSE , 
                 repos =   'https://cran.wu.ac.at/',
                 dependencies = TRUE, 
                 Ncpus = 8)

Note that not all packages may install correctly but, the vast majority most of them will.

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Citation

For attribution, please cite this work as

Spano (2021, Nov. 18). andreaspano blog: Reinstalling all R pkgs. Retrieved from https://andreaspano.github.io/posts/2021-11-18-reinstalling-all-r-pkgs/

BibTeX citation

@misc{spano2021reinstalling,
  author = {Spano, Andrea},
  title = {andreaspano blog: Reinstalling all R pkgs},
  url = {https://andreaspano.github.io/posts/2021-11-18-reinstalling-all-r-pkgs/},
  year = {2021}
}