sourcing: Detect whether a file is being sourced from another file.

sourcingR Documentation

Detect whether a file is being sourced from another file.

Description

sourcing() returns TRUE if it seems that it is called from a file that is being sourced, e.g., with \linkIntsource or \linkIntsys.source. Otherwise, it returns FALSE. The command is helpful when we want a file to behave differently depending on whether it is sourced.

Usage

sourcing()

Details

sourcing() is just an alias for sys.nframe() != 1L.

See Also

\linkInt

interactive, \linkIntsys.nframe

The function was inspired by inspired by interactive(), which detects whether a command is being run interactively. Stack Overflow user r2evans wrote a helpful post: see https://stackoverflow.com/a/47932989/697473.

Examples

if (sourcing()) {
  print("It seems that this code is being sourced. It's not being 
    executed directly from the global environment.")
} else {
  print("It seems that this code is not being sourced. It's being executed
    directly from the global environment.")
}

jbullock35/Bullock documentation built on April 1, 2022, 6:21 p.m.