定位错误(Locating errors)

显示调用堆栈

f <- function(a) g(a)
g <- function(b) h(b)
h <- function(c) i(c)
i <- function(d) {
  if (!is.numeric(d)) {
    stop("`d` must be numeric", call. = FALSE)
  }
}
traceback()

图片alt

图片alt

交互式调试(Interactive debugger)

browser()

browser()

Next, n: executes the next step in the function. If you have a variable named n, you’ll need print(n) to display its value.

Step into, or s: works like next, but if the next step is a function, it will step into that function so you can explore it interactively.

Finish, or f: finishes execution of the current loop or function.

Continue, c: leaves interactive debugging and continues regular execution of the function. This is useful if you’ve fixed the bad state and want to check that the function proceeds correctly.

Stop, Q: stops debugging, terminates the function, and returns to the global workspace. Use this once you’ve figured out where the problem is, and you’re ready to fix it and reload the code.

option(error=revover)

options(error = NULL)
options(error = recover)

debug

debug() inserts a browser statement in the first line of the specified function. undebug() removes it. Alternatively, you can use debugonce() to browse only on the next run.

source("test2.R")
findLineNum("test2.R",5)
utils::setBreakpoint("test2.R", 2)
trace()
untrace()

interactive debugger C/C++

http://r-pkgs.had.co.nz/src.html#src-debugging
https://github.com/wch/r-debug/blob/master/debugging-r.md
http://kevinushey.github.io/blog/2015/04/05/debugging-with-valgrind/
https://www.jimhester.com/2018/08/22/debugging-rstudio/

debug package

install("DESeq2", keep_source=TRUE)

参考

https://adv-r.hadley.nz/debugging.html
https://stackoverflow.com/questions/48934886/how-do-i-keep-source-files-when-using-rs-devtools-library-function-install