Remove all packages that do not come with R

2019-03-24 16:54发布

问题:

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

回答1:

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.



回答2:

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().



回答3:

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.



回答4:

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/



标签: r package