Debian Codegraph @

R/rebuild.R

raw
1
2 #' Rebuild the packages in your project library
3 #'
4 #' Rebuild and reinstall packages in your library. This can be useful as a
5 #' diagnostic tool -- for example, if you find that one or more of your
6 #' packages fail to load, and you want to ensure that you are starting from a
7 #' clean slate.
8 #'
9 #' @inherit renv-params
10 #'
11 #' @param packages The package(s) to be rebuilt. When `NULL`, all packages
12 #' in the library will be reinstalled.
13 #'
14 #' @param recursive Boolean; should dependencies of packages be rebuilt
15 #' recursively? Defaults to `TRUE`.
16 #'
17 #' @return A named list of package records which were installed by renv.
18 #'
19 #' @export
20 #'
21 #' @examples
22 #' \dontrun{
23 #'
24 #' # rebuild the 'dplyr' package + all of its dependencies
25 #' renv::rebuild("dplyr", recursive = TRUE)
26 #'
27 #' # rebuild only 'dplyr'
28 #' renv::rebuild("dplyr", recursive = FALSE)
29 #'
30 #' }
31 rebuild <- function(packages = NULL,
32 recursive = TRUE,
33 ...,
34 type = NULL,
35 prompt = interactive(),
36 library = NULL,
37 project = NULL)
38 {
39 renv_consent_check()
40 renv_scope_error_handler()
41 renv_dots_check(...)
42
43 project <- renv_project_resolve(project)
44 renv_project_lock(project = project)
45 renv_scope_verbose_if(prompt)
46
47 libpaths <- renv_libpaths_resolve(library)
48 library <- nth(libpaths, 1L)
49
50 # get collection of packages currently installed
51 records <- renv_snapshot_libpaths(libpaths = libpaths, project = project)
52 packages <- setdiff(packages %||% names(records), "renv")
53
54 # add in missing packages
55 for (package in packages) {
56 records[[package]] <- records[[package]] %||%
57 renv_available_packages_latest(package)
58 }
59
60 # make sure records are named
61 names(records) <- map_chr(records, `[[`, "Package")
62 if (empty(records)) {
63 writef("- There are no packages currently installed -- nothing to rebuild.")
64 return(invisible(records))
65 }
66
67
68 # apply any overrides
69 records <- renv_records_override(records)
70
71 # notify the user
72 preamble <- if (recursive)
73 "The following package(s) and their dependencies will be reinstalled:"
74 else
75 "The following package(s) will be reinstalled:"
76
77 renv_pretty_print_records(preamble, records[packages])
78 cancel_if(prompt && !proceed())
79
80 # figure out rebuild parameter
81 rebuild <- if (recursive) NA else packages
82
83 # perform the install
84 install(
85 packages = records[packages],
86 library = libpaths,
87 type = type,
88 rebuild = rebuild,
89 prompt = FALSE,
90 project = project
91 )
92 }