40

I wanted to try some new package. I installed it, it required a lot of dependencies, so it installed plenty of other packages. I tried it and I am not impressed - now I would like to uninstall that package including all the dependencies!

Is there any way to remove given packages including all dependencies which are not needed by any other package in the system?

I looked at ?remove.packages but there is no option to do this.

1
  • 4
    You could code up something based on tools::dependsOnPkgs.
    – hrbrmstr
    Oct 26, 2014 at 13:23

4 Answers 4

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

27
+100

Here is some code that will all you to remove a package and its unneeded dependencies. Note that its interpretation of "unneeded" dependent packages is the set of packages that this package depends on but that are not used in any other package. This means that it will also default to suggesting to uninstall packages that have no reverse dependencies. Thus I've implemented it as an interactive menu (like in update.packages) to give you control over what to uninstall.

library("tools")

removeDepends <- function(pkg, recursive = FALSE){
    d <- package_dependencies(,installed.packages(), recursive = recursive)
    depends <- if(!is.null(d[[pkg]])) d[[pkg]] else character()
    needed <- unique(unlist(d[!names(d) %in% c(pkg,depends)]))
    toRemove <- depends[!depends %in% needed]
    if(length(toRemove)){
         toRemove <- select.list(c(pkg,sort(toRemove)), multiple = TRUE,
                                 title = "Select packages to remove")
         remove.packages(toRemove)
         return(toRemove)
    } else {
        invisible(character())
    }
}

# Example
install.packages("YplantQMC") # installs an unneeded dependency "LeafAngle"
c("YplantQMC","LeafAngle") %in% installed.packages()[,1]
## [1] TRUE TRUE
removeDepends("YplantQMC")
c("YplantQMC","LeafAngle")  %in% installed.packages()[,1]
## [1] FALSE FALSE

Note: The recursive option may be particularly useful. If package dependencies further depend on other unneeded packages, setting recursive = TRUE is vital. If dependencies are shallow (i.e., only one level down the dependency tree), this can be left as FALSE (the default).

12

There is in fact a function remove.packages() in base R, but it's in the package utils, which you need to load first:

library(utils)
remove.packages()

It's not entirely clear to me how much recursive cleanup this function does.

3
  • 1
    but this doesn't do what the OP wants, which is to remove the package and all of its dependencies that are not required by any other installed packages ...
    – Ben Bolker
    Oct 26, 2014 at 17:19
  • @BenBolker Yes, I suspect you're right. I may have slightly misread the question - I had to go and do some hunting to find remove.packages() since it's not usually loaded, and thus visible.
    – Andrie
    Oct 26, 2014 at 17:22
  • I've edited OPs final words to make it clear what I think they meant.
    – Spacedman
    Oct 29, 2014 at 15:49
6

There are base R ways to handle this but I'm going to recommend a package (I know you're trying to get rid of these). I'm recommending this package for 2 reasons (1) it solves two problems you're having & (2) Dason K. and I are developing this package (full disclosure). This package's value stands in that the functions are easier to remember names that are consistent. It also does some combined operations. Note you could do all of this in base but this question is already pretty localized so thus I'm going to use a tool that makes answering easier.

This package will:

  1. allow you to delete package and dependencies
  2. allow you to install packages in a temporary directory rather than main library

The caveat is that you can't be 100% certain that the package dependency wasn't already there, installed by the user previously. Therefore I would take caution with every step of this solution that you're not deleting things that are of importance. This solution relies on 2 factors (1) pacman (2) file.info. We'll assume that dependencies that were modified within a certain (user defined) threshold of time are indeed unwanted packages. Note the word assume here.

I made this reproducible for the folks at home in that the answer will randomly install a package from CRAN with additional dependencies (this installs a package you do not already have locally with 3 or more dependencies; used random to not single out any package).

Making a reproducible example

library(pacman)

(available <- p_cran())
(randoms <- setdiff(available,  p_lib()))
(mypackages <- p_lib())

ndeps <- 1
while(ndeps < 3) {

    package <- sample(randoms, 1)
    deps <- unlist(p_depends(package, character.only=TRUE), use.names=FALSE)
    ndeps <- length(setdiff(deps,  mypackages))

}  

package
p_install(package, character.only = TRUE)

Uninstalling package

We will assign the package name from the first part to package or the OP can use the unwanted package they installed and assign that to package (my random package happened to be package <- "OrdinalLogisticBiplot"). This deletions process should, ideally, be done in a clean R session with no add-on packages (except pacman) loaded.

## function to grab file info date/time modified
infograb <- function(x) file.info(file.path(p_path(), x))[["mtime"]]

## determine the differences in times modified for "package" 
## and all other packages in library
diffs <- as.numeric(infograb(package)) -  sapply(p_lib(), infograb)

## user defined threshold
threshold <- 15

## determine packages just installed within the time frame of the unwanted package 
(delete_deps <- diffs[diffs < threshold & diffs >= 0])

## recursively find all packages that could have been installed 
potential_depends <- unlist(lapply(unlist(p_depends(package, character=TRUE)), 
    p_depends, character=TRUE, recursive=TRUE))

## delete packages that are both on the lists of (1) installed within time
## frame of unwanted package and a dependency of that package
p_delete(intersect(names(delete_deps), potential_depends), character.only = TRUE)

This approach makes some big assumptions.

A better approach from the get go

p_temp(package_to_try)

This allows you to try it out first and not have it muddy your local library.

If you're unimpressed with pacman you can use the method described above to delete it.

7
  • Hi Tyler! From you answer I did not recognize how is p_delete different from remove.packages. It seems that p_delete requires the dependency to be listed there as well, right? So then what is the added value of the pacman package?
    – Tomas
    Oct 26, 2014 at 13:42
  • I can see how to use pacman to auto-delete packages, but a full and foolproof solution would be a bit complex. (1) use p_depends; combine the elements of the list (depends, imports, suggests). (Ideally you would actually want a recursive enumeration of dependencies, as in tools::pkgDepends) (2) remove base/recommended packages from the list; optionally remove packages that are also required by any other installed packages (!). (3) use p_delete to delete.
    – Ben Bolker
    Oct 26, 2014 at 13:50
  • @Ben, but this could be done without pacman as well, right? Where is the added value of pacman? I think this answer is a bit off.
    – Tomas
    Oct 26, 2014 at 13:57
  • I think you may be right. I suspect that pacman provides a more satisfying wrapper around the base R tools, but not an already-coded tool for what you want.
    – Ben Bolker
    Oct 26, 2014 at 14:05
  • 1
    @TMS Sure it could be done without pacman. pacman is just a bunch of wrappers after all. With that said Tyler (and I) have been developing pacman and using it for some time so when questions about package management come up Tyler will most likely give an answer that utilizes pacman since it's familiar and makes some tasks easier.
    – Dason
    Oct 26, 2014 at 20:25
0

Here is a quick solution to have a look at the packages that are not required by any other locally installed packages.

library(pacman)
ip <- installed.packages()[,1]
deps <- lapply(1:length(ip), function(i) tryCatch(p_depends(ip[i], local = TRUE)$Imports, error = function(e) return(NULL)))
packages.on.which.things.depend <- sort(unique(unlist(deps)))
packages.on.which.nothing.depends <- setdiff(ip, packages.on.which.things.depend)
packages.on.which.nothing.depends

Then, have a quick look at it and remove the ones you are not using (just be careful and do not try to uninstall base).

After you have determined which ones you use and which you don’t use, you may proceed with something like

i.need <- c("AER", "car", "devtools", "glmnet", "gmm", "Hmisc", "pacman", "plm", "RcppArmadillo", "RcppEigen", "rmarkdown", "rugarch", "base", "datasets")
un <- setdiff(packages.on.which.nothing.depends, i.need)
un
remove.packages(un)

Rinse and repeat until there are no unneeded orphanes packages. R should not allow you to remove built-in system packages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.