[R] How to check for existence url from within a function?
maechler at stat.math.ethz.ch
Wed May 30 17:12:10 CEST 2007
>>>>> "Duncan" == Duncan Murdoch <murdoch at stats.uwo.ca>
>>>>> 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.
More information about the R-help