munterfi / hereR

R package that provides an interface to the HERE REST APIs: Geocoder API, Routing API, Traffic API, Public Transit API and Destination Weather API. Locations and routes are returned as 'sf' objects.
https://munterfi.github.io/hereR/
GNU General Public License v3.0
89 stars 11 forks source link

Feature: Add a robust argument to hereR::isoline. #118

Closed nbanion closed 3 years ago

nbanion commented 3 years ago

Sometimes hereR::isoline fails for specific cases in poi, causing an error. Consider adding a robust argument that allows the function to return values for the successful isolines and NAs for the unsuccessful ones. This argument would take a logical and default to FALSE.

I often find myself using this pattern:

library(dplyr)
library(hereR)
library(purrr)
library(tidyr)

# Failproof version of isoline (returns just the geometry for use in mutate).
safely_isoline <- function(...) {
  fxn <- safely(isoline)
  value <- fxn(...)
  if (is.null(value$error)) value$result$geometry else NA
}

# A sf object with points.
my_sf <- ... 

# Example usage: Replace some point geometries with isoline geometries.
my_sf %>%
  group_by(id) %>%           # Some unique identifier.
  nest(data = geometry) %>%  # A sfc with point geometries.
  mutate(geometry = map(data, safely_isoline)) %>%
  select(-data) %>% unnest(geometry) %>%
  ungroup()

The failing cases may deserve a bug report of their own. I haven't figured out the cause; all I know is that it seems to happen for specific cases when combined with specific function arguments. This issue is only concerned with moving past these failures.

munterfi commented 3 years ago

Hi, thanks for reporting.

Unfortunately I don't quite understand the requested feature, and therefore try to narrow it down to two cases:

In the first case the option with a boolean robust argument seems to be a reasonable option, which would replace the missing rows (NULL) with a row where every value is NA, except from the request id. In the second case we should rather try to fix the error in the function. If it is the second case, do you have a reproducible example? In your example code POIs are missing, which trigger this error.

nbanion commented 3 years ago

Let me put together a reproducible example before you invest more effort.

munterfi commented 3 years ago

Closing in favor of #132.