[R] Error handling

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Jul 9 18:33:36 CEST 2002



Ernesto Jardim wrote:
> 
> Hi
> 
> I don't want to know if it runs without errors, I want it to keep
> working even if an error occurs.

And that's try() for!
As in 
  try(log(vec[i]))
in your example.

Uwe Ligges


> Thanks
> 
> EJ
> 
> On Tue, 2002-07-09 at 17:02, Uwe Ligges wrote:
> >
> >
> > Ernesto Jardim wrote:
> > >
> > > Hi
> > >
> > > I wrote a function implementing an algoritm for simulation of spatial
> > > sampling plans (using geoR).
> > >
> > > I'm using a range of parameters to perform several simulation and
> > > sometimes this parameters create function errors wich cause the algoritm
> > > to stop.
> > >
> > > How can I avoid this ?
> > >
> > > Below there's a small example that I made to study the problem but I was
> > > not able to solve it. I've tried "restart" function but it caused an
> > > endeless loop.
> > >
> > > All help is wellcome.
> > >
> > > > # function
> > > >
> > > > fun.test<-function(vec){
> > > + for(i in 1:length(vec)){
> > > + a <- log(vec[i])
> > > + cat(vec[i],a,"\n")
> > > + }
> > > + }
> > > >
> > > > # case 1
> > > >
> > > > vec<-c(2,4,6)
> > > >
> > > > fun.test(vec)
> > > 2 0.6931472
> > > 4 1.386294
> > > 6 1.791759
> > > >
> > > > # case 2
> > > >
> > > > vec<-c(2,4,"a",6)
> > > >
> > > > fun.test(vec)
> > > Error in log(x) : Non-numeric argument to mathematical function
> > >
> > > I want that in case 2 the function jumps "a" and continues giving a
> > > similar result to case 1.
> >
> > See ?try.
> >
> > Uwe Ligges
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list