Learn R Programming

this.path (version 2.2.0)

here: Construct Path to File, Starting with Script's Directory

Description

Construct the path to a file from components / / paths in a platform-DEPENDENT way, starting with sys.dir(), env.dir(), or this.dir().

Usage

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

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

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

Value

A character vector of the arguments concatenated term-by-term, starting with the executing script's directory.

Arguments

...

further arguments passed to path.join().

..

the number of directories to go back.

local

See ?sys.path().

n, envir, matchThisEnv, srcfile

See ?this.path().

Details

The path to a file starts with a base. The base is .. number of directories back from the executing script's directory (this.dir()). The argument is named .. because ".." refers to the parent directory on Windows, under Unix-alikes, and for URL pathnames.

Examples

Run this code
FILE.R <- tempfile(fileext = ".R")
this.path:::.write.code({


    this.path::here()
    this.path::here(.. = 1)
    this.path::here(.. = 2)


    ## use 'here' to read input from a file located nearby
    this.path::here(.. = 1, "input", "file1.csv")


    ## or maybe to run another script
    this.path::here("script2.R")


}, FILE.R)


source(FILE.R, echo = TRUE, verbose = FALSE)


unlink(FILE.R)

Run the code above in your browser using DataLab