在R中使用复杂功能的tryCatch和plyr [英] tryCatch with a complicated function and plyr in R

查看:124
本文介绍了在R中使用复杂功能的tryCatch和plyr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的,很长的功能,我用来做模拟。它可以产生错误,主要是与以零方差相等的值结尾的随机向量进行比较,将其加入到PCA或逻辑回归中。



我正在使用 doMC plyr 。我不想要 tryCatch 函数内的每个小东西,因为错误的可能性很多,而且每个都有很小的可能性。



如何tryCatch每个运行,而不是 tryCatch 每一行?代码是这样的:

  iteration = function(){
a真的很长的模拟功能,可能会发生错误
}
reps = 10000
results = llply(1:reps,function(idx){out< -iteration()} ,. parallel = TRUE)


foreach 包使得这比使用 plyr

 库更容易(foreach)
output< - foreach(i = 1:reps,.errorhandling ='remove')%dopar%{
function
}


解决方案

您可以将try catch循环包装到您传递给llply的函数中吗?

  results = llply(1:reps,function(idx){
out = NA
try({
out< ; -iteration()
},silent = T)
out
} ,. parallel = TRUE)


I've got a complicated, long function that I'm using to do simulations. It can generate errors, mostly having to do with random vectors ending up with equal values with zero-variance, getting fed either into PCA's or logistic regressions.

I'm executing it on a cluster using doMC and plyr. I don't want to tryCatch every little thing inside of the function, because the possibilities for errors are many and the probabilities of each of them are small.

How do I tryCatch each run, rather than tryCatching every little line?? The code is something like this:

iteration = function(){
    a really long simulation function where errors can happen
    }
reps = 10000
results = llply(1:reps, function(idx){out<-iteration()},.parallel=TRUE)

EDIT about a year later: The foreach package makes this substantially easier than it is with plyr

library(foreach)
output <- foreach(i=1:reps, .errorhandling = 'remove')%dopar%{
  function
}

解决方案

Can you wrap the try catch loop in the function you pass to llply?

results = llply(1:reps, function(idx){
    out = NA
    try({ 
        out<-iteration()
    }, silent=T)
    out
},.parallel=TRUE)

这篇关于在R中使用复杂功能的tryCatch和plyr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆