2017-08-31 45 views

回答

0

不知道我是否完全理解你的问题,但如果你想捕捉一个错误,然后控制事件应该发生的错误,你想看看?tryCatch。这就是说,这里是一个例子,你如何捕捉函数中引发的错误并将其作为字符串返回。

编辑以显示如何,如果你喜欢

f <- function(...){ 
    tryCatch(is.character(...), error = function(e){ 
     var_e <- capture.output(e, file = "errors", append = TRUE) 
     readLines("errors") ## just to push the output to our variable at the terminal 
    }) 
} 

通过传递一个不带引号的串入功能强制错误将捕捉到的警告作为一个字符对象变量a

写入文件
> a <- f(noquotes) 
> a 
[1] "<simpleError in doTryCatch(return(expr), name, parentenv, handler): object 'noquotes' not found>" 
> typeof(a) 
[1] "character" 
+0

这是'sink'的一个很好的选择,但我的脚本将被视为条件对象,我无法获得脚本输出结果。 –

+1

您需要提供更多关于您的意思的信息...例如脚本本身或与其接近的示例以及为什么需要捕获错误 –

相关问题