R: Is a directory the project root?

root_criterion {rprojroot} | R Documentation

## Is a directory the project root?

### Description

Objects of the `root_criterion` class decide if a given directory is a project root.

### Usage

    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)

    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.
`desc` | A textual description of the test criterion, of the same length as `testfun`
`subdir` | Subdirectories to start the search in, if found
`x` | An object
`y` | An object
`filepath` | File path (can contain directories)
`contents` | Regular expression to match the file contents
`n` | integer. The (maximal) number of lines to read. Negative values indicate that one should read up to the end of input on the connection.
`pattern` | Regular expression to match the file name
`dirname` | A directory name, without subdirectories

### 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.

### 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.

### Examples

    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(".")

    ## End(Not run)

[Package rprojroot version 1.3-2 Index]