| 1 |
--- |
| 2 |
title: "Frequently asked questions" |
| 3 |
output: rmarkdown::html_vignette |
| 4 |
vignette: > |
| 5 |
%\VignetteIndexEntry{Frequently asked questions} |
| 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 |
## Why isn't my package being snapshotted into the lockfile? |
| 19 |
|
| 20 |
For a package to be recorded in the lockfile, it must be both: |
| 21 |
|
| 22 |
1. Installed your project library, *and* |
| 23 |
|
| 24 |
2. Used by the project, as determined by `renv::dependencies()`. |
| 25 |
|
| 26 |
This ensures that only the packages you truly require for your project will enter the lockfile; development dependencies (e.g. `devtools`) normally should not. |
| 27 |
|
| 28 |
So if you find a package is not entering the lockfile, check the output of `renv::dependencies()`. |
| 29 |
If an expected package is not listed, it's likely because `dependencies()` uses static analysis and does not understand all of the different ways in which a package might be used in a project. |
| 30 |
See the docs for more details. |
| 31 |
|
| 32 |
### Capturing all dependencies |
| 33 |
|
| 34 |
If you'd instead prefer to capture *all* packages installed into your project library (and eschew dependency discovery altogether), you can do so with: |
| 35 |
|
| 36 |
```{r} |
| 37 |
renv::settings$snapshot.type("all") |
| 38 |
``` |
| 39 |
|
| 40 |
Packages can also be explicitly ignored through a project setting, e.g. with: |
| 41 |
|
| 42 |
```{r} |
| 43 |
renv::settings$ignored.packages("<package>") |
| 44 |
``` |
| 45 |
|
| 46 |
You might also want to double-check the set of ignored packages (`renv::settings$ignored.packages()`) and confirm that you aren't unintentionally ignoring a package you actually require. |
| 47 |
|
| 48 |
See the documentation in `?snapshot` for more details. |
| 49 |
|
| 50 |
### Capturing explicit dependencies |
| 51 |
|
| 52 |
If you'd like to explicitly declare which packages your project depends on, you can do so by telling renv to form "explicit" snapshots: |
| 53 |
|
| 54 |
```{r} |
| 55 |
renv::settings$snapshot.type("explicit") |
| 56 |
``` |
| 57 |
|
| 58 |
In this mode, renv will only include packages which are explicitly listed in the project's `DESCRIPTION` file as dependencies. |
| 59 |
|
| 60 |
## How do I update the lockfile? |
| 61 |
|
| 62 |
The most important thing to remember is that `renv::snapshot()` captures the state of your project at the point in time when `renv::snapshot()` was called. |
| 63 |
In that sense, the "right" way to update the lockfile is to: |
| 64 |
|
| 65 |
1. Load the renv project, |
| 66 |
2. Make the changes you want; e.g. install packages; call `options(repos = <...>)`; ... |
| 67 |
3. Call `renv::snapshot()` to update the lockfile. |
| 68 |
|
| 69 |
That said, you are also free to modify the `renv.lock` lockfile by hand if necessary; e.g. if you want to manually add / change repositories, change the version of a package used, and so on. |
| 70 |
The `renv.lock` lockfile is a [JSON](https://www.json.org/json-en.html) file. A [JSON schema](https://json-schema.org/) is provided in the [renv repository](https://github.com/rstudio/renv/tree/main/inst/schema). |
| 71 |
|
| 72 |
The main downside to editing a package record in the lockfile directly is that you won't be able to provide a `Hash` for that package, and so renv won't be able to use its global package cache when installing that package. |
| 73 |
|
| 74 |
If you edit the lockfile by hand, you can use `renv::lockfile_validate()` to check it against renv's JSON schema: |
| 75 |
|
| 76 |
```{r} |
| 77 |
renv::lockfile_validate() |
| 78 |
``` |
| 79 |
|
| 80 |
## How should I handle development dependencies? |
| 81 |
|
| 82 |
This is related to the above question: by design, `renv.lock` normally only captures build-time or deploy-time dependencies; it may not capture the packages that you use in iterative workflows (e.g. `devtools`). |
| 83 |
However, you may want some way of still ensuring these development dependencies get installed when trying to restore a project library. |
| 84 |
|
| 85 |
For cases like these, we recommend tracking these packages in a project DESCRIPTION file; typically, within the `Suggests:` field. |
| 86 |
Then, you can execute: |
| 87 |
|
| 88 |
```{r} |
| 89 |
renv::install() |
| 90 |
``` |
| 91 |
|
| 92 |
to request that renv install the packages as described in the DESCRIPTION file. |
| 93 |
In addition, the `Remotes:` fields will be parsed and used, to ensure packages are installed from their declared remote source as appropriate. |
| 94 |
|
| 95 |
If you'd like development dependencies to be included in the lockfile by default, you can set: |
| 96 |
|
| 97 |
```{r} |
| 98 |
renv::settings$snapshot.dev(TRUE) |
| 99 |
``` |
| 100 |
|
| 101 |
When enabled, `renv::snapshot()` and `renv::status()` will include development dependencies automatically. |
| 102 |
|
| 103 |
## I'm returning to an older renv project. What do I do? |
| 104 |
|
| 105 |
Suppose you were using renv to manage an older project's dependencies. |
| 106 |
You have an older lockfile, capturing the dependencies in use when you were last working with that project. |
| 107 |
You now need to resume work on this project -- what do you do? |
| 108 |
|
| 109 |
The answer depends on how exactly you want to use the project. |
| 110 |
Do you want to treat it as a "time capsule", with dependencies frozen in time? |
| 111 |
Or are the dependencies in this project fluid, and you are primarily using renv just for isolation of project dependencies? |
| 112 |
|
| 113 |
For time capsules, use `renv::restore()` to reinstall the exact packages as declared in the project lockfile `renv.lock`. |
| 114 |
You may also need to find and install the older version of R used previously with that project, unless your intention is to upgrade R. |
| 115 |
|
| 116 |
For projects with fluid dependencies, call `renv::update()` to get the latest versions of the dependencies. |
| 117 |
Once you've verified that the code still works (or made the changes needed to get it working), call `renv::snapshot()` to record the latest versions. |
| 118 |
|
| 119 |
You can also take a more managed approach, that's somewhat in between the two extremes: |
| 120 |
|
| 121 |
1. Use `renv::restore()` to restore the project state as defined in the lockfile. |
| 122 |
|
| 123 |
2. Install and update packages deliberately with `renv::install()` and friends. |
| 124 |
|
| 125 |
3. Verify your code works, then call `renv::snapshot()` to update the new lockfile. |
| 126 |
|
| 127 |
## How can I check my packages for known vulnerabilities? |
| 128 |
|
| 129 |
If you use [Posit Package Manager](https://posit.co/products/enterprise/package-manager) as a repository, you can use `renv::vulns()` to check for known vulnerabilities in your project's dependencies: |
| 130 |
|
| 131 |
```{r} |
| 132 |
# check all packages recorded in the lockfile |
| 133 |
renv::vulns() |
| 134 |
|
| 135 |
# check specific packages |
| 136 |
renv::vulns(c("openssl==2.0.6", "curl==5.1.0")) |
| 137 |
``` |
| 138 |
|
| 139 |
Only packages with known vulnerabilities are included in the result. |
| 140 |
|
| 141 |
## Why are package downloads failing? |
| 142 |
|
| 143 |
Some issues ultimately boil down to a lack of connectivity between your machine and the R package repositories and remote sources you are trying to use. |
| 144 |
If you are working in a corporate environment, it may be worth confirming whether you have a corporate proxy in place inhibiting internet access, or whether R and renv need to be configured in a way compatible with your working environment. |
| 145 |
This is often true on Windows machines in enterprise environments, where the default "wininet" download method may work more reliably than others. |
| 146 |
Learn more in `vignette("package-install")`. |
| 147 |
|
| 148 |
In addition, note that renv places shims on the R search path that re-routes calls from `install.packages()` to `renv::install()`. |
| 149 |
If you need to bypass these shims, you can use `utils::install.packages(<...>)`; that is, with the call to `install.packages()` explicitly qualified with the package `utils::`. |
| 150 |
See `?renv::load` for more details. |