
Detect the current execution context
Source:R/detect-execution-context.R
detect_execution_context.RdIdentifies which of three execution environments the code is currently
running in: an interactive R session, a quarto render call, or a
plain Rscript invocation. This is useful for writing code that behaves
correctly across all three contexts, such as resolving input file paths
in a portable way.
Arguments
- interactive_fn
A function. Used to detect whether the session is interactive. Defaults to
base::interactive. Override in tests to simulate different execution environments.
Details
Detection follows a priority order:
If
interactive()isTRUE, returns"interactive".If the environment variable
QUARTO_DOCUMENT_PATHis set and non-empty, returns"quarto".Otherwise, returns
"rscript".
Examples
# \donttest{
context <- detect_execution_context()
input_file <- switch(context,
interactive = "data/sample.csv",
quarto = params$input_file,
rscript = commandArgs(trailingOnly = TRUE)[1]
)
# }