Created
August 21, 2017 23:45
-
-
Save duttashi/ba5aab1e62d15332b01bcd8be3d96211 to your computer and use it in GitHub Desktop.
To uninstall a R package and all its dependencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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