Skip to contents

A small set of functions wrapping up the call stack and command line inspection needed to determine a running script's filename from within the script itself.

Usage

current_filename()

current_source_filename()

current_cli_filename()

Value

A character vector of length 1 if a script name can be found, otherwise NULL. No manipulation is done to the filename, so it may be relative or absolute.

Details

current_filename() returns the result of current_source_filename() if not NULL, otherwise the result of current_cli_filename(), which might be NULL. You should use this wrapper function rather than the more-specific functions unless you have a very specific need.

current_source_filename() returns the filename from the most recent call to source in the current call stack. From within a sourced script, this is the filename of the script itself.

current_cli_filename() returns the filename found on the command line invocation of R or Rscript. This may or may not be the caller's file if there's been an intervening source.

Examples

# Put this in example.R and try running source("example.R")
# and `Rscript example.R`
filename <- current_filename()
message(filename)
#> /home/runner/work/_temp/1414cbc1-07f6-4efe-b729-b1c4a928cc73