[R] class of 'try' if error is raised

Enrico Schumann e@ @end|ng |rom enr|co@chum@nn@net
Sun Dec 15 15:30:22 CET 2019


>>>>> "HW" == Hans W Borchers <hwborchers using gmail.com> writes:

  HW> I have been informed by CRAN administrators that the development
  HW> version of R issues warnings for my package(s). Some are easy to mend
  HW> (such as Internet links not working anymore), but this one I don't
  HW> know how to avoid:

  HW>     Error in if (class(e) == "try-error") { : the condition has length > 1

  HW> I understand that `class` can return more than one value. But what
  HW> would be the appropriate way to catch an error in a construct like
  HW> this:

  HW>     e <- try(b <- solve(a), silent=TRUE)
  HW>     if (class(e) == "try-error") {
  HW>         # ... do something
  HW>     }

  HW> Should I instead compare the class with "matrix" or "array" (or
  HW> both)?. That is, in each case check with a correct result class
  HW> instead of an error?

  HW> Thanks, HW


You should probably use

   if (inherits(e, "try-error")) {
       # ... do something
   }

kind regards
    ENrico

-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net



More information about the R-help mailing list