ECII
ECII

Reputation: 10629

Remove all packages that do not come with R

How can I remove all installed packages except base and recommended?

Upvotes: 31

Views: 42375

Answers (6)

Simon O'Hanlon
Simon O'Hanlon

Reputation: 59970

Be CAREFUL! And read the docs before you try this:

# Pasted as a commented to prevent blindly copying and pasting
# remove.packages( installed.packages( priority = "NA" )[,1] )

By default this will remove packages from the first library in your .libPaths(). Otherwise you can specify the following argument to remove.packages():

, lib = .libPaths()[1]

Upvotes: 32

CoderGuy123
CoderGuy123

Reputation: 6659

WARNING, YOU WILL DELETE A LOT OF STUFF

Sometimes uninstalling packages does not work, in which case you may want to delete the folder of the packages. This can be done from R assuming you have permissions.

sapply(paste(installed.packages( priority = "NA" )[, 2], installed.packages( priority = "NA" )[, 1], sep = "/"), unlink, recursive = T)

You can preview the paths to be delete by:

sapply(paste(installed.packages( priority = "NA" )[, 2], installed.packages( priority = "NA" )[, 1], sep = "/"), identity)

This call:

  • Gets the list of installed non-base packages
  • Makes a vector of their installed paths
  • Loops over the paths
  • Deletes each folder recursively

Upvotes: 1

CoderGuy123
CoderGuy123

Reputation: 6659

Accepted answer no longer works (R 3.6.X), but this one does:

update.packages(checkBuilt = T, ask = F)

We use checkBuilt=T because this checks whether packages were built under an older version and need to be rebuilt (sometimes).

We use ask=F because otherwise we get a prompt for each package which is annoying.

Upvotes: 10

add-semi-colons
add-semi-colons

Reputation: 18810

Here is a solution available in the R-Blogger:

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

Here is the link for the original post: https://www.r-bloggers.com/how-to-remove-all-user-installed-packages-in-r/

Upvotes: 5

giocomai
giocomai

Reputation: 3518

If on Linux, the easiest thing is probably to remove the library folder, which by default is located in /home/yourusername/R.

On Fedora, for example, it is called x86_64-redhat-linux-gnu-library. If the folder /home/yourusername/R/x86_64-redhat-linux-gnu-library is deleted, it is automatically recreated at the following start of R. All default libraries are regularly available.

Upvotes: 2

Dirk is no longer here
Dirk is no longer here

Reputation: 368261

Instead of

Updated to R 3.0.0 and have to rebuild all packages.

just do

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

which is what I did on my R 3.0.0 (using lib.loc=... to point to my different local directories). This will update everything you have and which it can still get from repos such as CRAN. For install_git() etc, you are out of luck and need to reinstall.

But either way you do not need to remove the packages first.

Upvotes: 23

Related Questions