Skip to content

Instantly share code, notes, and snippets.

@sbalci
Forked from Mikuana/remove_user_packages.R
Created January 6, 2020 15:50
Show Gist options
  • Save sbalci/183eccb780d21b44b1f51a0892dcdbdc to your computer and use it in GitHub Desktop.
Save sbalci/183eccb780d21b44b1f51a0892dcdbdc to your computer and use it in GitHub Desktop.
Remove all user installed packages from R
# Shamlessly stolen from:
# https://www.r-bloggers.com/how-to-remove-all-user-installed-packages-in-r/
# create a list of all installed packages
ip <- as.data.frame(installed.packages())
head(ip)
# if you use MRO, make sure that no packages in this library will be removed
ip <- subset(ip, !grepl("MRO", ip$LibPath))
# we don't want to remove base or recommended packages either\
ip <- ip[!(ip[,"Priority"] %in% c("base", "recommended")),]
# determine the library where the packages are installed
path.lib <- unique(ip$LibPath)
# create a vector with all the names of the packages you want to remove
pkgs.to.remove <- ip[,1]
head(pkgs.to.remove)
# remove the packages
sapply(pkgs.to.remove, remove.packages, lib = path.lib)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment