Debian Codegraph @

R/remove.R

raw
1
2 #' Remove packages
3 #'
4 #' Remove (uninstall) \R packages.
5 #'
6 #' @inherit renv-params
7 #'
8 #' @param packages A character vector of \R packages to remove.
9 #' @param library The library from which packages should be removed. When
10 #' `NULL`, the active library (that is, the first entry reported in
11 #' `.libPaths()`) is used instead.
12 #'
13 #' @return A vector of package records, describing the packages (if any) which
14 #' were successfully removed.
15 #'
16 #' @export
17 #'
18 #' @example examples/examples-init.R
19 remove <- function(packages,
20 ...,
21 library = NULL,
22 project = NULL)
23 {
24 renv_scope_error_handler()
25 renv_dots_check(...)
26
27 project <- renv_project_resolve(project)
28 renv_project_lock(project = project)
29
30 library <- renv_path_normalize(library %||% renv_libpaths_active())
31
32 # NOTE: users might request that we remove packages which aren't currently
33 # installed, so we need to catch errors when trying to snapshot those packages
34 descpaths <- file.path(library, packages, "DESCRIPTION")
35 records <- lapply(descpaths, compose(catch, renv_snapshot_description))
36 names(records) <- packages
37 records <- Filter(function(record) !inherits(record, "error"), records)
38
39 if (library == renv_paths_library(project = project)) {
40 writef("- Removing package(s) from project library ...")
41 } else {
42 fmt <- "- Removing package(s) from library '%s' ..."
43 writef(fmt, renv_path_aliased(library))
44 }
45
46 if (length(packages) == 1) {
47 renv_remove_impl(packages, library)
48 return(invisible(records))
49 }
50
51 count <- 0
52 for (package in packages) {
53 if (renv_remove_impl(package, library))
54 count <- count + 1
55 }
56
57 writef("- Done! Removed %s.", nplural("package", count))
58 invisible(records)
59 }
60
61 renv_remove_impl <- function(package, library) {
62
63 path <- file.path(library, package)
64 if (!renv_file_exists(path)) {
65 writef("- Package '%s' is not installed -- nothing to do.", package)
66 return(FALSE)
67 }
68
69 recursive <- renv_file_type(path) == "directory"
70 printf("Removing package '%s' ... ", package)
71 unlink(path, recursive = recursive)
72 writef("Done!")
73
74 TRUE
75
76 }