gpt4 book ai didi

r - 在顶层获取错误调用?

转载 作者:行者123 更新时间:2023-12-04 12:48:47 25 4
gpt4 key购买 nike

当我使用 tryCatch ,我可以定义一个错误处理程序并使用 conditionCall确定导致错误的调用。例如,

tryCatch(
eval(parse(text="prnit('Hello')")),
error=function(e) {
cl <- conditionCall(e)
#...
})

我可以在 R 提示符下接听错误的电话吗?到现在为止,我只找到了这个解决方案:
> err_hdl  <- function() {
file1 <- tempfile("Rrawhist")
savehistory(file1)
rawhist <- readLines(file1)
unlink(file1)

cat("Error : ", tail(rawhist,1), "\n")
return(TRUE)
}
> options(error=err_hdl)
> prnit("Hello")

但我相信一定有更直接的方法..

任何提示表示赞赏!

我发出了赏金。实现 err_hdl 行为的第一个答案上面没有与平台无关的文件 I/O 的函数将被接受。

编辑——上面的代码似乎只适用于 Windows。我正在寻找引发错误的输入。

最佳答案

它看起来像 dump.frames可以告诉不要转储到文件,而是转储到 .GlobalEnv 中的对象.但是,我没有在 Mac 上测试过它。以下有帮助吗?

err_hdl2 <- function() {
dump.frames("theErr", to.file = FALSE)
cat("What happened?\n", attr(theErr,"error.message"), "\nOh.\n")
}
options(error = err_hdl2)

> prnit(dt)
Error: could not find function "prnit"
What happened?
Error: could not find function "prnit"

Oh.

创建对象 theErr 可能存在缺陷在全局环境中,我想。
> theErr
$`function ()
{
dump.frames("theErr", to.file = FALSE)
cat("What`
<environment: 0x1030fe140>

attr(,"error.message")
[1] "Error: could not find function \"prnit\"\n"
attr(,"class")
[1] "dump.frames"

关于r - 在顶层获取错误调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11173053/

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