Re: [R] How to say "if error"

From: Joris Meys <jorismeys_at_gmail.com>
Date: Thu, 24 Jun 2010 15:49:24 +0200

An old-fashioned and I guess also advised-against method would be to use try() itself, eg :

set.seed(1)

x <- rnorm(1:10)
y <- letters[1:10]
z <- rnorm(1:10)

for (i in list(x,y,z)){
  cc <- try(sum(i), silent=T)
  if(is(cc,"try-error")) {next}
  print(cc)
}

Put silent=F if you want to see the error methods. See also ?try (and ?is ) Cheers
Joris

On Thu, Jun 24, 2010 at 3:34 PM, Duncan Murdoch <murdoch.duncan_at_gmail.com> wrote:
> On 24/06/2010 7:06 AM, Paul Chatfield wrote:
>>
>> I've had a look at the conditions in base and I can't get the ones to work
>> I've looked at but it is all new to me.
>> For example, I can work the examples for tryCatch, but it won't print a
>> finally message for me when I apply it to my model.  Even if I could get
>> this to work, I think it would still cause a break e.g.
>> for (j in 1:10)
>> {tryCatch(ifelse(j==5, stop(j), j), finally=print("oh dear"))}
>>
>> Thanks for the suggestion though - any others?
>>
>
> I think you don't want to use finally, which is just code that's guaranteed
> to be executed at the end.  You want to catch the errors and continue.  For
> example,
>
> for (j in 1:10)
> { tryCatch(ifelse(j==5, stop(j), print(j)), error=function(e) {print("caught
> error"); print(e)}) }
>
> Duncan Murdoch
>
> ______________________________________________
> 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.
>

-- 
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
Joris.Meys_at_Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

______________________________________________
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.
Received on Thu 24 Jun 2010 - 13:51:25 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 24 Jun 2010 - 14:50:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive