R 在顶层错误时获取调用?

R 在顶层错误时获取调用?,r,error-handling,R,Error Handling,当我使用tryCatch时,我可以定义一个错误处理程序,并使用conditionCall确定导致错误的调用。比如说, tryCatch( eval(parse(text="prnit('Hello')")), error=function(e) { cl <- conditionCall(e) #... }) tryCatch( eval(parse(text=“prnit('Hello')”), 错误=函数(e){ cl err_hdl它

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

tryCatch(
    eval(parse(text="prnit('Hello')")),
    error=function(e) {
      cl <- conditionCall(e)
      #...
    })
tryCatch(
eval(parse(text=“prnit('Hello')”),
错误=函数(e){

cl err_hdl它看起来像是
dump。可以告诉帧
不要转储到文件,而是转储到
.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.

第一个示例中没有错误。您在第一个示例中的意思更像这样,对吗?
tryCatch(eval(parse(text=prnit('Hello')),error=function(e){conditionCall(e)})
@Josh O'Brien是的,你是对的。更改了问题。@kohske问题是我不能对在R控制台中输入的命令使用
tryCatch
。我正在为交互式会话寻找一种自定义错误捕获机制。FWIW,关于您的编辑,
err\u hdl
功能应与平台无关,如前4行您的
err\u hdl
函数的前4行与
history
函数中的前4行相同。感谢对
dump.frames
的提示,这对我来说是一个新函数。我可以获取导致错误的输入吗,即示例中的输入
prnit(dt)
?@KarstenW.,我不确定我是否理解“获取”的含义.你想用
prnit(dt)
做什么?啊,是不是错误消息只是返回了它找不到的函数,而不是生成它的完整代码行?@KarstenW.,我想我明白你的意思了,但是既然
tail(rawhist,1)
在您的示例中,此计算机上没有返回任何有意义的内容,这仍然是一个猜测。我将看看明天是否能取得进展。@KarstenW.,不幸的是,没有任何进展要报告,除了添加任务回调以保存用户输入的最后一个表达式(如所述)似乎无法保存产生错误的表达式.但你可能已经发现了。
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
$`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"