我有一个看起来像这样的并行过程:
library(foreach)
library(doSNOW)
cl = makeCluster(parallel::detectCores() - 1, type = "SOCK", outfile = "out.txt")
registerDoSNOW(cl)
l = foreach(i = 1:100) %dopar% {
res = withCallingHandlers(
read.csv("somefilethatdoesntexist.csv"), error = function(e) e)
if(inherits(res, "error")) res = NULL
res
}
我的期望是,即使“表达式”中有错误,循环也应该继续,但它会因错误而退出,并且不会创建生成的“l”变量。
这似乎尤其与丢失的文件有关。但是,如果我将它包装在 tryCatch 中并在“表达式”中进行适当处理,它怎么会出错?