proj_root: Return the (possible) root directory of a project

View source: R/paths.R

proj_rootR Documentation

Return the (possible) root directory of a project

Description

Given a path of a file (or dir) in a potential project (e.g., an R package or an RStudio project), return the path to the project root directory.

Usage

proj_root(path = "./", rules = root_rules)

root_rules

Arguments

path

The initial path to start the search. If it is a file path, its parent directory will be used.

rules

A matrix of character strings of two columns: the first column contains regular expressions to look for filenames that match the patterns, and the second column contains regular expressions to match the content of the matched files. The regular expression can be an empty string, meaning that it will match anything.

Format

An object of class matrix (inherits from array) with 2 rows and 2 columns.

Details

The search for the root directory is performed by a series of tests, currently including looking for a ‘DESCRIPTION’ file that contains ⁠Package: *⁠ (which usually indicates an R package), and a ‘*.Rproj’ file that contains ⁠Version: *⁠ (which usually indicates an RStudio project). If files with the expected patterns are not found in the initial directory, the search will be performed recursively in upper-level directories.

Value

Path to the root directory if found, otherwise NULL.

Note

This function was inspired by the rprojroot package, but is much less sophisticated. It is a rather simple function designed to be used in some of packages that I maintain, and may not meet the need of general users until this note is removed in the future (which should be unlikely). If you are sure that you are working on the types of projects mentioned in the ‘Details’ section, this function may be helpful to you, otherwise please consider using rprojroot instead.


xfun documentation built on Nov. 2, 2023, 6 p.m.