[R] Error handling

Corey Moffet cmoffet at nwrc.ars.usda.gov
Tue Jul 9 20:42:47 CEST 2002


I don't know if this helps.

This soulution will give some warnings:

# function

fun.test<-function(vec){
  for(i in 1:length(vec)){
    b <- as.numeric(vec[i])
    if (!is.na(try(log(b)))) {
      cat(b,log(b),"\n")
    }
  }
}

# case 1
vec<-c(2,4,6)
fun.test(vec)

# case 2
vec<-c(2,4,"a",6)
fun.test(vec)



At 04:23 PM 7/9/2002 +0100, 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.
>
>Thanks
>
>EJ
>
>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
.-.-
>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
>_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
._._
>
With best wishes and kind regards I am

Sincerely,

Corey A. Moffet
Support Scientist

University of Idaho
Northwest Watershed Research Center
800 Park Blvd, Plaza IV, Suite 105
Boise, ID 83712-7716
(208) 422-0718
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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