2017-08-31 40 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

您需要提供更多關於您的意思的信息...例如腳本本身或與其接近的示例以及爲什麼需要捕獲錯誤 –

相關問題