Skip to content

Instantly share code, notes, and snippets.

@duttashi
Created August 21, 2017 23:45
Show Gist options
  • Save duttashi/ba5aab1e62d15332b01bcd8be3d96211 to your computer and use it in GitHub Desktop.
Save duttashi/ba5aab1e62d15332b01bcd8be3d96211 to your computer and use it in GitHub Desktop.
To uninstall a R package and all its dependencies
# The below code is adoped from the answer by user `Thomas` posted on StackOverflow https://stackoverflow.com/questions/26573368/uninstall-remove-r-package-with-dependencies
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())
}
}
removeDepends("some-package-name")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment