class of 'try' if error is raised

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

class of 'try' if error is raised

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

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

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

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

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

Thanks, HW

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: class of 'try' if error is raised

Enrico Schumann-2
>>>>> "HW" == Hans W Borchers <[hidden email]> 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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: class of 'try' if error is raised

Bert Gunter-2
In reply to this post by Hans W Borchers-2
See ?try which links you to ?tryCatch for the preferred approach.

Alternatively:  if(inherits(e, "try-error")) ....  ## should work and
satisfy CRAN

-- Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sun, Dec 15, 2019 at 6:21 AM Hans W Borchers <[hidden email]>
wrote:

> I have been informed by CRAN administrators that the development
> version of R issues warnings for my package(s). Some are easy to mend
> (such as Internet links not working anymore), but this one I don't
> know how to avoid:
>
>     Error in if (class(e) == "try-error") { : the condition has length > 1
>
> I understand that `class` can return more than one value. But what
> would be the appropriate way to catch an error in a construct like
> this:
>
>     e <- try(b <- solve(a), silent=TRUE)
>     if (class(e) == "try-error") {
>         # ... do something
>     }
>
> Should I instead compare the class with "matrix" or "array" (or
> both)?. That is, in each case check with a correct result class
> instead of an error?
>
> Thanks, HW
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: class of 'try' if error is raised

Hans W Borchers-2
Yes, CRAN did accept 'if(inherits(e, "try-error"))'.

I remember now, when I used the try-construct the first time, I also
saw tryCatch and found it a bit too extensive for my purposes. Will
look at it again when needed.

Thanks to you and Enrico

On Sun, 15 Dec 2019 at 16:03, Bert Gunter <[hidden email]> wrote:

>
> See ?try which links you to ?tryCatch for the preferred approach.
>
> Alternatively:  if(inherits(e, "try-error")) ....  ## should work and satisfy CRAN
>
> -- Bert
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Sun, Dec 15, 2019 at 6:21 AM Hans W Borchers <[hidden email]> wrote:
>>
>> I have been informed by CRAN administrators that the development
>> version of R issues warnings for my package(s). Some are easy to mend
>> (such as Internet links not working anymore), but this one I don't
>> know how to avoid:
>>
>>     Error in if (class(e) == "try-error") { : the condition has length > 1
>>
>> I understand that `class` can return more than one value. But what
>> would be the appropriate way to catch an error in a construct like
>> this:
>>
>>     e <- try(b <- solve(a), silent=TRUE)
>>     if (class(e) == "try-error") {
>>         # ... do something
>>     }
>>
>> Should I instead compare the class with "matrix" or "array" (or
>> both)?. That is, in each case check with a correct result class
>> instead of an error?
>>
>> Thanks, HW
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.