hereR Documentation

Construct Path to File, Starting With Script's Directory

Description

here() constructs file paths starting with this.dir().

this.proj() constructs file paths starting with the project root of this.dir().

reset.proj() resets the path cache of this.proj(). This can be useful if you create a new project that you would like to be detected without restarting your R session.

Usage

here(..., local = FALSE, n = 0,
     envir = parent.frame(n + 1),
     matchThisEnv = getOption("topLevelEnvironment"),
     srcfile = if (n) sys.parent(n) else 0, .. = 0)

this.proj(..., local = FALSE, n = 0,
          envir = parent.frame(n + 1),
          matchThisEnv = getOption("topLevelEnvironment"),
          srcfile = if (n) sys.parent(n) else 0)

reset.proj()

## alias for 'here'
ici(..., local = FALSE, n = 0,
    envir = parent.frame(n + 1),
    matchThisEnv = getOption("topLevelEnvironment"),
    srcfile = if (n) sys.parent(n) else 0, .. = 0)

Arguments

...

further arguments passed to path.join().

local, n, envir, matchThisEnv, srcfile

See ?this.path().

..

the number of directories to go back.

Details

For this.proj(), the project root has the same criterion as here::here(), but unlike here::here(), this.proj() supports sub-projects and multiple projects in use at once. Additionally, this.proj() is independent of working directory, whereas here::here() relies on the working directory being set somewhere within the project when package:here is loaded. Arguably, this makes it better than here::here().

Value

A character vector of the arguments concatenated term-by-term.

Examples

tmpdir <- tempfile(pattern = "dir")
dir.create(tmpdir)

writeLines("this file signifies that its directory is the project root",
    this.path::path.join(tmpdir, ".here"))

FILE.R <- this.path::path.join(tmpdir, "src", "R", "script1.R")
dir.create(this.path::dirname2(FILE.R), recursive = TRUE)
this.path:::.write.code({
    this.path::this.path()
    this.path::this.proj()
    ## use 'here' to run another script located nearby
    this.path::here("script2.R")
    ## or maybe to read input from a file
    this.path::here(.. = 2, "input", "data1.csv")
    ## but sometimes it is easier to use the project root
    ## this allows you to move the R script up or down
    ## a directory without changing the .. number
    this.path::this.proj("input", "data1.csv")
}, FILE.R)

source(FILE.R, echo = TRUE)

unlink(tmpdir, recursive = TRUE)