% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/detect-execution-context.R
\name{detect_execution_context}
\alias{detect_execution_context}
\title{Detect the current execution context}
\usage{
detect_execution_context(interactive_fn = interactive)
}
\arguments{
\item{interactive_fn}{A function. Used to detect whether the session is
interactive. Defaults to \code{base::interactive}. Override in tests to
simulate different execution environments.}
}
\value{
A character string, one of \code{"interactive"}, \code{"quarto"}, or
\code{"rscript"}.
}
\description{
Identifies which of three execution environments the code is currently
running in: an interactive R session, a \verb{quarto render} call, or a
plain \code{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.
}
\details{
Detection follows a priority order:
\enumerate{
\item If \code{interactive()} is \code{TRUE}, returns \code{"interactive"}.
\item If the environment variable \code{QUARTO_DOCUMENT_PATH} is set and non-empty,
returns \code{"quarto"}.
\item Otherwise, returns \code{"rscript"}.
}
}
\examples{
\donttest{
context <- detect_execution_context()

input_file <- switch(context,
  interactive = "data/sample.csv",
  quarto      = params$input_file,
  rscript     = commandArgs(trailingOnly = TRUE)[1]
)
}
}
