| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/dependencies.R |
| 3 |
\name{dependencies} |
| 4 |
\alias{dependencies} |
| 5 |
\title{Find R package dependencies in a project} |
| 6 |
\usage{ |
| 7 |
dependencies( |
| 8 |
path = getwd(), |
| 9 |
root = NULL, |
| 10 |
..., |
| 11 |
quiet = NULL, |
| 12 |
progress = TRUE, |
| 13 |
errors = c("reported", "fatal", "ignored"), |
| 14 |
dev = FALSE |
| 15 |
) |
| 16 |
} |
| 17 |
\arguments{ |
| 18 |
\item{path}{The path to a \code{.R}, \code{.Rmd}, \code{.qmd}, \code{DESCRIPTION}, a directory |
| 19 |
containing such files, or an R function. The default uses all files |
| 20 |
found within the current working directory and its children.} |
| 21 |
|
| 22 |
\item{root}{The root directory to be used for dependency discovery. |
| 23 |
Defaults to the active project directory. You may need to set this |
| 24 |
explicitly to ensure that your project's \code{.renvignore}s (if any) are |
| 25 |
properly handled.} |
| 26 |
|
| 27 |
\item{...}{Unused arguments, reserved for future expansion. If any arguments |
| 28 |
are matched to \code{...}, renv will signal an error.} |
| 29 |
|
| 30 |
\item{quiet}{Boolean; be quiet while checking for dependencies? |
| 31 |
Setting \code{quiet = TRUE} is equivalent to setting \code{progress = FALSE} |
| 32 |
and \code{errors = "ignored"}, and overrides those options when not \code{NULL}.} |
| 33 |
|
| 34 |
\item{progress}{Boolean; report progress output while enumerating |
| 35 |
dependencies?} |
| 36 |
|
| 37 |
\item{errors}{How should errors that occur during dependency enumeration be |
| 38 |
handled? |
| 39 |
\itemize{ |
| 40 |
\item \code{"reported"} (the default): errors are reported to the user, but |
| 41 |
otherwise ignored. |
| 42 |
\item \code{"fatal"}: errors are fatal and stop execution. |
| 43 |
\item \code{"ignored"}: errors are ignored and not reported to the user. |
| 44 |
}} |
| 45 |
|
| 46 |
\item{dev}{Boolean; include development dependencies? These packages are |
| 47 |
typically required when developing the project, but not when running it |
| 48 |
(i.e. you want them installed when humans are working on the project but |
| 49 |
not when computers are deploying it). |
| 50 |
|
| 51 |
Development dependencies include packages listed in the \code{Suggests} field |
| 52 |
of a \code{DESCRIPTION} found in the project root, and roxygen2 or devtools if |
| 53 |
their use is implied by other project metadata. They also include packages |
| 54 |
used in \verb{~/.Rprofile} if \code{config$user.profile()} is \code{TRUE}.} |
| 55 |
} |
| 56 |
\value{ |
| 57 |
An \R \code{data.frame} of discovered dependencies, mapping inferred |
| 58 |
package names to the files in which they were discovered. Note that the |
| 59 |
\code{Package} field might name a package remote, rather than just a plain |
| 60 |
package name. |
| 61 |
} |
| 62 |
\description{ |
| 63 |
\code{dependencies()} will scan files within your project, looking for \R files |
| 64 |
and the packages used within those \R files. This is done primarily by |
| 65 |
parsing the code and looking for calls of the form \code{library(package)}, |
| 66 |
\code{require(package)}, \code{requireNamespace("package")}, and \code{package::method()}. |
| 67 |
renv also supports package loading with |
| 68 |
\href{https://cran.r-project.org/package=box}{box} (\code{box::use(...)}) and |
| 69 |
\href{https://cran.r-project.org/package=pacman}{pacman} (\code{pacman::p_load(...)}). |
| 70 |
|
| 71 |
For \R package projects, \code{renv} will also detect dependencies expressed |
| 72 |
in the \code{DESCRIPTION} file. For projects using Python, \R dependencies within |
| 73 |
the \R code chunks of your project's \code{.ipynb} files will also be used. |
| 74 |
|
| 75 |
Note that the \code{\link[rmarkdown:rmarkdown-package]{rmarkdown}} package is |
| 76 |
required in order to scan dependencies in R Markdown files. |
| 77 |
} |
| 78 |
\section{Missing dependencies}{ |
| 79 |
\code{dependencies()} uses static analysis to determine which packages are used |
| 80 |
by your project. This means that it inspects, but doesn't run, the \R code |
| 81 |
in your project. Static analysis generally works well, but is not |
| 82 |
100\% reliable in detecting the packages required by your project. For |
| 83 |
example, \code{renv} is unable to detect this kind of usage: |
| 84 |
|
| 85 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{for (package in c("dplyr", "ggplot2")) \{ |
| 86 |
library(package, character.only = TRUE) |
| 87 |
\} |
| 88 |
}\if{html}{\out{</div>}} |
| 89 |
|
| 90 |
It also can't generally tell if one of the packages you use, uses one of |
| 91 |
its suggested packages. For example, the \code{tidyr::separate_wider_delim()} |
| 92 |
function requires the \code{stringr} package, but \code{stringr} is only suggested, |
| 93 |
not required, by \code{tidyr}. |
| 94 |
|
| 95 |
If you find that renv's dependency discovery misses one or more packages |
| 96 |
that you actually use in your project, one escape hatch is to include a file |
| 97 |
called \verb{_dependencies.R} that includes straightforward library calls: |
| 98 |
|
| 99 |
\if{html}{\out{<div class="sourceCode">}}\preformatted{library(dplyr) |
| 100 |
library(ggplot2) |
| 101 |
library(stringr) |
| 102 |
}\if{html}{\out{</div>}} |
| 103 |
} |
| 104 |
|
| 105 |
\section{Ignoring files}{ |
| 106 |
By default, renv will read your project's \code{.gitignore}s (if present) to |
| 107 |
determine whether certain files or folders should be included when traversing |
| 108 |
directories. If preferred, you can also create a \code{.renvignore} file (with |
| 109 |
entries of the same format as a standard \code{.gitignore} file) to tell renv |
| 110 |
which files to ignore within a directory. If both \code{.renvignore} and |
| 111 |
\code{.gitignore} exist within a folder, the \code{.renvignore} will be used in lieu of |
| 112 |
the \code{.gitignore}. |
| 113 |
|
| 114 |
See \url{https://git-scm.com/docs/gitignore} for documentation on the |
| 115 |
\code{.gitignore} format. Some simple examples here: |
| 116 |
|
| 117 |
\if{html}{\out{<div class="sourceCode">}}\preformatted{# ignore all R Markdown files |
| 118 |
*.Rmd |
| 119 |
|
| 120 |
# ignore all data folders |
| 121 |
data/ |
| 122 |
|
| 123 |
# ignore only data folders from the root of the project |
| 124 |
/data/ |
| 125 |
}\if{html}{\out{</div>}} |
| 126 |
|
| 127 |
Using ignore files is important if your project contains a large number |
| 128 |
of files; for example, if you have a \verb{data/} directory containing many |
| 129 |
text files. |
| 130 |
\subsection{Profile-specific Ignore Rules}{ |
| 131 |
|
| 132 |
Profile-specific sections are also supported in \code{.renvignore} files. |
| 133 |
These sections are marked with a comment header of the form \verb{#| <code>}, |
| 134 |
where \verb{<code>} is \R code that indicates if this section of the \code{.renvignore} |
| 135 |
should apply. The \code{profile} variable is set to the same value as the current |
| 136 |
profile, or \code{"default"} if the default profile (no profile) is selected. |
| 137 |
See \code{vignette("profiles", package = "renv")} for more information on profiles. |
| 138 |
|
| 139 |
\if{html}{\out{<div class="sourceCode">}}\preformatted{# ignore all directories by default |
| 140 |
*/ |
| 141 |
|
| 142 |
#| profile == "default" |
| 143 |
!default |
| 144 |
|
| 145 |
#| profile == "extra" |
| 146 |
!extra |
| 147 |
}\if{html}{\out{</div>}} |
| 148 |
|
| 149 |
Note that the first section in a \code{.renvignore} file implicitly applies to |
| 150 |
all profiles. |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
\section{Errors}{ |
| 155 |
renv's attempts to enumerate package dependencies in your project can fail |
| 156 |
-- most commonly, because of failures when attempting to parse your \R code. |
| 157 |
You can use the \code{errors} argument to suppress these problems, but a |
| 158 |
more robust solution is tell renv not to look at the problematic code. |
| 159 |
As well as using \code{.renvignore}, as described above, you can also suppress errors |
| 160 |
discovered within individual \code{.Rmd} chunks by including \code{renv.ignore=TRUE} |
| 161 |
in the chunk header. For example: |
| 162 |
|
| 163 |
\if{html}{\out{<div class="sourceCode">}}\preformatted{```\{r chunk-label, renv.ignore=TRUE\} |
| 164 |
# code in this chunk will be ignored by renv |
| 165 |
``` |
| 166 |
}\if{html}{\out{</div>}} |
| 167 |
|
| 168 |
Similarly, if you'd like renv to parse a chunk that is otherwise ignored |
| 169 |
(e.g. because it has \code{eval=FALSE} as a chunk header), you can set: |
| 170 |
|
| 171 |
\if{html}{\out{<div class="sourceCode">}}\preformatted{```\{r chunk-label, eval=FALSE, renv.ignore=FALSE\} |
| 172 |
# code in this chunk will _not_ be ignored |
| 173 |
``` |
| 174 |
}\if{html}{\out{</div>}} |
| 175 |
} |
| 176 |
|
| 177 |
\section{Development dependencies}{ |
| 178 |
renv has some support for distinguishing between development and run-time |
| 179 |
dependencies. For example, your Shiny app might rely on |
| 180 |
\href{https://ggplot2.tidyverse.org}{ggplot2} (a run-time dependency) but while |
| 181 |
you use \href{https://usethis.r-lib.org}{usethis} during development, your app |
| 182 |
doesn't need it to run (i.e. it's only a development dependency). |
| 183 |
|
| 184 |
You can record development dependencies by listing them in the \code{Suggests} |
| 185 |
field of your project's \code{DESCRIPTION} file. Development dependencies will be installed by |
| 186 |
\code{\link[=install]{install()}} (when called without arguments) but will not be tracked in |
| 187 |
the project snapshot. If you need greater control, you can also try project |
| 188 |
profiles as discussed in \code{vignette("profiles")}. |
| 189 |
} |
| 190 |
|
| 191 |
\examples{ |
| 192 |
\dontrun{ |
| 193 |
|
| 194 |
# find R package dependencies in the current directory |
| 195 |
renv::dependencies() |
| 196 |
|
| 197 |
} |
| 198 |
} |