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

Heinz Tuechler tuechler at gmx.at
Sat May 26 13:13:15 CEST 2007


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'
  print(try.error)
  try(close(con.url))
  invisible(try.error)
}

## call the function
url.error(url.string)   # result -> FALSE



More information about the R-help mailing list