Debian Codegraph @

inst/doc/packages.Rmd

raw
1 ---
2 title: "Package development"
3 output: rmarkdown::html_vignette
4 vignette: >
5 %\VignetteIndexEntry{Package development}
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 ## Development
20
21 Often, R packages will have other R packages as dependencies. For this, one must
22 declare their R package dependencies within the package `DESCRIPTION` file. If
23 you want to prepare your environment for package development, you can use:
24
25 ```{r}
26 renv::install()
27 ```
28
29 to install the packages as declared in the package's `DESCRIPTION` file. This
30 action is roughly analogous to `remotes::install_deps()`.
31
32 If you're developing a package that you intend to release to CRAN, then you
33 likely want to build and test your package against the latest versions of your
34 dependencies as available on CRAN. For this, you should consider using:
35
36 ```{r}
37 renv::update()
38 ```
39
40 to ensure your package dependencies are up-to-date, as appropriate.
41
42
43 ## Isolation
44
45 Normally, a package under development should be tested against the
46 latest-available versions of its dependencies on CRAN. However, in some cases,
47 you may need to ensure your package is compatible with other packages also
48 currently under development.
49
50 In these cases, the renv project library can be useful -- you can install the
51 development version(s) of your dependencies into the project library, without
52 worrying about clobbering any packages already installed in your system library.
53
54 In these cases, you can declare your development dependencies using the
55 `Remotes` field of the `DESCRIPTION` file; e.g.
56
57 ```sh
58 Remotes:
59 r-lib/ggplot2
60 ```
61
62 and `renv::install()` will parse that remotes declaration and retrieve the
63 requested package. See the remotes vignette,
64 [Dependency resolution for R package development][dependencies],
65 for more details.
66
67
68 ## Library Paths
69
70 For package projects using renv, a library path outside of the project
71 directory will be used instead. As an example, on macOS, this might look like:
72
73 ```r
74 > .libPaths()
75 [1] "/Users/kevin/Library/Caches/org.R-project.R/R/renv/library/example-552f6e80/R-4.3/aarch64-apple-darwin20"
76 [2] "/Users/kevin/Library/Caches/org.R-project.R/R/renv/sandbox/R-4.3/aarch64-apple-darwin20/ac5c2659"
77 ```
78
79 This is done to avoid issues with `R CMD build`, which can become very slow if
80 your project contains a large number of files -- as can happen if the project
81 library is located in `renv/library` (the typical location). Note that even
82 though the library is located outside of the project, the library path
83 generated will still be unique to that project, and so the project is still
84 effectively isolated in the same way as other renv projects normally are.
85
86 If you want to customize the location where `renv` places project libraries in
87 this scenario, you can use the `RENV_PATHS_LIBRARY_ROOT` environment variable.
88 For example:
89
90 ```sh
91 RENV_PATHS_LIBRARY_ROOT = ~/.renv/library
92 ```
93
94 If you'd still prefer to keep your project library within the project directory,
95 you can set:
96
97 ```sh
98 RENV_PATHS_LIBRARY = renv/library
99 ```
100
101 within an appropriate `.Renviron` start-up profile -- but please be aware of
102 the caveats to doing this, as the performance of `R CMD build` will be affected.
103
104
105 ## Testing
106
107 While developing your package, you may want to use a continuous integration
108 service (such as [Travis CI](https://www.travis-ci.com)) to build and test
109 your package remotely. You can use renv to help facilitate this testing --
110 see the [Continuous Integration](ci.html) vignette for more information. In
111 particular, clever use of the renv cache can help save time that might
112 normally be spent on package installation. See
113 <https://github.com/rstudio/renv/blob/main/.github/workflows/R-CMD-check.yaml>
114 for an example of how renv uses itself for package management in its own
115 CI tests.
116
117
118 ## Submitting to CRAN
119
120 Note that packages submitted to CRAN should be designed to work with the other R
121 packages currently available on CRAN. For that reason, when preparing your package
122 for submission, you'll need to ensure your source package tarball does not
123 include any `renv` infrastructure. `renv` makes this easy by automatically
124 including
125
126 ```sh
127 ^renv$
128 ^renv\.lock$
129 ```
130
131 in your package's `.Rbuildignore` file. This instructs `R CMD build` to not
132 include these files and folders in the generated package tarball. Through this,
133 even if `renv` is used during package development, it's still easy to build and
134 publish your package to CRAN as you would when developing packages without `renv`.
135
136
137 [dependencies]: https://remotes.r-lib.org/articles/dependencies.html