在 R 的浏览器中添加 trace/breakpoint
add trace/breakpoint while already in R's browser
Edit:为了记录,接受的答案有一个显着的下降,因为它重新执行了前 n 行重新调试时函数中的代码。这可能没问题,但是当这些代码行包含副作用(例如,数据库更新)and/or 长时间计算时,发生的事情就很明显了。我不相信 R 提供了这样做的能力 "properly"(像其他一些语言那样)。无赖。
一些调试器允许您在调试器中动态添加断点。 R 中可以实现该功能吗?一个例子:
quux <- function(..)
{ # line 1
"line 2"
"line 3"
"line 4"
"line 5"
"line 6"
}
trace("quux", tracer = browser, at = 3)
# [1] "quux"
quux()
# Tracing quux() step 3
# Called from: eval(expr, envir, enclos)
# Browse[1]>
# debug: [1] "line 3"
在调试时,我相信我想在代码中跳转。想象一下这个函数有几百行代码,我宁愿不单步执行它们。
我希望能够做到这一点,并从当前行跳转到下一个有趣的行,但不幸的是它只是继续超出了功能。
# Browse[2]>
trace("quux", tracer = browser, at = 5)
# [1] "quux"
# Browse[2]>
c
# [1] "line 6"
# # (out of the debugger)
调试器中的 trace
调用只是将断点添加到原始(全局)函数,如我立即再次调用该函数所示:
quux()
# Tracing quux() step 5
# Called from: eval(expr, envir, enclos)
# Browse[1]>
# debug: [1] "line 5"
我尝试在浏览器中同时设置两者 (at=c(3,5)
),但这只是为我退出调试器并再次调用函数时设置那些行。
我猜这与 trace
附加断点的函数有关。查看 trace
(和 .TraceWithMethods
),我想我需要设置 where
,但我不知道如何让它在-调试功能。
(大图是我正在对处理 kafka 主导的数据流的函数进行故障排除。我目前的两个选项是(a)用更合适的跟踪重启函数,但这需要我也清除并重新启动数据流;或者 (b) 在调试器中逐行进行,当有数百行代码时很乏味。)
这可能是一种解决方案。首先按照你的 post:
> quux <- function(..)
+ { # line 1
+ x <- 1 # added for illustration
+ "line 3"
+ "line 4"
+ "line 5"
+ print(x) # added for illustration
+ "line 7"
+ "line 8"
+ }
>
> trace("quux", tracer = browser, at = 4)
[1] "quux"
> quux()
Tracing quux() step 4
Called from: eval(expr, p)
Browse[1]> n
debug: [1] "line 4"
接下来我们在调试器中进行如下操作:
Browse[2]> this_func <- eval(match.call()[[1]]) # find out which funcion is called
Browse[2]> formals(this_func) <- list() # remove arguments
Browse[2]> body(this_func) <- body(this_func)[-(2:4)] # remove lines we have evalutaed
Browse[2]> trace("this_func", tracer = browser,
+ at = 8 - 4 + 1) # at new line - old trace point
Tracing function "this_func" in package "base"
[1] "this_func"
Browse[2]> this_func # print for illustration
function ()
{
"line 5"
print(x)
"line 7"
"line 8"
}
Browse[2]> environment(this_func) <- environment() # change enviroment so x is present
Browse[2]> this_func() # call this_func
[1] 1
[1] "line 8"
缺点是在我们退出对 this_func
的调用后,我们在对 quux
的原始调用中返回到 "line 5"
。此外,我们必须跟踪最后的 at
值。我们也许可以从另一个函数中得到这个?
Edit:为了记录,接受的答案有一个显着的下降,因为它重新执行了前 n 行重新调试时函数中的代码。这可能没问题,但是当这些代码行包含副作用(例如,数据库更新)and/or 长时间计算时,发生的事情就很明显了。我不相信 R 提供了这样做的能力 "properly"(像其他一些语言那样)。无赖。
一些调试器允许您在调试器中动态添加断点。 R 中可以实现该功能吗?一个例子:
quux <- function(..)
{ # line 1
"line 2"
"line 3"
"line 4"
"line 5"
"line 6"
}
trace("quux", tracer = browser, at = 3)
# [1] "quux"
quux()
# Tracing quux() step 3
# Called from: eval(expr, envir, enclos)
# Browse[1]>
# debug: [1] "line 3"
在调试时,我相信我想在代码中跳转。想象一下这个函数有几百行代码,我宁愿不单步执行它们。
我希望能够做到这一点,并从当前行跳转到下一个有趣的行,但不幸的是它只是继续超出了功能。
# Browse[2]>
trace("quux", tracer = browser, at = 5)
# [1] "quux"
# Browse[2]>
c
# [1] "line 6"
# # (out of the debugger)
调试器中的 trace
调用只是将断点添加到原始(全局)函数,如我立即再次调用该函数所示:
quux()
# Tracing quux() step 5
# Called from: eval(expr, envir, enclos)
# Browse[1]>
# debug: [1] "line 5"
我尝试在浏览器中同时设置两者 (at=c(3,5)
),但这只是为我退出调试器并再次调用函数时设置那些行。
我猜这与 trace
附加断点的函数有关。查看 trace
(和 .TraceWithMethods
),我想我需要设置 where
,但我不知道如何让它在-调试功能。
(大图是我正在对处理 kafka 主导的数据流的函数进行故障排除。我目前的两个选项是(a)用更合适的跟踪重启函数,但这需要我也清除并重新启动数据流;或者 (b) 在调试器中逐行进行,当有数百行代码时很乏味。)
这可能是一种解决方案。首先按照你的 post:
> quux <- function(..)
+ { # line 1
+ x <- 1 # added for illustration
+ "line 3"
+ "line 4"
+ "line 5"
+ print(x) # added for illustration
+ "line 7"
+ "line 8"
+ }
>
> trace("quux", tracer = browser, at = 4)
[1] "quux"
> quux()
Tracing quux() step 4
Called from: eval(expr, p)
Browse[1]> n
debug: [1] "line 4"
接下来我们在调试器中进行如下操作:
Browse[2]> this_func <- eval(match.call()[[1]]) # find out which funcion is called
Browse[2]> formals(this_func) <- list() # remove arguments
Browse[2]> body(this_func) <- body(this_func)[-(2:4)] # remove lines we have evalutaed
Browse[2]> trace("this_func", tracer = browser,
+ at = 8 - 4 + 1) # at new line - old trace point
Tracing function "this_func" in package "base"
[1] "this_func"
Browse[2]> this_func # print for illustration
function ()
{
"line 5"
print(x)
"line 7"
"line 8"
}
Browse[2]> environment(this_func) <- environment() # change enviroment so x is present
Browse[2]> this_func() # call this_func
[1] 1
[1] "line 8"
缺点是在我们退出对 this_func
的调用后,我们在对 quux
的原始调用中返回到 "line 5"
。此外,我们必须跟踪最后的 at
值。我们也许可以从另一个函数中得到这个?