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

From: Farrel Buchinsky <fjbuch_at_gmail.com>
Date: Sun 07 May 2006 - 04:11:54 EST

"Berton Gunter" <gunter.berton@gene.com> wrote in message news:008601c67097$de1b46e0$5bc4fea9@gne.windows.gene.com...
> ?try
>
> as in
>
> result<- try (some R expression...)
> if (inherits(result,'try-error')) ...do something
> else ...do something else

No heaven on earth yet.

how would I incorporate this kind of functionality into Resultdt<-lapply(PGWide[,240:389], tdt)

everything would have to be built into the tdt spot in the above statement. How does one get the if...else in there? Does one have to do that as one would program a function or could one write the if...else right into "Resultdt<-lapply(PGWide[,240:389], tdt)"

This works
> for (few in c(9,10,11,12,243,20)) if
> (inherits(try(tdt(PGWide[,few])),'try-error')) print("messed up") else
> print("works")

[1] "works"
[1] "works"
[1] "works"
[1] "works"

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: 2 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, as.allele.pair = TRUE, allow.ambiguous = (parent ==
3: 2 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, as.allele.pair = TRUE, allow.ambiguous = (parent ==
4: 4 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, as.allele.pair = TRUE, allow.ambiguous = (parent ==
[1] "messed up"
[1] "works"
Warning message:
1 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, as.allele.pair = TRUE, allow.ambiguous = (parent ==

BUT THIS DOES NOT lapply(PGWide[,c(9,10,11,12,,243,20)], if (inherits(try(tdt),'try-error') print("messed up") else print("works"))
Error: syntax error in "lapply(PGWide[,c(9,10,11,12,,243,20)], if (inherits(try(tdt),'try-error') print"

Any idea why...can it be that one cannot have multiple commands on one line
> p=7 f=8

Error: syntax error in "p=7 f"

in the lapply, how would R know that I was sending the list to tdt?

-- 
Farrel Buchinsky, MD
Pediatric Otolaryngologist
Allegheny General Hospital
Pittsburgh, PA

______________________________________________
R-help@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
Received on Sun May 07 04:16:10 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 07 May 2006 - 14:10:00 EST.

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