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

From: Heinz Tuechler <tuechler_at_gmx.at>
Date: Sat, 26 May 2007 16:51:00 +0100

Thank you, Duncan, especially for the hint concerning "inherits".

Heinz

At 08:02 26.05.2007 -0400, Duncan Murdoch wrote:
>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
>> not find a way to do it from within a function, as in the second part.
>>
>> Where could I find information on how to do this?
>>
>> Thanks,
>> Heinz
>>
>>
>> ## set nonexisting url
>> url.string <- 'http://www.google.at/nonexist.html'
>>
>> ## first part
>> 1 # to start with defined .Last.value
>> try(con.url <- url(url.string, open='rb'))
>> class.try.res <- class(.Last.value)
>> try.error <- class.try.res== 'try-error'
>> print(try.error) # TRUE
>> try(close(con.url))
>>
>> ## try() within a function
>> url.error <- function(url.string) {
>> 1 # to start with defined .Last.value
>> try(con.url <- url(url.string, open='rb'))
>> class.try.res <- class(.Last.value)
>> try.error <- class.try.res== 'try-error'
>
>.Last.value isn't set until your function returns. You should write this as
>
>con.url <- try(url(url.string, open='rb'))
>try.error <- inherits(con.url, "try-error")
>
>Notice that I used "inherits", rather than testing for equality. It's
>documented that the result of try() will be "of class 'try-error'" if an
>error occurs, but there may be circumstances (in the future?) where
>different types of errors are signalled by using a more complicated class.
>
>Duncan Murdoch
>
>> print(try.error)
>> try(close(con.url))
>> invisible(try.error)
>> }
>>
>> ## call the function
>> url.error(url.string) # result -> FALSE
>>
>> ______________________________________________
>> R-help_at_stat.math.ethz.ch mailing list
>> 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.
>



R-help_at_stat.math.ethz.ch mailing list
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. Received on Sat 26 May 2007 - 14:57:48 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 Sat 26 May 2007 - 17:31:01 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.