Title: | Finding Files in Project Subdirectories |
---|---|
Description: | Robust, reliable and flexible paths to files below a project root. The 'root' of a project is defined as a directory that matches a certain criterion, e.g., it contains a certain regular file. |
Authors: | Kirill Müller [aut, cre] |
Maintainer: | Kirill Müller <[email protected]> |
License: | MIT + file LICENSE |
Version: | 2.0.4.9010 |
Built: | 2024-12-09 03:09:54 UTC |
Source: | https://github.com/r-lib/rprojroot |
Robust, reliable and flexible paths to files below a project root. The 'root' of a project is defined as a directory that matches a certain criterion, e.g., it contains a certain regular file.
See the "Value" section in root_criterion()
for documentation
of root criterion objects, and criteria for useful predefined
root criteria.
Maintainer: Kirill Müller [email protected] (ORCID)
Useful links:
Report bugs at https://github.com/r-lib/rprojroot/issues
criteria ## Not run: is_r_package$find_file("NAMESPACE") root_fun <- is_r_package$make_fix_file() root_fun("NAMESPACE") ## End(Not run)
criteria ## Not run: is_r_package$find_file("NAMESPACE") root_fun <- is_r_package$make_fix_file() root_fun("NAMESPACE") ## End(Not run)
This is a collection of commonly used root criteria.
criteria is_rstudio_project is_r_package is_remake_project is_drake_project is_pkgdown_project is_renv_project is_projectile_project is_quarto_project is_git_root is_svn_root is_vcs_root is_testthat from_wd
criteria is_rstudio_project is_r_package is_remake_project is_drake_project is_pkgdown_project is_renv_project is_projectile_project is_quarto_project is_git_root is_svn_root is_vcs_root is_testthat from_wd
is_rstudio_project
looks for a file with extension .Rproj
.
is_r_package
looks for a DESCRIPTION
file.
is_remake_project
looks for a remake.yml
file.
is_drake_project
looks for a .drake
directory.
is_pkgdown_project
looks for a _pkgdown.yml
, _pkgdown.yaml
, pkgdown/_pkgdown.yml
and/or inst/_pkgdown.yml
file.
is_renv_project
looks for an renv.lock
file.
is_projectile_project
looks for a .projectile
file.
is_quarto_project
looks for a _quarto.yml
file.
is_git_root
looks for a .git
directory.
is_svn_root
looks for a .svn
directory.
is_vcs_root
looks for the root of a version control
system, currently only Git and SVN are supported.
is_testthat
looks for the testthat
directory, works when
developing, testing, and checking a package.
from_wd
uses the current working directory.
A root is defined as a directory that contains a regular file whose name matches a given pattern and which optionally contains a given text. The search for a root starts at a given directory (the working directory by default), and proceeds up the directory hierarchy.
get_root_desc()
returns the description of the criterion
for a root path. This is especially useful for composite root criteria
created with |.root_criterion()
.
find_root(criterion, path = ".") get_root_desc(criterion, path)
find_root(criterion, path = ".") get_root_desc(criterion, path)
criterion |
|
path |
|
Starting from the working directory, the find_root()
function searches
for the root.
If a root is found, the ...
arguments are used to construct a path;
thus, if no extra arguments are given, the root is returned.
If no root is found, an error is thrown.
The normalized path of the root as specified by the search criterion. Throws an error if no root is found
## Not run: find_root(has_file_pattern( pattern = glob2rx("DESCRIPTION"), contents = "^Package: " )) ## End(Not run)
## Not run: find_root(has_file_pattern( pattern = glob2rx("DESCRIPTION"), contents = "^Package: " )) ## End(Not run)
find_root_file()
is a wrapper around find_root()
that
appends an arbitrary number of path components to the root using
base::file.path()
.
find_root_file(..., criterion, path = ".") find_rstudio_root_file(..., path = ".") find_package_root_file(..., path = ".") find_remake_root_file(..., path = ".") find_testthat_root_file(..., path = ".")
find_root_file(..., criterion, path = ".") find_rstudio_root_file(..., path = ".") find_package_root_file(..., path = ".") find_remake_root_file(..., path = ".") find_testthat_root_file(..., path = ".")
... |
|
criterion |
|
path |
|
This function operates on the notion of relative paths.
The ...
argument is expected to contain a path relative to the root.
If the first path component passed to ...
is already an absolute path,
the criterion
and path
arguments are ignored,
and ...
is forwarded to file.path()
.
The normalized path of the root as specified by the search criteria, with the additional path components appended. Throws an error if no root is found.
find_root()
utils::glob2rx()
base::file.path()
## Not run: find_package_root_file("tests", "testthat.R") has_file("DESCRIPTION", "^Package: ")$find_file has_file("DESCRIPTION", "^Package: ")$make_fix_file(".") ## End(Not run)
## Not run: find_package_root_file("tests", "testthat.R") has_file("DESCRIPTION", "^Package: ")$find_file has_file("DESCRIPTION", "^Package: ")$make_fix_file(".") ## End(Not run)
Objects of the root_criterion
class decide if a
given directory is a project root.
root_criterion(testfun, desc, subdir = NULL) is_root_criterion(x) as_root_criterion(x) ## S3 method for class 'character' as_root_criterion(x) ## S3 method for class 'root_criterion' as_root_criterion(x) ## S3 method for class 'root_criterion' x | y has_file(filepath, contents = NULL, n = -1L, fixed = FALSE) has_dir(filepath) has_file_pattern(pattern, contents = NULL, n = -1L, fixed = FALSE) has_basename(basename, subdir = NULL)
root_criterion(testfun, desc, subdir = NULL) is_root_criterion(x) as_root_criterion(x) ## S3 method for class 'character' as_root_criterion(x) ## S3 method for class 'root_criterion' as_root_criterion(x) ## S3 method for class 'root_criterion' x | y has_file(filepath, contents = NULL, n = -1L, fixed = FALSE) has_dir(filepath) has_file_pattern(pattern, contents = NULL, n = -1L, fixed = FALSE) has_basename(basename, subdir = NULL)
testfun |
|
desc |
|
subdir |
|
x |
|
y |
|
filepath |
|
contents , fixed
|
|
n |
|
pattern |
|
basename |
|
Construct criteria using root_criterion
in a very general fashion
by specifying a function with a path
argument, and a description.
The as_root_criterion()
function accepts objects of class
root_criterion
, and character values; the latter will be
converted to criteria using has_file
.
Root criteria can be combined with the |
operator. The result is a
composite root criterion that requires either of the original criteria to
match.
The has_file()
function constructs a criterion that checks for the
existence of a specific file (which itself can be in a subdirectory of the
root) with specific contents.
The has_dir()
function constructs a criterion that checks for the
existence of a specific directory.
The has_file_pattern()
function constructs a criterion that checks for the
existence of a file that matches a pattern, with specific contents.
The has_basename()
function constructs a criterion that checks if the
base::basename()
of the root directory has a specific name,
with support for case-insensitive file systems.
An S3 object of class root_criterion
with the following members:
testfun
The testfun
argument
desc
The desc
argument
subdir
The subdir
argument
find_file
A function with ...
and path
arguments
that returns a path relative to the root,
as specified by this criterion.
The optional path
argument specifies the starting directory,
which defaults to "."
.
The function forwards to find_root_file()
,
which passes ...
directly to file.path()
if the first argument is an absolute path.
make_fix_file
A function with a path
argument that
returns a function that finds paths relative to the root. For a
criterion cr
, the result of cr$make_fix_file(".")(...)
is identical to cr$find_file(...)
. The function created by
make_fix_file()
can be saved to a variable to be more independent
of the current working directory.
root_criterion(function(path) file.exists(file.path(path, "somefile")), "has somefile") has_file("DESCRIPTION") is_r_package ## Not run: is_r_package$find_file is_r_package$make_fix_file(".") ## End(Not run)
root_criterion(function(path) file.exists(file.path(path, "somefile")), "has somefile") has_file("DESCRIPTION") is_r_package ## Not run: is_r_package$find_file is_r_package$make_fix_file(".") ## End(Not run)