Skip to content

Commit

Permalink
Document r_session$debug()
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Jul 15, 2019
1 parent cd33d0e commit e8b86fe
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
68 changes: 68 additions & 0 deletions R/r-session.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#' rs$close(grace = 1000)
#'
#' rs$traceback()
#' rs$debug()
#' rs$attach()
#' ```
#'
#' @section Arguments:
Expand Down Expand Up @@ -84,6 +86,12 @@
#' equivalent to the [traceback()] call, but it is performed in the
#' subprocess.
#'
#' `rs$debug()` is an interactive debugger to inspect the dumped frames
#' in the subprocess, after an error. See more at [r_session_debug].
#'
#' `rs$attach()` is an experimental function that provides a REPL
#' (Read-Eval-Print-Loop) to the subprocess.
#'
#' @name r_session
#' @examples
#' \dontrun{
Expand Down Expand Up @@ -710,3 +718,63 @@ r_session_options_default <- function() {
extra = list()
)
}

#' Interactive debugging of persistent R sessions
#'
#' The `r_session$debug()` method is an interactive debugger to inspect
#' the stack of the background process after an error.
#'
#' `$debug()` starts a REPL (Read-Eval-Print-Loop), that evaluates R
#' expressions in the subprocess. It is similar to [browser()] and
#' [debugger()] and also has some extra commands:
#'
#' * `.help` prints a short help message.
#' * `.where` prints the complete stack trace of the error. (The same as
#' the `$traceback()` method.
#' * `.inspect <n>` switches the "focus" to frame `<n>`. Frame 0 is the
#' global environment, so `.inspect 0` will switch back to that.
#'
#' To exit the debugger, press the usual interrupt key, i.e. `CTRL+c` or
#' `ESC` in some GUIs.
#'
#' Here is an example session that uses `$debug()` (some output is omitted
#' for brevity):
#'
#' ```
#' # ----------------------------------------------------------------------
#' > rs <- r_session$new()
#' > rs$run(function() knitr::knit("no-such-file"))
#' Error in rs_run(self, private, func, args) :
#' callr subprocess failed: cannot open the connection
#'
#' > rs$debug()
#' Debugging in process 87361, press CTRL+C (ESC) to quit. Commands:
#' .where -- print stack trace
#' .inspect <n> -- inspect a frame, 0 resets to .GlobalEnv
#' .help -- print this message
#' <cmd> -- run <cmd> in frame or .GlobalEnv
#'
#' 3: file(con, "r")
#' 2: readLines(input2, encoding = "UTF-8", warn = FALSE)
#' 1: knitr::knit("no-such-file") at #1
#'
#' RS 87361 > .inspect 1
#'
#' RS 87361 (frame 1) > ls()
#' [1] "encoding" "envir" "ext" "in.file" "input" "input.dir"
#' [7] "input2" "ocode" "oconc" "oenvir" "oopts" "optc"
#' [13] "optk" "otangle" "out.purl" "output" "quiet" "tangle"
#' [19] "text"
#'
#' RS 87361 (frame 1) > input
#' [1] "no-such-file"
#'
#' RS 87361 (frame 1) > file.exists(input)
#' [1] FALSE
#'
#' RS 87361 (frame 1) > # <CTRL + C>
#' # ----------------------------------------------------------------------
#' ```
#'
#' @name r_session_debug
NULL
4 changes: 2 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ bold <- function(x) {
update_history <- function(cmd) {
tmp <- tempfile()
on.exit(unlink(tmp, recursive = TRUE))
savehistory(tmp)
utils::savehistory(tmp)
cat(cmd, "\n", sep = "", file = tmp, append = TRUE)
loadhistory(tmp)
utils::loadhistory(tmp)
}
2 changes: 2 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
cliapp
CMD
Eval
finalizer
igraph
interruptible
macOS
pkgdown
pollable
processx
REPL
subcommand
subprocess
subprocesses
Expand Down
8 changes: 8 additions & 0 deletions man/r_session.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions man/r_session_debug.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e8b86fe

Please sign in to comment.