So after about 4 hours struggling with Try and TryCatch I am throwing in the towel. I have a more complicated function that used logspline through iterative distributions and at some point the logspline doesnt function correctly for some subsets but is fine with others so I need to be able to identify when the error occurs and stop curtailing the distribution and I think this Try or TryCatch should do the trick but I think I am missing something here. A simple example result_<-list() for(i in 1:10){ if(inherits(try(sqrt(9-i),silent=TRUE),"try-error")){ #If sqrt fails result_[[i]]<-0 } else { #If sqrt succeeds result_[[i]] <- 1 } } I would expect this to fail only when i > 9 but succeeds each time. DO i need to specify something different where "try-error" resides? Thanks guys Cheers, Josh -- View this message in context: http://r.789695.n4.nabble.com/To-Try-or-to-TryCatch-I-have-tried-to-long-tp4165578p4165578.html Sent from the R help mailing list archive at Nabble.com.
I think the problem is that it's only a warning and not an error. result_<-list() for(i in 1:10){ if(inherits(try(sqrt(9-i),silent=TRUE),"try-error")){ #If sqrt fails cat('fail',i,'\n') result_[[i]]<-0 } else { #If sqrt succeeds cat('succeed',i,'\n') result_[[i]] <- 1 } } result_ On Tue, Dec 6, 2011 at 11:11 AM, LCOG1 <jroll@lcog.org> wrote:> So after about 4 hours struggling with Try and TryCatch I am throwing in > the > towel. I have a more complicated function that used logspline through > iterative distributions and at some point the logspline doesnt function > correctly for some subsets but is fine with others so I need to be able to > identify when the error occurs and stop curtailing the distribution and I > think this Try or TryCatch should do the trick but I think I am missing > something here. A simple example > > > result_<-list() > for(i in 1:10){ > if(inherits(try(sqrt(9-i),silent=TRUE),"try-error")){ > #If sqrt fails > result_[[i]]<-0 } else { > #If sqrt succeeds > result_[[i]] <- 1 } > } > > > I would expect this to fail only when i > 9 but succeeds each time. DO i > need to specify something different where "try-error" resides? Thanks guys > > Cheers, > Josh > > > -- > View this message in context: > http://r.789695.n4.nabble.com/To-Try-or-to-TryCatch-I-have-tried-to-long-tp4165578p4165578.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
I've had the same problem in other cases. Does anyone know how to catch warnings as well as errors? On Tue, Dec 6, 2011 at 12:34 PM, ROLL Josh F <JRoll@lcog.org> wrote:> ** > Agreed. But what I am trying to control is when I get a warning. I want > to do something different if a warning OR an error pop up? Any ideas? > > Cheers > > ------------------------------ > *From:* gleynes@gmail.com [mailto:gleynes@gmail.com] *On Behalf Of *Gene > Leynes > *Sent:* Tuesday, December 06, 2011 10:27 AM > *To:* ROLL Josh F > *Cc:* r-help@r-project.org > *Subject:* Re: [R] To Try or to TryCatch, I have tried to long > > I think the problem is that it's only a warning and not an error. > > result_<-list() > for(i in 1:10){ > if(inherits(try(sqrt(9-i),silent=TRUE),"try-error")){ > #If sqrt fails > cat('fail',i,'\n') > result_[[i]]<-0 } else { > #If sqrt succeeds > cat('succeed',i,'\n') > result_[[i]] <- 1 } > } > result_ > > On Tue, Dec 6, 2011 at 11:11 AM, LCOG1 <jroll@lcog.org> wrote: > >> So after about 4 hours struggling with Try and TryCatch I am throwing in >> the >> towel. I have a more complicated function that used logspline through >> iterative distributions and at some point the logspline doesnt function >> correctly for some subsets but is fine with others so I need to be able to >> identify when the error occurs and stop curtailing the distribution and I >> think this Try or TryCatch should do the trick but I think I am missing >> something here. A simple example >> >> >> result_<-list() >> for(i in 1:10){ >> if(inherits(try(sqrt(9-i),silent=TRUE),"try-error")){ >> #If sqrt fails >> result_[[i]]<-0 } else { >> #If sqrt succeeds >> result_[[i]] <- 1 } >> } >> >> >> I would expect this to fail only when i > 9 but succeeds each time. DO i >> need to specify something different where "try-error" resides? Thanks >> guys >> >> Cheers, >> Josh >> >> >> -- >> View this message in context: >> http://r.789695.n4.nabble.com/To-Try-or-to-TryCatch-I-have-tried-to-long-tp4165578p4165578.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> R-help@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. >> > >[[alternative HTML version deleted]]