Quick summary
Summarize this blog with AI
Introduction
Use remove.packages("name", lib = chosen_library) to remove a package from a specific library. First inspect the selected library and the packages that depend on the target. Uninstalling files is different from detaching a package, unloading a namespace, or editing a project lockfile. The runnable demonstration below changes only a newly created temporary library.
Tested with R 4.5.1. Run the code blocks in order. Install the named packages before starting; inputs are supplied below.
Choose a library and inspect its installed packages
This copies the recommended lattice package into a disposable library. It does not install from the network or remove the original installation. The copy exists solely to demonstrate an uninstall.
pkg <- "lattice"
source_path <- find.package(pkg)
demo_lib <- tempfile("package-removal-demo-")
dir.create(demo_lib)
stopifnot(file.copy(source_path, demo_lib, recursive = TRUE))
installed <- installed.packages(lib.loc = demo_lib)
stopifnot(pkg %in% installed[, "Package"])
print(installed[, c("Package", "Version", "LibPath"), drop = FALSE])
stopifnot(normalizePath(installed[pkg, "LibPath"]) == normalizePath(demo_lib))
For your own library, inspect .libPaths() and installed.packages(), then choose the exact LibPath. The same package may exist in several libraries. Do not copy the example’s package name into a command targeting a system library.
Separate dependencies from reverse dependencies
The small database below makes the direction visible: target imports helper, and reporter imports target. Reverse dependencies identify potential consumers that removal could break.
db <- matrix(NA_character_, nrow = 3, ncol = 5,
dimnames = list(c("helper", "target", "reporter"),
c("Package", "Version", "Depends", "Imports", "LinkingTo")))
db[, "Package"] <- rownames(db); db[, "Version"] <- "1.0"
db["target", "Imports"] <- "helper"
db["reporter", "Imports"] <- "target"
forward <- tools::package_dependencies("target", db = db, recursive = TRUE)[[1]]
reverse <- tools::package_dependencies("target", db = db, recursive = TRUE,
reverse = TRUE)[[1]]
stopifnot(identical(forward, "helper"), identical(reverse, "reporter"))
print(list(required_by_target = forward, requires_target = reverse))
For an installed library, pass its installed.packages matrix as db; include all libraries used by the project when assessing consumers. This is a metadata check, not a search of every script. Do not automatically uninstall either list. A removal need not be blocked just because another package depends on the target.
Remove only the reviewed copy and verify that library
Package availability through another library is not proof that the selected copy survived. Inspect the same library after removal.
remove.packages(pkg, lib = demo_lib)
after <- installed.packages(lib.loc = demo_lib)
stopifnot(!pkg %in% after[, "Package"])
stopifnot(!dir.exists(file.path(demo_lib, pkg)))
stopifnot(dir.exists(source_path)) # original remains
stopifnot(length(find.package(pkg, lib.loc = demo_lib, quiet = TRUE)) == 0L)
unlink(demo_lib, recursive = TRUE)
cat("Temporary copy removed; original package preserved.\n")
In a real project, close or restart sessions using the package before removal. A loaded namespace can remain in memory after its files change, making requireNamespace an unsuitable sole uninstall check.
Respect permissions and project dependency records
If you lack write access, confirm that you selected the right library and contact its owner. Do not broadly change permissions or run R as administrator just to remove an unfamiliar package.
For an existing renv project, follow that project’s dependency workflow and review the lockfile change. Removing a package directory alone does not necessarily change a lockfile, and a later restore may reinstall it. Test the project after the deliberate change. Do not initialize a new dependency manager or schedule automatic updates as part of a simple uninstall.