Skip to contents

A function to test if I get a response from a URL

Usage

url_exists(x, non_2xx_return_value = FALSE, is_quiet = FALSE)

Arguments

x

the URL to test

non_2xx_return_value

PARAM_DESCRIPTION, Default: FALSE

is_quiet

flag to determine whether print statements are suppressed, TRUE to suppress messages and FALSE to show them, Default: FALSE

Value

returns TRUE if the url exists

Details

gratefully pilfered from https://stackoverflow.com/questions/52911812/check-if-url-exists-in-r

Examples

#EXAMPLE1
some_urls <-
c(
  "https://jimcoll.github.io/a/bad/programmer/",    # Should 404
  "https://github.com/NOAA-OWP/inundation-mapping", # Should exist
  "https://nooneshouldeverhavethisurl.gov/badtest"  # Does not exist in any form
)
data.frame(
  exists = sapply(some_urls, url_exists, USE.NAMES = FALSE),
  some_urls,
  stringsAsFactors = FALSE
) %>% tibble::as_tibble() %>% print()
#> Warning: Requests for [https://jimcoll.github.io/a/bad/programmer/] responded but without an HTTP status code in the 200-299 range
#> # A tibble: 3 × 2
#>   exists some_urls                                     
#>   <lgl>  <chr>                                         
#> 1 FALSE  https://jimcoll.github.io/a/bad/programmer/   
#> 2 TRUE   https://github.com/NOAA-OWP/inundation-mapping
#> 3 NA     https://nooneshouldeverhavethisurl.gov/badtest