An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120525/0540d927/attachment.pl>
question about TryCatch and lapply
4 messages · John Kerpel, jim holtman, Martin Morgan
Please show us the 'lapply' statement you are using. Here is a simple case of catching an error in an lapply and continuing:
lapply(c(1,2,-3, 4), function(x){
+ a <- try(stopifnot(x > 0)) # force an error + if (inherits(a, 'try-error')) return(NULL) + x + }) Error : x > 0 is not TRUE [[1]] [1] 1 [[2]] [1] 2 [[3]] NULL [[4]] [1] 4
On Fri, May 25, 2012 at 2:51 PM, John Kerpel <john.kerpel at gmail.com> wrote:
Folks:
I've replaced an outer for-loop with lapply and it works great. ?But, I
can't seem to do the following type of exception handling:
tryCatch(dlmMLE(x)$value==Inf,error = function(e) NULL)
which basically says if the likelihood is Inf, throw an error. ?But what I
want it to do is just go to the next index in the list. ?When I was using a
for-loop I used:
if(tryCatch(dlmMLE(x)$value==Inf,error = function(e) 1)==1) {next} else
.... which worked fine.
Is there a way to do the same thing in lapply?
Thanks for your time. (I've checked Gmane for this type of problem and I
wasn't sure if this problem was answered or not...)
John
? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120525/e6e2d223/attachment.pl>
On 5/25/2012 12:48 PM, John Kerpel wrote:
Jim: That's the ticket! I'm actually using parLapply with a long, ugly function - so I was loath to post that mess. Many thanks - you saved my weekend.
In the context of tryCatch in your question
lst <- list(1, 2, -3, 4)
sapply(lst, function(x) tryCatch({
stopifnot(x > 0)
x
}, error=function(err) {
NA
}))
which separates out the implementation of the function from the error
handling instead of intermingling the two as with try(). Or a little
more elaboration, reporting the error to stderr as might be appropriate
for a parallel job
sapply(lst, function(x) {
tryCatch({
stopifnot(x>0)
sqrt(x)
}, error=function(err) {
## demote to message on stderr; in context of tryCatch so
## 'x' visible
message(conditionMessage(err), "; x = ", x)
NaN
})
})
or for warnings, showing use of restarts
withCallingHandlers({
sapply(lst, sqrt)
}, warning=function(warn) {
message(conditionMessage(warn))
## restart where the warning occurs; see ?warning and source for
## warning()
invokeRestart("muffleWarning")
})
Martin
On Fri, May 25, 2012 at 2:23 PM, jim holtman<jholtman at gmail.com> wrote:
Please show us the 'lapply' statement you are using. Here is a simple case of catching an error in an lapply and continuing:
lapply(c(1,2,-3, 4), function(x){
+ a<- try(stopifnot(x> 0)) # force an error + if (inherits(a, 'try-error')) return(NULL) + x + }) Error : x> 0 is not TRUE [[1]] [1] 1 [[2]] [1] 2 [[3]] NULL [[4]] [1] 4 On Fri, May 25, 2012 at 2:51 PM, John Kerpel<john.kerpel at gmail.com> wrote:
Folks: I've replaced an outer for-loop with lapply and it works great. But, I can't seem to do the following type of exception handling: tryCatch(dlmMLE(x)$value==Inf,error = function(e) NULL) which basically says if the likelihood is Inf, throw an error. But what
I
want it to do is just go to the next index in the list. When I was
using a
for-loop I used:
if(tryCatch(dlmMLE(x)$value==Inf,error = function(e) 1)==1) {next} else
.... which worked fine.
Is there a way to do the same thing in lapply?
Thanks for your time. (I've checked Gmane for this type of problem and I
wasn't sure if this problem was answered or not...)
John
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Dr. Martin Morgan, PhD Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109