Rev 88581 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/browser.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2023 R Core Team% Distributed under GPL 2 or later\name{browser}\alias{browser}\title{Environment Browser}\description{Interrupt the execution of an expression and allow the inspection ofthe environment where \code{browser} was called from.}\usage{browser(text = "", condition = NULL, expr = TRUE, skipCalls = 0L)}\arguments{\item{text}{a text string that can be retrieved once the browser is invoked.}\item{condition}{a condition that can be retrieved once the browser isinvoked.}\item{expr}{a \dQuote{condition}. By default, and whenever not falseafter being coerced to \code{\link{logical}}, thedebugger will be invoked, otherwise control is returned directly.}\item{skipCalls}{how many previous calls to skip when reporting thecalling context.}}\details{A call to \code{browser} can be included in the body of a function.When reached, this causes a pause in the execution of thecurrent expression and allows access to the \R interpreter.The purpose of the \code{text} and \code{condition} arguments are toallow helper programs (e.g., external debuggers) to insert specificvalues here, so that the specific call to browser (perhaps its locationin a source file) can be identified and special processing can beachieved. The values can be retrieved by calling \code{\link{browserText}}and \code{\link{browserCondition}}.The purpose of the \code{expr} argument is to allow for the illusionof conditional debugging. It is an illusion, because execution isalways paused at the call to browser, but control is only passedto the evaluator described below if \code{expr} is not \code{FALSE} aftercoercion to logical.In most cases it is going to be more efficient to use an \code{if}statement in the calling program, but in some cases using this argumentwill be simpler.The \code{skipCalls} argument should be used when the \code{browser()}call is nested within another debugging function: it will look furtherup the call stack to report its location.At the browser prompt the user can enter commands or \R expressions,followed by a newline. The commands are\describe{\item{\code{c}}{exit the browserand continue execution at the next statement.}\item{\code{cont}}{synonym for \code{c}.}\item{\code{f}}{finish execution of the current loop or function.}\item{\code{help}}{print this list of commands.}\item{\code{n}}{evaluate the next statement, stepping overfunction calls. For byte compiled functions interrupted by\code{browser} calls, \code{n} is equivalent to \code{c}.}\item{\code{s}}{evaluate the next statement, stepping intofunction calls. Again, byte compiled functions make\code{s} equivalent to \code{c}.}\item{\code{where}}{print a stack trace of all active function calls.}\item{\code{r}}{invoke a \code{"resume"} restart if one isavailable; interpreted as an \R expression otherwise. Typically\code{"resume"} restarts are established for continuing from userinterrupts.}\item{\code{Q}}{exit the browser and the current evaluation andreturn to the top-level prompt.}}Leading and trailing whitespace is ignored, except for an empty line.Handling of empty lines depends on the \code{"browserNLdisabled"}\link[=options]{option}; if it is \code{TRUE}, empty lines are ignored.If not, an empty line is the same as \code{n} (or \code{s}, if it was usedmost recently).Anything else entered at the browser prompt is interpreted as an\R expression to be evaluated in the calling environment: inparticular typing an object name will cause the object to be printed,and \code{ls()} lists the objects in the calling frame. (If you wantto look at an object with a name such as \code{n}, print itexplicitly, or use \I{autoprint} via \code{(n)}.The number of lines printed for the deparsed call can be limited bysetting \code{\link{options}(deparse.max.lines)}.The browser prompt is of the form \code{Browse[\var{n}]>}: here\code{\var{n}} indicates the \sQuote{browser level}. The browser canbe called when browsing (and often is when \code{\link{debug}} is inuse), and each recursive call increases the number. (The actualnumber is the number of \sQuote{contexts} on the context stack: thisis usually \code{2} for the outer level of browsing and \code{1} whenexamining dumps in \code{\link{debugger}}.)This is a primitive function but does argument matching in thestandard way.}\section{Interaction with Condition Handling}{Because the browser prompt is implemented using the\link[=conditions]{restart and condition handling mechanism},it prevents error handlers set up before the breakpoint from beingcalled or invoked. The implementation follows this model:\preformatted{repeat withRestarts(withCallingHandlers(readEvalPrint(),error = function(cnd) {cat("Error:", conditionMessage(cnd), "\n")invokeRestart("browser")}),browser = function(...) NULL)readEvalPrint <- function(env = parent.frame()) {print(eval(parse(prompt = "Browse[n]> "), env))}}The restart invocation interrupts the lookup for condition handlersand transfers control to the next iteration of the debugger \acronym{REPL}.Note that condition handlers for other classes (such as \code{"warning"})are still called and may cause a non-local transfer of control out of thedebugger.}\references{\bibshow{R:Becker+Chambers+Wilks:1988, R:Chambers:1998}}\seealso{\code{\link{debug}}, and\code{\link{traceback}} for the stack on error.\code{\link{browserText}} for how to retrieve the text and condition.}\keyword{programming}\keyword{environment}