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

函数 returns 轮廓图像的坐标,如果随机 {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