2016-09-27 75 views
0

我讀了很多關於使用tryCatch()(來自本網站和其他人),但我似乎無法得到它的工作。我知道這是一個多餘的問題,我很抱歉,但我真的可以使用幫助。跳過R中的錯誤消息

read.rwl()打破了我的循環,因爲我試圖讀取的一些數據是凌亂的。我想跳過list.rwl中的任何網址,這些網址會中斷我的循環,同時還會將作爲對象的URL保存爲會破壞循環的網址。

itrdb <- read.csv ("itrdb.csv") 
itrdb.rwl <- (itrdb [,7]) 

library (dplR) 

for (i in 1:length(itrdb)) { 
list.rwl <- as.character (rwl.crn [1] [i]) 
skip_with_message = simpleError('Did not work out') 
x <- tryCatch(read.rwl (list.rwl), error = function(e) skip_with_message) 
} 
+1

如果您使用的是非基礎包中的函數,則應該包含庫()調用所需的所有內容。並且您應該爲正在使用的對象包含dput。 –

+0

嘗試重新閱讀其中一些來源。我懷疑他們建議你像上面那樣使用'tryCatch'。 – nrussell

+0

感謝您的有用評論。我知道我沒有遠程使用tryCatch。我嘗試了其他變化無濟於事,因此我發佈了我的問題。 – ZempOh

回答

0

一旦您使用tryCatch捕獲錯誤,您可以使用它做些事情。在下面的例子中,我只是捕獲錯誤(或產生一個隨機數),但您可以調整產生NA或適合您的流量的任何內容。有關更多示例,請參閱?tryCatch

x <- as.list(rep(NA, 10)) 

set.seed(357) 
for (i in 1:10) { 
    feelin.lucky <- runif(1) 

    if (feelin.lucky >= 0.5) { 
    x[[i]] <- tryCatch({ 
     simpleError("this is error") 
    }, error = function(e) e) 
    } else{ 
    x[[i]] <- rnorm(1) 
    } 
} 

> x 
[[1]] 
[1] -1.597783 

[[2]] 
[1] 0.3947471 

[[3]] 
<simpleError: this is error> 

[[4]] 
<simpleError: this is error> 

[[5]] 
<simpleError: this is error> 

[[6]] 
<simpleError: this is error> 

[[7]] 
<simpleError: this is error> 

[[8]] 
<simpleError: this is error> 

[[9]] 
[1] -0.7539072 

[[10]] 
[1] -0.4690151