GitHub

Folders and files

NameName

Last commit message

Last commit date

Determine a script's filename from within the script itself
Description:
     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()
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’.
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.
Examples:
     # Put this in example.R and try running source("example.R")
     # and `Rscript example.R`
     filename <- current_filename()
     print(filename)

Read the original on github.com ↗