Debian Codegraph @

R/init.R

raw
1
2 the$init_running <- FALSE
3
4 #' Use renv in a project
5 #'
6 #' @description
7 #' Call `renv::init()` to start using renv in the current project. This will:
8 #'
9 #' 1. Set up project infrastructure (as described in [scaffold()]) including
10 #' the project library and the `.Rprofile` that ensures renv will be
11 #' used in all future sessions,
12 #'
13 #' 1. Discover the packages that are currently being used in your project
14 #' (via [dependencies()]), and install them into the project library
15 #' (as described in [hydrate()]),
16 #'
17 #' 1. Create a lockfile that records the state of the project library so it
18 #' can be restored by others (as described in [snapshot()]),
19 #'
20 #' 1. Restart R (if running inside RStudio).
21 #'
22 #' If you call `renv::init()` with a project that is already using renv, it will
23 #' attempt to do the right thing: it will restore the project library if it's
24 #' missing, or otherwise ask you what to do.
25 #'
26 #' # Repositories
27 #'
28 #' If the default \R repositories have not already been set, renv will use
29 #' the [Posit Public Package Manager](https://packagemanager.posit.co/) CRAN
30 #' mirror for package installation. The primary benefit to using this mirror is
31 #' that it can provide pre-built binaries for \R packages on a variety of
32 #' commonly-used Linux distributions. This behavior can be configured or
33 #' disabled if desired -- see the options in [renv::config()] for more details.
34 #'
35 #' @inherit renv-params
36 #'
37 #' @param project The project directory. When `NULL` (the default), the current
38 #' working directory will be used. The \R working directory will be
39 #' changed to match the requested project directory.
40 #'
41 #' @param settings A list of [settings] to be used with the newly-initialized
42 #' project.
43 #'
44 #' @param bare Boolean; initialize the project with an empty project library,
45 #' without attempting to discover and install \R package dependencies?
46 #'
47 #' @param force Boolean; force initialization? By default, renv will refuse
48 #' to initialize the home directory as a project, to defend against accidental
49 #' misusages of `init()`.
50 #'
51 #' @param repos The \R repositories to be used in this project.
52 #' See **Repositories** for more details.
53 #'
54 #' @param bioconductor The version of Bioconductor to be used with this project.
55 #' Setting this may be appropriate if renv is unable to determine that your
56 #' project depends on a package normally available from Bioconductor. Set this
57 #' to `TRUE` to use the default version of Bioconductor recommended by the
58 #' `BiocManager` package. When `NULL` (the default), the value is inferred
59 #' from the `bioconductor.init` configuration option -- see [config] for more
60 #' details.
61 #'
62 #' @param load Boolean; should the project be loaded after it is initialized?
63 #'
64 #' @param restart Boolean; attempt to restart the \R session after initializing
65 #' the project? A session restart will be attempted if the `"restart"` \R
66 #' option is set by the frontend hosting \R.
67 #'
68 #' @export
69 #'
70 #' @example examples/examples-init.R
71 init <- function(project = NULL,
72 ...,
73 profile = NULL,
74 settings = NULL,
75 bare = FALSE,
76 force = FALSE,
77 repos = NULL,
78 bioconductor = NULL,
79 load = TRUE,
80 restart = interactive())
81 {
82 renv_consent_check()
83 renv_scope_error_handler()
84 renv_dots_check(...)
85
86 renv_scope_binding(the, "init_running", TRUE)
87
88 project <- renv_path_normalize(project %||% getwd())
89 renv_project_lock(project = project)
90
91 # initialize profile
92 if (!is.null(profile))
93 renv_profile_set(profile)
94
95 # normalize repos
96 repos <- renv_repos_normalize(repos %||% renv_init_repos())
97 renv_scope_options(repos = repos)
98
99 # form path to lockfile, library
100 library <- renv_paths_library(project = project)
101 lockfile <- renv_lockfile_path(project)
102
103 # ask user what type of project this is
104 type <- settings$snapshot.type %||% renv_init_type(project)
105 settings$snapshot.type <- type
106
107 # initialize bioconductor pieces
108 biocver <- renv_init_bioconductor(bioconductor, project)
109 if (!is.null(biocver)) {
110
111 # validate that this version of bioconductor is appropriate
112 renv_bioconductor_validate(version = biocver)
113
114 # make sure a Bioconductor package manager is installed
115 renv_bioconductor_init(library = library)
116
117 # retrieve bioconductor repositories appropriate for this project
118 repos <- renv_bioconductor_repos(project = project, version = biocver)
119 renv_scope_options(repos = repos)
120
121 # notify user
122 writef("- Using Bioconductor version '%s'.", biocver)
123 settings[["bioconductor.version"]] <- biocver
124
125 }
126
127 # prepare and move into project directory
128 renv_init_validate_project(project, force)
129 renv_init_settings(project, settings)
130
131 # for bare inits, just activate the project
132 if (bare) {
133 renv_imbue_impl(project)
134 return(renv_init_fini(project, profile, load, restart))
135 }
136
137 # compute and cache dependencies to (a) reveal problems early and (b) compute once
138 renv_snapshot_dependencies(project, type = type, dev = TRUE)
139
140 # determine appropriate action
141 action <- renv_init_action(project, library, lockfile, bioconductor)
142 cancel_if(empty(action) || identical(action, "cancel"))
143
144 # compute library paths for this project
145 libpaths <- renv_init_libpaths(project = project)
146
147 # perform the action
148 if (action == "init") {
149 renv_scope_options(renv.config.dependency.errors = "ignored")
150 renv_imbue_impl(project, library = library)
151 hydrate(library = library, repos = repos, prompt = FALSE, report = FALSE, project = project)
152 snapshot(library = libpaths, repos = repos, prompt = FALSE, project = project, force = TRUE)
153 } else if (action == "restore") {
154 ensure_directory(library)
155 renv_sandbox_activate(project = project)
156 restore(project = project, library = libpaths, repos = repos, prompt = FALSE)
157 }
158
159 # activate the newly-hydrated project
160 renv_init_fini(project, profile, load, restart)
161
162 }
163
164 renv_init_fini <- function(project, profile, load, restart) {
165
166 renv_activate_impl(
167 project = project,
168 profile = profile,
169 version = renv_metadata_version(),
170 load = load,
171 restart = restart
172 )
173
174 invisible(project)
175
176 }
177
178 renv_init_action <- function(project, library, lockfile, bioconductor) {
179
180 # if the user has asked for bioconductor, treat this as a re-initialization
181 if (!is.null(bioconductor))
182 return("init")
183
184 # figure out appropriate action
185 case(
186
187 # if both the library and lockfile exist, ask user for intended action
188 file.exists(lockfile)
189 ~ renv_init_action_conflict_lockfile(project, library, lockfile),
190
191 # if a private library exists but no lockfile, ask whether we should use it
192 file.exists(library)
193 ~ renv_init_action_conflict_library(project, library, lockfile),
194
195 # otherwise, we just want to initialize the project
196 ~ "init"
197
198 )
199
200 }
201
202 renv_init_action_conflict_lockfile <- function(project, library, lockfile) {
203
204 if (!interactive())
205 return("nothing")
206
207 title <- "This project already has a lockfile. What would you like to do?"
208 choices <- c(
209 restore = "Restore the project from the lockfile.",
210 init = "Discard the lockfile and re-initialize the project.",
211 nothing = "Activate the project without snapshotting or installing any packages.",
212 cancel = "Abort project initialization."
213 )
214
215 selection <- tryCatch(
216 utils::select.list(choices, title = title, graphics = FALSE),
217 interrupt = identity
218 )
219
220 if (inherits(selection, "interrupt"))
221 return(NULL)
222
223 names(selection)
224
225 }
226
227 renv_init_action_conflict_library <- function(project, library, lockfile) {
228
229 if (!interactive())
230 return("nothing")
231
232 # if the project library exists, but it's empty,
233 # treat this as a request to initialize the project
234 # https://github.com/rstudio/renv/issues/1668
235 db <- installed_packages(lib.loc = library, priority = NA_character_)
236 if (nrow(db) == 0L)
237 return("init")
238
239 # if only renv is installed, but it matches the version of renv being used
240 renvonly <-
241 NROW(db) == 1L &&
242 db[["Package"]] == "renv" &&
243 db[["Version"]] == renv_package_version("renv")
244
245 if (renvonly)
246 return("init")
247
248 title <- "This project already has a private library. What would you like to do?"
249 choices <- c(
250 nothing = "Activate the project and use the existing library.",
251 init = "Re-initialize the project with a new library.",
252 cancel = "Abort project initialization."
253 )
254
255 selection <- tryCatch(
256 utils::select.list(choices, title = title, graphics = FALSE),
257 interrupt = identity
258 )
259
260 if (inherits(selection, "interrupt"))
261 return(NULL)
262
263 names(selection)
264
265 }
266
267 renv_init_validate_project <- function(project, force) {
268
269 # allow all project directories when force = TRUE
270 if (force)
271 return(TRUE)
272
273 # disallow attempts to initialize renv in the home directory
274 home <- path.expand("~/")
275 msg <- if (renv_file_same(project, home))
276 "refusing to initialize project in home directory"
277 else if (renv_path_within(home, project))
278 sprintf("refusing to initialize project in directory '%s'", project)
279
280 if (!is.null(msg)) {
281 msg <- paste(msg, "-- use renv::init(force = TRUE) to override")
282 stopf(msg)
283 }
284
285 }
286
287 renv_init_settings <- function(project, settings) {
288
289 defaults <- renv_settings_get(project)
290 merged <- renv_settings_merge(defaults, settings)
291 renv_settings_persist(project, merged)
292 invisible(merged)
293
294 }
295
296 renv_init_bioconductor <- function(bioconductor, project) {
297
298 # if we're re-initializing a project that appears to depend
299 # on Bioconductor, then use the latest Bioconductor release
300 if (is.null(bioconductor)) {
301 lockpath <- renv_paths_lockfile(project = project)
302 if (file.exists(lockpath)) {
303 lockfile <- renv_lockfile_read(lockpath)
304 bioconductor <- !is.null(lockfile[["Bioconductor"]])
305 }
306 }
307
308 # allow override via option when null
309 bioconductor <- bioconductor %||% config$bioconductor.init()
310
311 # resolve bioconductor argument
312 version <- case(
313 is.character(bioconductor) ~ bioconductor,
314 identical(bioconductor, TRUE) ~ renv_bioconductor_version(project, refresh = TRUE),
315 identical(bioconductor, FALSE) ~ NULL
316 )
317
318 # resolve symbolic version names like "devel" or "release"
319 # to their numeric equivalents for the lockfile
320 if (length(version) && version %in% c("devel", "release", "future"))
321 version <- format(renv_remotes_resolve_bioc_version(version))
322
323 version
324
325 }
326
327 renv_init_repos <- function(repos = getOption("repos")) {
328
329 # if PPM is disabled, just use default repositories
330 repos <- convert(repos, "list")
331 if (!renv_ppm_enabled())
332 return(repos)
333
334 # check whether the user has opted into using PPM by default
335 enabled <- config$ppm.default()
336 if (!enabled)
337 return(repos)
338
339 # check for default repositories
340 #
341 # note that if the user is using RStudio, we only want to override
342 # the repositories if they haven't explicitly set their own repo URL
343 #
344 # https://github.com/rstudio/renv/issues/1782
345 rstudio <- structure(
346 list(CRAN = "https://cran.rstudio.com/"),
347 RStudio = TRUE
348 )
349
350 isdefault <-
351 identical(repos, list(CRAN = "@CRAN@")) ||
352 identical(repos, rstudio)
353
354 if (isdefault) {
355 repos[["CRAN"]] <- config$ppm.url()
356 }
357
358 repos
359
360 }
361
362 renv_init_type <- function(project) {
363
364 # check if the user has already requested a snapshot type
365 type <- renv_settings_get(project, name = "snapshot.type", default = NULL)
366 if (!is.null(type))
367 return(type)
368
369 # if we don't have a DESCRIPTION file, use the default
370 if (!file.exists(file.path(project, "DESCRIPTION")))
371 return(settings$snapshot.type(project = project))
372
373 # otherwise, ask the user if they want to explicitly enumerate their
374 # R package dependencies in the DESCRIPTION file
375 choice <- menu(
376
377 title = c(
378 "This project contains a DESCRIPTION file.",
379 "Which files should renv use for dependency discovery in this project?"
380 ),
381
382 choices = c(
383 explicit = "Use only the DESCRIPTION file. (explicit mode)",
384 implicit = "Use all files in this project. (implicit mode)"
385 )
386
387 )
388
389 if (identical(choice, "cancel"))
390 cancel()
391
392 writef("- Using '%s' snapshot type. Please see `?renv::snapshot` for more details.\n", choice)
393 choice
394
395 }