gpt4 book ai didi

r - 要求 R 调试器不要截断堆栈调用

转载 作者:行者123 更新时间:2023-12-04 23:49:57 29 4
gpt4 key购买 nike

我想调试以下函数,但假设在调试器中查看 moreajaj 的参数等于什么(假设不像在这个人为的示例中那么明显)是有用的。我可以在调试器框架中打印它,但是在每个参数的每个框架中都这样做很烦人。在宣布每一帧时,如何才能使完整的参数调用在调试器中打印?

options(error = dump.frames)

#### suppose I do not see the function definitions, only see debugger below
some_function <- function(...) {
stop('give error')
}

willGiveError <- function() {
some_function(alongargument = "some long kind of default", anotherlongargument = "more long default something as example", moreajaj = "kdjflksdjf")
}

outerFunction <- function() willGiveError()

outerFunction()

# Error in some_function(alongargument = "some long kind of default", anotherlongargument = "more long default something as example", :
# give error
> debugger()
# Message: Error in some_function(alongargument = "some long kind of default", anotherlongargument = "more long default something as example", :
# give error
# Available environments had calls:
# 1: outerFunction()
# 2: #1: willGiveError()
# 3: #2: some_function(alongargument = "some long kind of default", another
# 4: #2: stop("give error")
#
# Enter an environment number, or 0 to exit Selection:

正如您在上面的调试器输出中看到的那样,它在第 2 帧中的“another”之后被剪切,我想在此处看到 anotherlongargument = "more long default something as example", moreajaj = "kdjflksdjf")

最佳答案

dump.frames() 的评估过程中,这些调用的文本表示被截断了。通过调用 limitedLabels() .

一个简单的解决方案是编写一个类似的函数,不同之处仅在于提供(足够大的值)maxwidth=论据 limitedLabels() .

## Identical to dump.frames _except_ for addition of maxwidth=10000 argument
my.dump.frames <-
function (dumpto = "last.dump", to.file = FALSE)
{
calls <- sys.calls()
last.dump <- sys.frames()
names(last.dump) <- limitedLabels(calls, maxwidth=10000) # <-- edited line
last.dump <- last.dump[-length(last.dump)]
attr(last.dump, "error.message") <- geterrmessage()
class(last.dump) <- "dump.frames"
if (dumpto != "last.dump")
assign(dumpto, last.dump)
if (to.file)
save(list = dumpto, file = paste(dumpto, "rda", sep = "."))
else assign(dumpto, last.dump, envir = .GlobalEnv)
invisible()
}

然后设置它,而不是 dump.frames ,作为当错误中断 R 的评估时要运行的函数。
options(error=my.dump.frames)

有了这个设置,运行你提供的代码,然后进入调试器,就会得到你希望的结果:
debugger()
# Message: Error in some_function(alongargument = "some long kind of default", anotherlongargument = "more long default something as example", :
# give error
# Available environments had calls:
# 1: outerFunction()
# 2: #1: willGiveError()
# 3: #2: some_function(alongargument = "some long kind of default", anotherlongargument = "more long default something as example", moreajaj = "kdjflksdjf")
# 4: #2: stop("give error")
#
# Enter an environment number, or 0 to exit Selection:

关于r - 要求 R 调试器不要截断堆栈调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24988068/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com