0

i'm trying to run a API request for a number of parameters with the lapply function in R.

However, when i run this function, i get the error " Error in file(con, "r") : cannot open the connection"

Google suggests using setInternet2(TRUE) to fix this issue, however, i get the error: Error: 'setInternet2' is defunct. See help("Defunct"

localisedDestinationNameForGivenLang <- function (LocationId) {
 gaiaURL <- paste0("https://URL/",LocationId, "?geoVersion=rwg&lcid=", "1036", 
"&cid=geo&apk=explorer")
 print(LocationId)
 localisation <- fromJSON(gaiaURL)
}

lapply(uniqueLocationId, localisedDestinationNameForGivenLang)

Can someone suggest a fix please?

Tim496
  • 162
  • 3
  • 19
  • pls provide example of actual code you are trying to run and which line fails. – 93i7hdjb Mar 05 '18 at 16:05
  • @ErikKornet I've updated the description, thanks! – Tim496 Mar 05 '18 at 16:39
  • looks like some iteration of what gets stored in `gaiaURL` isn't connectable. Can you tell if any of your `LocationID` values are working, and/or which one isn't? – 93i7hdjb Mar 05 '18 at 16:54
  • @ErikKornet - you're correct, some of the LocationID's produce a 400 error. As my list of ID's ~ 17,000 and they're not obviously incorrect without calling the API do you know how to fix this? Maybe tryCatch()? – Tim496 Mar 06 '18 at 10:59
  • will post response as an answer – 93i7hdjb Mar 06 '18 at 14:45

1 Answers1

0

Here's a sample of how you could identify which sites are throwing errors while still getting response from the ones that don't:

urls = c("http://citibikenyc.com/stations/test", "http://citibikenyc.com/stations/json")

grab_data     <-    function(url) {
  out <- tryCatch(
    {fromJSON(url)}, 
    error=function(x) {
      message(paste(url, x))
      error_msg = paste(url, "threw an error")
      return(error_msg)
    })
    return(out)
}

result <- lapply(urls, grab_data)

result will be a list that contains API response for urls that work, and error msg for those that don't.

93i7hdjb
  • 1,136
  • 1
  • 9
  • 15