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.