Learn R Programming

this.path (version 0.5.1)

this.path: Determine Executing Script's Filename

Description

this.path() returns the normalized path of the executing script.

this.dir() is a shorter way of writing dirname(this.path()), returning the normalized path of the directory in which the executing script is located.

Usage

this.path(verbose = getOption("verbose"))
this.dir(...)

Arguments

verbose

TRUE or FALSE; should the method in which the path of the executing script was determined be printed?

arguments passed to this.path.

Value

character string; the executing script's filename.

Details

There are three ways in which R code is typically run; in ‘RStudio’ or ‘Rgui’ by running the current line or selection with the Run button (or appropriate keyboard shortcut), through a source call (a call to function source, sys.source, debugSource in ‘RStudio’, or testthat::source_file), and from a shell (including the Windows command-line / / Unix terminal).

To retrieve the executing script's filename, first an attempt is made to find a source call. The calls are searched in reverse order so as to grab the most recent source call in the case of nested source calls. If a source call was found, the argument file (fileName in the case of debugSource, path in the case of testthat::source_file) is returned from the function's evaluation environment. If you have your own source-like function that you'd like to be recognized by this.path, please contact this.path so it can be implemented.

If no source call is found up the calling stack, then an attempt is made to figure out how R is currently being used.

If R is being run from a shell, the shell arguments are searched for -f FILE or --file=FILE (the two methods of taking input from FILE). If exactly one of either type of argument is supplied, the text FILE is returned. It is an error to use this.path when none or multiple arguments of either type are supplied.

If R is being run from a shell under Unix-alikes with -g Tk or --gui=Tk, this.path() will signal an error. This is because ‘Tk’ does not make use of its -f FILE, --file=FILE argument.

If R is being run from ‘RStudio’, the active document's filename (the document in which the cursor is active) is returned (at the time of evaluation). If the active document is the R console, the source document's filename (the document open in the current tab) is returned (at the time of evaluation). Please note that the source document will NEVER be a document open in another window (with the Show in new window button). It is important to not leave the current tab (either by closing or switching tabs) while any calls to this.path have yet to be evaluated in the run selection. It is an error for no documents to be open or for a document to not exist (not saved anywhere).

If R is being run from ‘Rgui’, the source document's filename (the document most recently interacted with besides the R Console) is returned (at the time of evaluation). Please note that minimized documents will be IGNORED. It is important to not leave the current document (either by closing the document or interacting with another document) while any calls to this.path have yet to be evaluated in the run selection. It is an error for no documents to be open or for a document to not exist (not saved anywhere).

If R is being run from ‘AQUA’, the executing script's path cannot be determined. Unlike ‘RStudio’ and ‘Rgui’, there is currently no way to request the path of an open document. Until such a time that there is a method for requesting the path of an open document, consider using ‘RStudio’.

If R is being run in another manner, it is an error to use this.path.

If your GUI of choice is not implemented with this.path, please contact this.path so it can be implemented.

See Also

here

this.path-package

source, sys.source, debugSource, testthat::source_file

R.from.shell

Examples

Run this code
# NOT RUN {
this.path:::write.code(file = FILE <- tempfile(), {

    withAutoprint({


        cat(sQuote(this.path::this.path(verbose = TRUE)), "\n\n")


    }, verbose = FALSE)

})


source(FILE, verbose = FALSE)
sys.source(FILE, envir = environment())
if (.Platform$GUI == "RStudio")
    get("debugSource", "tools:rstudio", inherits = FALSE)(FILE)
if (requireNamespace("testthat"))
    testthat::source_file(FILE, chdir = FALSE, wrap = FALSE)


this.path:::.Rscript(c("--default-packages=NULL", "--vanilla", FILE))
# }
# NOT RUN {
<!-- % essentials::Rscript(c("--default-packages=NULL", "--vanilla"), file = FILE) -->
# }
# NOT RUN {

# this.path also works when source-ing a URL
# (included tryCatch in case an internet connection is not available)
tryCatch({
    source("https://raw.githubusercontent.com/ArcadeAntics/this.path/main/tests/this.path_w_URLs.R")
}, condition = base::message)
# }

Run the code above in your browser using DataLab