Debian Codegraph @

inst/doc/package-sources.Rmd

raw
1 ---
2 title: "Package sources"
3 output: rmarkdown::html_vignette
4 vignette: >
5 %\VignetteIndexEntry{Package sources}
6 %\VignetteEngine{knitr::rmarkdown}
7 %\VignetteEncoding{UTF-8}
8 ---
9
10 ```{r, include=FALSE}
11 knitr::opts_chunk$set(
12 collapse = TRUE,
13 comment = "#>",
14 eval = FALSE
15 )
16 ```
17
18 ```{r setup}
19 library(renv)
20 ```
21
22
23 # Package sources
24
25 renv uses an installed package's `DESCRIPTION` file to infer its source. For
26 example, packages installed from the CRAN repositories typically have the field:
27
28 ```sh
29 Repository: CRAN
30 ```
31
32 set, and renv takes this as a signal that the package was retrieved from CRAN.
33
34 ## Inferring package sources
35
36 The following fields are checked, in order, when inferring a package's source:
37
38 1. The `RemoteType` field; typically written for packages installed by the
39 devtools, remotes and pak packages,
40
41 1. The `Repository` field; for example, packages retrieved from CRAN will
42 typically have the `Repository: CRAN` field,
43
44 1. The `biocViews` field; typically present for packages installed from the
45 Bioconductor repositories,
46
47 As a fallback, if renv is unable to determine a package's source from the
48 `DESCRIPTION` file directly, but a package of the same name is available in the
49 active R repositories (as specified in `getOption("repos")`), then the package
50 will be treated as though it was installed from an R package repository.
51
52 If all of the above methods fail, renv will finally check for a package
53 available from the _cellar_. The package cellar is typically used as an escape
54 hatch, for packages which do not have a well-defined remote source, or for
55 packages which might not be remotely accessible from your machine.
56
57
58 ## Unknown sources
59
60 If renv is unable to infer a package's source, it will inform you during
61 `renv::snapshot()` -- for example, if we attempted to snapshot a package
62 called `skeleton` with no known source:
63
64 ```r
65 > renv::snapshot()
66 The following package(s) were installed from an unknown source:
67
68 skeleton
69
70 renv may be unable to restore these packages in the future.
71 Consider reinstalling these packages from a known source (e.g. CRAN).
72
73 Do you want to proceed? [y/N]:
74 ```
75
76 While you can still create a lockfile with such packages, `restore()` will
77 likely fail unless you can ensure this package is installed through some
78 other mechanism.
79
80
81 ## Custom R package repositories
82
83 Custom and local R package repositories are supported as well. The only
84 requirement is that these repositories are set as part of the `repos` R
85 option, and that these repositories are named. For example, you might use:
86
87 ```{r}
88 repos <- c(CRAN = "https://cloud.r-project.org", WORK = "https://work.example.org")
89 options(repos = repos)
90 ```
91
92 to tell renv to work with both the official CRAN package repository, as well
93 as a package repository you have hosted and set up in your work environment.
94
95 ### Repository snapshots
96
97 If you use [Posit Package Manager](https://packagemanager.posit.co/), you can
98 use `renv::checkout()` to switch your project to a specific snapshot date.
99 This installs the versions of all packages that were available on that date:
100
101 ```{r}
102 # use packages as they were on 2024-01-15
103 renv::checkout(date = "2024-01-15")
104 ```
105
106 You can also generate a lockfile from a snapshot date without installing
107 anything, by using `actions = "snapshot"`:
108
109 ```{r}
110 renv::checkout(date = "2024-01-15", actions = "snapshot")
111 ```
112
113 See `?renv::checkout` for more details.
114
115 ## Bioconductor
116
117
118 renv has been designed to work together as seamlessly as possible with
119 the [Bioconductor](https://bioconductor.org/) project. This vignette outlines
120 some of the extra steps that may be required when using renv with packages
121 obtained from Bioconductor.
122
123
124 ## Initializing a project
125
126 To initialize renv in a project using Bioconductor, you can pass the
127 `bioconductor` argument to `renv::init()`:
128
129 ```{r}
130 # use the latest-available Bioconductor release
131 renv::init(bioconductor = TRUE)
132
133 # use a specific version of Bioconductor
134 renv::init(bioconductor = "3.20")
135 ```
136
137 This will tell renv to activate the appropriate Bioconductor repositories,
138 and to use those repositories when attempting to restore packages.
139
140 ### Bioconductor releases
141
142 Bioconductor prepares different versions of its package repositories, for use
143 with different versions of R. The version of Bioconductor used within a
144 particular renv project is stored both as a project setting, and also within
145 the project lockfile. This allows you to "lock" a particular project to a
146 particular Bioconductor release, even as new Bioconductor releases are made
147 available for newer versions of R.
148
149 To set the version of Bioconductor used in a project, you can use:
150
151 ```{r}
152 renv::settings$bioconductor.version("3.20")
153 ```
154
155 If you later choose to upgrade R, you may need to upgrade the version
156 of Bioconductor used as well.
157
158 If you want to override the Bioconductor repositories used by renv, you can
159 also explicitly set the following option:
160
161 ```{r}
162 options(renv.bioconductor.repos = c(...))
163 ```
164
165
166 ## The package cellar
167
168 In some cases, your project may depend on R packages which are not available
169 from any external source, or that external source may not be accessible from the
170 machine calling `renv::restore()`. To help accommodate these scenarios, renv
171 allows you to prepare a package "cellar", to be used as an ad-hoc repository of
172 packages during restore. This allows you to provide package tarballs that can be
173 used to restore packages which cannot be retrieved from any other source.
174
175 The environment variable `RENV_PATHS_CELLAR` can be used to customize the
176 package cellar location. It should point to a directory containing package
177 binaries and sources, with a structure of the form:
178
179 - `${RENV_PATHS_CELLAR}/<package>_<version>.tar.gz`; or
180 - `${RENV_PATHS_CELLAR}/<package>/<package>_<version>.tar.gz`
181
182 Alternatively, you can also use a project-local cellar by placing your
183 packages within a folder located at `<project>/renv/cellar`. Note that this
184 folder does not exist by default; you must create it to opt-in.
185
186 - `<project>/renv/cellar/<package>_<version>.tar.gz`; or
187 - `<project>/renv/cellar/<package>/<package>_<version>.tar.gz`
188
189 As an example, if your project depended on a package `skeleton 1.0.0`, you could
190 place a tarball for this package in one of the following locations:
191
192 - `${RENV_PATHS_CELLAR}/skeleton_1.0.0.tar.gz`
193 - `${RENV_PATHS_CELLAR}/skeleton/skeleton_1.0.0.tar.gz`
194 - `<project>/renv/cellar/skeleton_1.0.0.tar.gz`; or
195 - `<project>/renv/cellar/skeleton/skeleton_1.0.0.tar.gz`
196
197 Once this is done, renv will consult these directories during future attempts
198 to restore your packages.
199
200 You can install a package from the cellar like any other package, i.e. `renv::install("skeleton")`.
201
202 During restore, if a compatible package is located within the cellar, that copy
203 of the package will be preferred even if that package might otherwise be
204 accessible from its associated remote source. For example, if `skeleton 1.0.0`
205 was also available on CRAN, `renv::restore()` would still use the tarball
206 available in the cellar rather than the version available from CRAN.
207
208 If you want to see what paths renv is using for the cellar, you can use:
209
210 ```{r}
211 renv:::renv_paths_cellar()
212 ```
213
214 See `?paths` for more details.
215
216 ### Explicit sources
217
218 You can also provide explicit source paths in the lockfile if desired. This is
219 most useful if you are building an renv lockfile "by hand", or need to tweak
220 an existing lockfile to point at a separate package for installation. For
221 example, you could have a package record in `renv.lock` of the form:
222
223 ```json
224 {
225 "Package": "skeleton",
226 "Version": "1.0.1",
227 "Source": "/mnt/r/pkg/skeleton_1.0.1.tar.gz"
228 }
229 ```
230
231 Packages should have the following extensions, depending on whether the archive
232 contains a binary copy of the package or the package sources:
233
234 | **Platform** | **Binary** | **Sources** |
235 | ------------ | ---------- | ----------- |
236 | Windows | `.zip` | `.tar.gz` |
237 | macOS | `.tgz` | `.tar.gz` |
238 | Linux | `.tar.gz` | `.tar.gz` |
239
240 Note that on Linux, both binaries and sources should have the `.tar.gz`
241 extension, but R and renv will handle this as appropriate during installation.
242
243
244 ## ABI compatibility
245
246 ABI compatibility issues can arise if different packages were built against
247 different versions of a shared dependency. For example, one package may have
248 been built against Rcpp 1.0.6, and another package might have been built against
249 Rcpp 1.0.7. However, because only one version of the Rcpp package can be loaded
250 at a time within an R session, mixing of these two packages might cause issues
251 either on load or at runtime depending on the version of Rcpp available.
252
253 It's worth emphasizing that this is not Rcpp's fault; a package built against
254 Rcpp 1.0.7 would reasonably expect newer APIs made available by that version of
255 the package would be available at runtime, and that contract would be violated
256 if an older version of Rcpp were installed in the project library. The challenge
257 for renv is that this build-time dependency is not clearly communicated to
258 renv; in general, it is not possible to know what packages (and their
259 versions) a particular package was built against.
260
261
262 ## Build-time dependencies
263
264 R packages might occasionally (and unintentionally) take a build-time
265 dependency on another R package -- for example, a package with the code:
266
267 ```{r}
268 # please don't do this!
269 `%>%` <- magrittr::`%>%`
270 ```
271
272 would take the version of `%>%` that was available from the version of magrittr
273 that was available at _build_ time, not the one available at _run_ time. This
274 could be problematic if, for example, an update to the magrittr package
275 changed in a way that made old definitions of `%>%` incompatible with newer
276 internal functions.
277
278 In general, it is a mistake for packages to take a build-time dependency on
279 exported objects from another package; rather, such objects should be imported
280 at runtime (using e.g. `importFrom()` in the package `NAMESPACE` file).