簡體   English   中英

tryCatch()問題

[英]Problems with tryCatch()

我正在嘗試在R中構建一個如下所示的腳本:

randomlist <- list(x=ran1, y=ran2) #{x,y}
contour <- Conte(c(round(randomlist$x),round(randomlist$y)),img@grey)
#if Conte function returns me an error, then get new randomlist values
#Do until it doesn't returns an error

如果隨機{x,y}在圖像區域內{x,y}則該函數返回輪廓圖像的坐標。 我知道這也許是愚蠢的,不是很明確的問題,但是我不擅長R,錯誤處理對我來說很混亂。 我嘗試了tryCatch()但我不知道如何應用它。 非常感謝你!

可復制的代碼會有所幫助。 我通常用try來處理這類問題:

temp <- function(N) {
  if (N < 0) stop("Error")
  return(N)
}

out <- 0 
n <- 0 
out <- try(temp(rnorm(1)))
while(class(out) == "try-error") {
 n <- n + 1
 print(n) 
 out <- try(temp(rnorm(1))) 
}  
out 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM