Closed MikeJohnPage closed 4 years ago
Thanks for spotting this issue. I will work on it over the weekend. Feel free to send a PR if you managed to fix it yourself.
Are you using CRAN or GitHub version?
There are still some changes that haven't been pushed to CRAN so the dev (GitHub) version is the latest.
I don't get the error that you mentioned when running PostcodesioR::postcode_lookup("IM26BG")
This line works for me as expected using the dev version.
Was that the only postcode that caused errors? Did you hit the query limit (postcodes.io limits the number of calls)?
In response to your questions:
map
functions from the {purrr} package to iterate over a vector of postcodes one-by-one.In case you are interested I solved the problem by using a combination of functions from {purrr} to ignore errors:
# Load libraries
library(tidyverse)
library(PostcodesioR)
# Create vector of postcodes
postcodes <- c("TEST123", "TEST456", "TEST789")
# Geocoding function to check if postcode is valid, and if so, return lat/lng,
# else silently ignore
geocode <- function(postcode){
if(postcode_validation(postcode)){
p <- postcode_lookup(postcode)
tibble(postcode = postcode,
latitude = purrr::pluck(p, "latitude"),
longitude = purrr::pluck(p, "longitude"))
}
}
# Handle errors safely
safe_geo <- safely(geocode)
# Return lat/lng coords
coords <- map(postcodes, safe_geo) %>%
transpose() %>%
use_series("result") %>%
bind_rows()
I updated to the GitHub dev version and indeed it no longer throws an error for the same postcode. Thanks for the update 👍
For some postcodes, I think the
postcode_lookup()
function is failing in (what I think) is an unexpected way.Take the postcode "IM26BG" as an example. The postcode appears to be valid:
However, the
postcode_lookup
function fails in R:Passing the postcode into the 'Lookup a postcode' API endpoint on the official postcodes.io site reveals that this postcode returns
null
in many of the fields:I believe this explains the R error, as it appears the
lookup_postcode()
function is failing when it finds null values (creatingdiffering number of rows
).I think the
lookup_postcode()
function should convert null values to NA or fail with a more informative error message.