MyNixOS website logo
Description

Finding Files in Project Subdirectories.

Robust, reliable and flexible paths to files below a project root. The 'root' of a project is defined as a directory that matches a certain criterion, e.g., it contains a certain regular file.

rprojroot

Lifecycle: stable rcc CRAN_Status_Badge Codecov test coverage

This package helps accessing files relative to a project root to stop the working directory insanity. It is a low-level helper package for the here package.

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.

dir <- tempfile()
pkg <- usethis::create_package(dir)
#>  Creating '/tmp/RtmpBLE08t/file294c3c8acca7/'
#>  Setting active project to '/tmp/RtmpBLE08t/file294c3c8acca7'
#>  Creating 'R/'
#>  Writing 'DESCRIPTION'
#> Package: file294c3c8acca7
#> Title: What the Package Does (One Line, Title Case)
#> Version: 0.0.0.9000
#> Date: 2020-11-08
#> Authors@R (parsed):
#>     * Kirill Müller <[email protected]> [aut, cre] (<https://orcid.org/0000-0002-1416-3412>)
#> Description: What the package does (one paragraph).
#> License: GPL-3
#> URL: https://github.com/krlmlr/rprojroot,
#>     https://krlmlr.github.io/rprojroot
#> BugReports: https://github.com/krlmlr/rprojroot/issues
#> Encoding: UTF-8
#> LazyData: true
#> Roxygen: list(markdown = TRUE)
#> RoxygenNote: 7.1.1.9000
#>  Writing 'NAMESPACE'
#>  Setting active project to '<no active project>'

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(), rooted at the working directory:

setwd(pkg)
is_r_package
#> Root criterion: contains a file `DESCRIPTION` with contents matching `^Package: `
is_r_package$find_file()
#> [1] "/tmp/RtmpBLE08t/file294c3c8acca7"
is_r_package$find_file("tests", "testthat")
#> [1] "/tmp/RtmpBLE08t/file294c3c8acca7/tests/testthat"

This works identically when starting from a subdirectory:

setwd(file.path(pkg, "R"))
is_r_package$find_file()
#> [1] "/tmp/RtmpBLE08t/file294c3c8acca7"
is_r_package$find_file("tests", "testthat")
#> [1] "/tmp/RtmpBLE08t/file294c3c8acca7/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:

setwd(file.path(pkg, "R"))
path <- is_r_package$find_file()
is_r_package$find_file(path, "tests", "testthat")
#> [1] "/tmp/RtmpBLE08t/file294c3c8acca7/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:

install.package("rprojroot")

See the documentation for more detail.


Code of Conduct

Please note that the rprojroot project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Metadata

Version

2.0.4

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows