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 character
as.root_criterion(x)
# S3 method for root_criterion
as.root_criterion(x)
# S3 method for root_criterion
|(x, y)
has_file(filepath, contents = NULL, n = -1L)
has_dir(filepath)
has_file_pattern(pattern, contents = NULL, n = -1L)
has_dirname(dirname, subdir = NULL)
A function with one parameter that returns TRUE
if the directory specified by this parameter is the project root,
and FALSE
otherwise. Can also be a list of such functions.
A textual description of the test criterion, of the same length
as testfun
Subdirectories to start the search in, if found
An object
An object
File path (can contain directories)
Regular expression to match the file contents
integer. The (maximal) number of lines to read. Negative values indicate that one should read up to the end of input on the connection.
Regular expression to match the file name
A directory name, without subdirectories
An S3 object of class root_criterion
wit the following members:
testfun
The testfun
argument
desc
The desc
argument
subdir
The subdir
argument
find_file
A function with ...
argument that returns
for a path relative to the root specified by this criterion.
The optional path
argument specifies the starting directory,
which defaults to "."
.
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.
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_dirname()
function constructs a criterion that checks if the
base::dirname()
has a specific name.
# NOT RUN {
root_criterion(function(path) file.exists(file.path(path, "somefile")), "has somefile")
has_file("DESCRIPTION")
is_r_package
is_r_package$find_file
# }
# NOT RUN {
is_r_package$make_fix_file(".")
# }
Run the code above in your browser using DataCamp Workspace