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)

## Arguments

testfun 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

## Value

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.

## Details

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.

## Examples

root_criterion(function(path) file.exists(file.path(path, "somefile")), "has somefile")#> Root criterion: has somefilehas_file("DESCRIPTION")#> Root criterion: contains a file DESCRIPTIONis_r_package#> Root criterion: contains a file DESCRIPTION with contents matching ^Package: is_r_package$find_file#> function (..., path = ".") #> { #> find_root_file(..., criterion = criterion, path = path) #> } #> <environment: 0x7fe8c8175e28># NOT RUN { is_r_package$make_fix_file(".")
# }