Re: [R] How to check for existence url from within a function?

From: Martin Maechler <>
Date: Wed, 30 May 2007 17:12:10 +0200

>>>>> "Duncan" == Duncan Murdoch <> >>>>> on Sat, 26 May 2007 08:02:11 -0400 writes:

    Duncan> On 26/05/2007 7:13 AM, Heinz Tuechler wrote:
>> Dear All,
>> To check if an url exists, I can use try(). This works, as I expected, if I
>> do it directly, as in the first part of the following example, but I could

    Duncan> .Last.value isn't set until your function returns. You should write this as

    Duncan> con.url <- try(url(url.string, open='rb'))     Duncan> try.error <- inherits(con.url, "try-error")

    Duncan> Notice that I used "inherits", rather than testing for equality.  It's 
    Duncan> documented that the result of try() will be "of class 'try-error'" if an 
    Duncan> error occurs, but there may be circumstances (in the future?) where 
    Duncan> different types of errors are signalled by using a more complicated class.

There's an additional reason for inherits(.,.) and hence against class(<foo>) == "bar" :

Whenever try() does not catch an error, since there was none, i.e., the result of try(foobar(..)) is just foobar(..), foobar(..) may well return an object with an (S3) class vector of length > 1.
In those cases, the equality test returns a logical vector, typically c(FALSE, FALSE)
and using that in if(.....) gives at least a warning.

Martin mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Wed 30 May 2007 - 15:47:45 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 30 May 2007 - 16:31:08 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.