# [rprojroot](https://rprojroot.r-lib.org/) This package helps accessing files relative to a *project root* to [stop the working directory insanity](https://gist.github.com/jennybc/362f52446fe1ebc4c49f). It is a low-level helper package for the [here](https://here.r-lib.org/) package. ``` r library(rprojroot) ``` ## Example The rprojroot package works best when you have a “project”: all related files contained in a subdirectory that can be categorized using a strict criterion. Let’s create a package for demonstration. ``` r dir <- tempfile() pkg <- usethis::create_package(dir) #> ✔ Creating #> /var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpVBhMLR/file337b29a46586/. #> ✔ Setting active project to #> "/private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpVBhMLR/file337b29a46586". #> ✔ Creating R/. #> ✔ Writing DESCRIPTION. #> Package: file337b29a46586 #> Title: What the Package Does (One Line, Title Case) #> Version: 0.0.0.9000 #> Date: 2025-06-27 #> Authors@R (parsed): #> * Kirill Müller [aut, cre] () #> Description: What the package does (one paragraph). #> License: MIT #> URL: https://github.com/krlmlr/rprojroot, #> https://krlmlr.github.io/rprojroot #> BugReports: https://github.com/krlmlr/rprojroot/issues #> Encoding: UTF-8 #> Roxygen: list(markdown = TRUE) #> RoxygenNote: 7.3.2.9000 #> ✔ Writing NAMESPACE. #> ✔ Setting active project to "". ``` R packages satisfy the `is_r_package` criterion. A criterion is an object that contains a `find_file()` function. With `pkg` as working directory, the function works like [`file.path()`](https://rdrr.io/r/base/file.path.html), rooted at the working directory: ``` r setwd(pkg) is_r_package #> Root criterion: contains a file "DESCRIPTION" with contents matching "^Package: " is_r_package$find_file() #> [1] "/private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpVBhMLR/file337b29a46586" is_r_package$find_file("tests", "testthat") #> [1] "/private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpVBhMLR/file337b29a46586/tests/testthat" ``` This works identically when starting from a subdirectory: ``` r setwd(file.path(pkg, "R")) is_r_package$find_file() #> [1] "/private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpVBhMLR/file337b29a46586" is_r_package$find_file("tests", "testthat") #> [1] "/private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpVBhMLR/file337b29a46586/tests/testthat" ``` There is one exception: if the first component passed to `find_file()` is already an absolute path. This allows safely applying this function to paths that may be absolute or relative: ``` r setwd(file.path(pkg, "R")) path <- is_r_package$find_file() is_r_package$find_file(path, "tests", "testthat") #> [1] "/private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/RtmpVBhMLR/file337b29a46586/tests/testthat" ``` As long as you are sure that your working directory is somewhere inside your project, you can retrieve the project root. ## Installation and further reading Install the package from CRAN: ``` r install.package("rprojroot") ``` See the [documentation](https://rprojroot.r-lib.org/articles/rprojroot.html) for more detail. # Package index ## Finding the root - [`find_root()`](https://rprojroot.r-lib.org/reference/find_root.md) [`get_root_desc()`](https://rprojroot.r-lib.org/reference/find_root.md) : Find the root of a directory hierarchy - [`find_root_file()`](https://rprojroot.r-lib.org/reference/find_root_file.md) [`find_rstudio_root_file()`](https://rprojroot.r-lib.org/reference/find_root_file.md) [`find_package_root_file()`](https://rprojroot.r-lib.org/reference/find_root_file.md) [`find_remake_root_file()`](https://rprojroot.r-lib.org/reference/find_root_file.md) [`find_testthat_root_file()`](https://rprojroot.r-lib.org/reference/find_root_file.md) : File paths relative to the root of a directory hierarchy ## Criteria - [`criteria`](https://rprojroot.r-lib.org/reference/criteria.md) [`is_rstudio_project`](https://rprojroot.r-lib.org/reference/criteria.md) [`is_vscode_project`](https://rprojroot.r-lib.org/reference/criteria.md) [`is_r_package`](https://rprojroot.r-lib.org/reference/criteria.md) [`is_remake_project`](https://rprojroot.r-lib.org/reference/criteria.md) [`is_drake_project`](https://rprojroot.r-lib.org/reference/criteria.md) [`is_targets_project`](https://rprojroot.r-lib.org/reference/criteria.md) [`is_pkgdown_project`](https://rprojroot.r-lib.org/reference/criteria.md) [`is_renv_project`](https://rprojroot.r-lib.org/reference/criteria.md) [`is_projectile_project`](https://rprojroot.r-lib.org/reference/criteria.md) [`is_quarto_project`](https://rprojroot.r-lib.org/reference/criteria.md) [`is_git_root`](https://rprojroot.r-lib.org/reference/criteria.md) [`is_svn_root`](https://rprojroot.r-lib.org/reference/criteria.md) [`is_vcs_root`](https://rprojroot.r-lib.org/reference/criteria.md) [`is_testthat`](https://rprojroot.r-lib.org/reference/criteria.md) [`from_wd`](https://rprojroot.r-lib.org/reference/criteria.md) : Prespecified criteria - [`root_criterion()`](https://rprojroot.r-lib.org/reference/root_criterion.md) [`is_root_criterion()`](https://rprojroot.r-lib.org/reference/root_criterion.md) [`as_root_criterion()`](https://rprojroot.r-lib.org/reference/root_criterion.md) [`` `|`( ``*``*`)`](https://rprojroot.r-lib.org/reference/root_criterion.md) [`has_file()`](https://rprojroot.r-lib.org/reference/root_criterion.md) [`has_dir()`](https://rprojroot.r-lib.org/reference/root_criterion.md) [`has_file_pattern()`](https://rprojroot.r-lib.org/reference/root_criterion.md) [`has_basename()`](https://rprojroot.r-lib.org/reference/root_criterion.md) : Is a directory the project root? ## Package documentation - [`rprojroot`](https://rprojroot.r-lib.org/reference/rprojroot-package.md) [`rprojroot-package`](https://rprojroot.r-lib.org/reference/rprojroot-package.md) : rprojroot: Finding Files in Project Subdirectories # Articles ### All vignettes - [Finding files in project subdirectories](https://rprojroot.r-lib.org/articles/rprojroot.md):