Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

magic_path

Find a file or directory under a root directory


Description

Given a path, try to find it recursively under a root directory. The input path can be an incomplete path, e.g., it can be a base filename, and magic_path() will try to find this file under subdirectories.

Usage

magic_path(
  ...,
  root = proj_root(),
  relative = TRUE,
  error = TRUE,
  message = getOption("xfun.magic_path.message", TRUE),
  n_dirs = getOption("xfun.magic_path.n_dirs", 10000)
)

Arguments

...

A character vector of path components.

root

The root directory under which to search for the path. If NULL, the current working directory is used.

relative

Whether to return a relative path.

error

Whether to signal an error if the path is not found, or multiple paths are found.

message

Whether to emit a message when multiple paths are found and error = FALSE.

n_dirs

The number of subdirectories to recursively search. The recursive search may be time-consuming when there are a large number of subdirectories under the root directory. If you really want to search for all subdirectories, you may try n_dirs = Inf.

Value

The path found under the root directory, or an error when error = TRUE and the path is not found (or multiple paths are found).

Examples

## Not run: 
xfun::magic_path("mtcars.csv")  # find any file that has the base name mtcars.csv

## End(Not run)

xfun

Miscellaneous Functions to Support Packages Maintained by 'Yihui Xie'

v0.22
MIT + file LICENSE
Authors
Yihui Xie [aut, cre, cph] (<https://orcid.org/0000-0003-0645-5666>), Wush Wu [ctb], Daijiang Li [ctb], Xianying Tan [ctb], Salim Brüggemann [ctb] (<https://orcid.org/0000-0002-5329-5987>), Christophe Dervieux [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.