如何调试泛型函数(在调试包中使用debug或mtrace)?
作为一个例子,我想调试NADA包中的cenreg,特别是采用公式输入的方法。
您可以像这样检索方法详细信息:
library(NADA)
getMethod("cenreg", c("formula", "missing", "missing"))
function (obs, censored, groups, ...)
{
.local <- function (obs, censored, groups, dist, conf.int = 0.95,
...)
{
dist = ifelse(missing(dist), "lognormal", dist)
...
}
问题是cenreg本身看起来像这样:
body(cenreg)
# standardGeneric("cenreg")
我不知道如何单步执行底层方法,而不是通用包装器。
我的前两个建议非常基本:(1)将函数调用包装在一个 try()
(经常提供有关S4课程的更多信息)和(2)通话 traceback()
抛出错误后(有时可以提示问题实际发生的位置)。
调用 debug()
在这种情况下无济于事,所以你需要使用 trace
要么 browser
。从调试帮助页面:
"In order to debug S4 methods (see Methods), you need to use trace, typically
calling browser, e.g., as "
trace("plot", browser, exit=browser, signature = c("track", "missing"))
S4课程很难处理;一个例子是评论 该 debug
包 文档(关于使用 mtrace()
与S4课程):
"I have no plans to write S4 methods, and hope not to have to
debug other people’s!"
一个 最近在R-Help上提出了类似的问题。邓肯默多克的推荐:
"You can insert a call to browser() if you want to modify the source. If
you'd rather not do that, you can use trace() to set a breakpoint in it.
The new setBreakpoint() function in R 2.10.0 will also work, if you
install the package from source with the R_KEEP_PKG_SOURCE=yes
environment variable set. It allows you to set a breakpoint at a
particular line number in the source code."
我以前从未这样做过(它需要R 2.10.0),但你可以尝试从源码安装 R_KEEP_PKG_SOURCE=yes
。
顺便说一句,你可以使用 NADA的CRAN镜子 在github中浏览源代码。
我的前两个建议非常基本:(1)将函数调用包装在一个 try()
(经常提供有关S4课程的更多信息)和(2)通话 traceback()
抛出错误后(有时可以提示问题实际发生的位置)。
调用 debug()
在这种情况下无济于事,所以你需要使用 trace
要么 browser
。从调试帮助页面:
"In order to debug S4 methods (see Methods), you need to use trace, typically
calling browser, e.g., as "
trace("plot", browser, exit=browser, signature = c("track", "missing"))
S4课程很难处理;一个例子是评论 该 debug
包 文档(关于使用 mtrace()
与S4课程):
"I have no plans to write S4 methods, and hope not to have to
debug other people’s!"
一个 最近在R-Help上提出了类似的问题。邓肯默多克的推荐:
"You can insert a call to browser() if you want to modify the source. If
you'd rather not do that, you can use trace() to set a breakpoint in it.
The new setBreakpoint() function in R 2.10.0 will also work, if you
install the package from source with the R_KEEP_PKG_SOURCE=yes
environment variable set. It allows you to set a breakpoint at a
particular line number in the source code."
我以前从未这样做过(它需要R 2.10.0),但你可以尝试从源码安装 R_KEEP_PKG_SOURCE=yes
。
顺便说一句,你可以使用 NADA的CRAN镜子 在github中浏览源代码。