Debian Codegraph @

vignettes/profiles.Rmd

raw
1 ---
2 title: "Project profiles"
3 output: rmarkdown::html_vignette
4 vignette: >
5 %\VignetteIndexEntry{Project profiles}
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
19 ## Introduction
20
21 It is possible to activate and switch between different profiles associated
22 with a project. A profile can be thought of as a different mode in which a
23 project is used. For example:
24
25 - A "development" profile might be used when developing and testing a project,
26 - A "production" profile might be used for production deployments,
27 - A "shiny" profile might be used when running the Shiny application.
28
29 At its heart, activating or using a particular profile implies using a different
30 set of paths for the project library and lockfile. With this, it is possible
31 to associate different packages, and different dependencies, with different
32 workflows in a single project using renv.
33
34
35 ## Usage
36
37 By default, renv projects use the "default" profile, which implies that
38 library and lockfile paths are set in the typical way. To activate a particular
39 profile, use:
40
41 ```{r}
42 renv::activate(profile = "dev")
43 ```
44
45 This creates a profile called `"dev"`, and sets it as the default for the
46 project, so that newly-launched R sessions will operate using the `"dev"`
47 profile. After setting this and re-launching R, you should see that the
48 project library and lockfile paths are resolved within the `renv/profiles/dev`
49 folder from the project root.
50
51 Alternatively, if you want to activate a particular profile for an R session
52 without setting it as the default for new R sessions, you can use:
53
54 ```{r}
55 Sys.setenv(RENV_PROFILE = "dev")
56 ```
57
58 and renv will automatically use that profile as appropriate when computing
59 library and lockfile paths. Similarly, from the command line, you might
60 enforce the use of a particular profile in an renv project with:
61
62 ```{sh}
63 export RENV_PROFILE=dev
64 ```
65
66 With that set, renv would default to using the `"dev"` profile for any
67 newly-launched R sessions within renv projects.
68
69 To activate the "default" profile used by a project, use:
70
71 ```{r}
72 renv::activate(profile = "default")
73 ```
74
75
76 ## Managing profile-specific dependencies
77
78 Profile-specific package dependencies can be declared within the project's
79 top-level `DESCRIPTION` file. For example, to declare that the shiny
80 profile depends on the [shiny] and [tidyverse] packages:
81
82 ```{sh}
83 Config/renv/profiles/shiny/dependencies: shiny, tidyverse
84 ```
85
86 If you'd like to also declare that these packages should be installed from
87 a custom remote (analogous to the `Remotes` field for the default profile),
88 you can define those remotes with a separate field:
89
90 ```{sh}
91 Config/renv/profiles/shiny/remotes: rstudio/shiny, tidyverse/tidyverse
92 ```
93
94 These remotes will then be respected in calls to `renv::install()`.
95
96 The packages and remotes must be specified separately, as renv cannot
97 determine the package name associated with a particular remote without
98 explicitly resolving that remote. Remote resolution normally requires a web
99 request, which renv tries to avoid in "regular" dependency discovery.
100
101 If you'd prefer that only the packages enumerated in this field are used, you
102 can opt-in to using `"explicit"` snapshots, and leave the `Imports`, `Depends`
103 and `Suggests` fields blank:
104
105 ```{r}
106 renv::settings$snapshot.type("explicit")
107 ```
108
109 When set, only the dependencies listed in the project `DESCRIPTION` file will
110 be used when the lockfile is generated. See `?renv::snapshot` for more details.
111
112
113 ## Profile-specific ignore rules
114
115 `.renvignore` files support profile-specific sections using comments of the
116 form `#| <expr>`, where `<expr>` is an R expression evaluated to determine
117 whether the subsequent ignore rules should apply. This is useful when
118 different profiles should scan different parts of the project for dependencies.
119
120 For example, to ignore the `shiny/` directory unless the "shiny" profile
121 is active:
122
123 ```
124 # rules in this section apply to all profiles
125 data/*.csv
126
127 #| profile == "shiny"
128 # only ignore these when the shiny profile is active
129 scripts/batch-*.R
130 ```
131
132 The first section (before any `#|` comment) applies to all profiles. If you
133 want to restrict it to only the default profile, add `#| profile == "default"`
134 as the first line.
135
136
137 [shiny]: https://shiny.posit.co/
138 [tidyverse]: https://tidyverse.org/