From linux command-line remove all existing R packages:
sudo apt-get update
sudo apt-get remove r-base r-base-dev r-base-core r-base-html r-cran-boot r-cran-class r-cran-cluster r-cran-codetools r-cran-foreign r-cran-kernsmooth r-cran-lattice r-cran-mass r-cran-matrix r-cran-mgcv r-cran-nlme r-cran-nnet r-cran-rpart r-cran-spatial r-cran-survival r-doc-html r-recommended
Next, update /etc/apt/sources.list and add these lines (comment out old R version, add new pkg list):
# deb [arch=i386,amd64] https://cran.rstudio.com/bin/linux/ubuntu xenial/
# deb-src [arch=i386,amd64] https://cran.rstudio.com/bin/linux/ubuntu xenial/
# new version of R - 3.5.x
deb https://cloud.r-project.org/bin/linux/ubuntu xenial-cran35/
Then, back at the command-line, refresh the local apt cache and install R:
sudo apt-get update
sudo apt-get install r-base r-base-dev r-doc-info
Next, cleanup and left-over but no longer needed packages:
sudo apt-get autoremove
Start R as root with sudo R
and then run the following:
update.packages(ask=FALSE, checkBuilt = TRUE)
The above works as the system .libPaths() on linux do not have the R version in them.
Replace <username>
below with your actual username
lib_loc <- "/home/<username>/R/x86_64-pc-linux-gnu-library/3.4"
to_install <- unname(installed.packages(lib.loc = lib_loc)[, "Package"])
to_install
install.packages(pkgs = to_install)
If you have a few packages that you are not sure are already installed, and don't want to go through installing if already present, use these lines, replacing list.of.packages with your own list.
list.of.packages <- c("pkg1", "pkg2")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)