[R] How to a handle an error in a loop

Berton Gunter gunter.berton at gene.com
Sat May 6 01:01:39 CEST 2006


?try

as in

result<- try (some R expression...)
if (inherits(result,'try-error')) ...do something
else ...do something else


Hope this allows you to get to heaven....

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Farrel 
> Buchinsky
> Sent: Friday, May 05, 2006 3:54 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] How to a handle an error in a loop
> 
> I am about one step away from heaven on earth. I think only one step!
> I am using dgc.genetics to run a TDT test on thousands of 
> genetic loci. I 
> have learnt (through the help of others on this mailing list) 
> to send the 
> complex output to useful data frames which in turn allow me 
> to look at the 
> big picture and screen the thousands of loci.
> 
> Resultdt<-lapply(PGWide[,240:290], tdt)
> the above would do 51 loci at a time. I want to do 6000 all 
> in one shot.
> the only problem is that about once every 100 variables in 
> PGWide there is a 
> locus whose data trips up the tdt function. And when it does, 
> it short 
> circuits the entire loop. Nothing gets written  to Resultdt.
> 
> > Resultdt<-lapply(PGWide[,240:389], tdt)
> Error in rep.default(1, nrow(U)) : rep() incorrect type for 
> second argument
> In addition: Warning messages:
> 1: 1 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, 
> as.allele.pair = 
> TRUE, allow.ambiguous = (parent ==
> 2: 1 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, 
> as.allele.pair = 
> TRUE, allow.ambiguous = (parent ==
> 3: 4 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, 
> as.allele.pair = 
> TRUE, allow.ambiguous = (parent ==
> 
> 
> It is very laborious working out which column it is that 
> caused the error.
> But by narrowing down the range of the looping, one can do 
> it. So eventually 
> I got to:
> 
> > tdt(PGWide[,243])
> Error in rep.default(1, nrow(U)) : rep() incorrect type for 
> second argument
> In addition: Warning message:
> 4 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, 
> as.allele.pair = TRUE, 
> allow.ambiguous = (parent ==
> 
> Do I have to grapple with the code inside the tdt function in 
> order to 
> handle the error (disappointing because I only half 
> understand the R code in 
> it) or is there an easy way to instruct R to simply move onto 
> the next one. 
> I saw something about try() but it got me nowhere. I tried
> Resultdt<-lapply(PGWide[,385:389],try(tdt))
> 
> 
> -- 
> Farrel Buchinsky, MD
> Pediatric Otolaryngologist
> Allegheny General Hospital
> Pittsburgh, PA
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list