我的目標是捕獲R拋出並將其存儲在對象中的錯誤。在循環中使用geterrmessage() - R
下面是一些僞代碼:
for(i in 1:length(a)){try(
if (i==4)(print(a[i]/"b"))else(print(a[i]/b[i]))
)}
[1] -0.125
[1] -0.2857143
[1] -0.5
Error in a[i]/"b" : non-numeric argument to binary operator
[1] -1.25
[1] -2
[1] -3.5
[1] -8
[1] Inf
[1] 10
所以我希望捕獲的4號重複的錯誤是:Error in a[i]/"b" : non-numeric argument to binary operator
成一個對象說:
error<-()
iferror(error[i]<-geterrmessage())
我知道IFERROR爲一個函數在R中不可用,但我試圖給出這個想法,因爲geterrmessage
僅捕獲它看到的最後一個錯誤
所以對於示例i想要說的error[1:3]<-'NA'
和error[5:10]<-'NA'
因爲沒有錯誤,但
error[4]<-"Error in a[i]/"b" : non-numeric argument to binary operator"
這樣,以後我可以檢查error
對象和明白的地方,什麼錯誤發生
如果你能幫助我寫這將是極好的代碼和高度讚賞的
你看着'tryCatch'?可能會有用 –