1

I'm following the answer provided in this question: Drawing journey path using leaflet in R

And hoping to replicate the same idea: drawing a map showing driving routes between cities.

For reference, when I simply copy and paste the code from SymbolixAu's answer... works great! (The simple map that is, the googe_map, not the "shiny" code).

So in other words, I think I got my api key's set up well. But for some reason, when I try to use the same code on my data of locations, it doesn't work. Here is my code:

df_locations<-structure(list(origin = c("WARWICK", "EAST PROVIDENCE", "WARREN", 
                      "CENTERDALE", "CENTRAL FALLS", "DAVISVILLE", "NORTH PROVIDENCE", 
                      "EAST PROVIDENCE", "PROVIDENCE", "CHEPACHET"), destination = c("CENTERDALE", "EAST PROVIDENCE", "BRISTOL", "JOHNSTON", "CRANSTON", "WARWICK","NORTH PROVIDENCE", "EAST PROVIDENCE", "WARREN", "CHEPACHET")), class = "data.frame", row.names = c(NA, -10L))


## loop over each pair of locations, and extract the polyline from the result
lst_directions <- apply(df_locations, 1, function(x){
res <- google_directions(
key = api_key
, origin = x[['origin']]
, destination = x[['destination']]
)

df_result <- data.frame(
origin = x[['origin']]
, destination = x[['destination']]
, route = res$routes$overview_polyline$points
)
return(df_result)
})

## convert the results to a data.frame
df_directions <- do.call(rbind, lst_directions)

## plot the map
google_map(key = map_key ) %>%
add_polylines(data = df_directions, polyline = "route")

Right when I get to the "apply" loop, I receive the error "Error in data.frame(origin = x[["origin"]], destination = x[["destination"]], : arguments imply differing number of rows: 1, 0"

When I re-run the exact same code, but simply use the example data frame:

df_locations <- data.frame(
origin = c("Melbourne, Australia", "Sydney, Australia")
, destination = c("Sydney, Australia", "Brisbane, Australia")
, stringsAsFactors = F
)

It works perfectly. Any ideas? I was wondering if its because my locations only have city names (no states or countries), so I tweaked the example data to only say "Melbourne" or "Sydney", still worked.

Joe Crozier
  • 944
  • 8
  • 20
  • It is possible that the function: `google_directions()` is not returning a valid response for some or all of your locations. – Dave2e Jan 16 '20 at 18:38
  • @Dave2e While that is entirely possible, when I previously had the "geocode" function find the lat and long coordinates for one of the columns, it worked great. Would they be that different? How could I even check? – Joe Crozier Jan 16 '20 at 18:43
  • The error is indicating that one of your vectors to the `data.frame` is of zero length. The "res", the output from `google_directions()` seems like a likely source. As a quick debug, I would try adding `print(res)` to the loop and attempt to reproduce. If there is a bad value, maybe a network timeout, or a bad input to the function. – Dave2e Jan 16 '20 at 19:05
  • @Dave2e So what's weird is: I just tried your print(res) code. First, I did it with the sample data to see what I should be expecting, worked great. Then, I tried to use it on the "real" data I have with hundreds of rows, and it was like drinking from a fire hose, tons of info coming across screen and I couldn't find the problem in like pages of info thrown at me (looked like lots of good google directions) before I got same error, so then I tried the print(res) with the sample data from above, and the error was thrown at me before ANY directions were shown. I.e it behaved differently. – Joe Crozier Jan 16 '20 at 19:24
  • 1
    It is interesting that it behaves differently. There is one more possible cause, some web APIs will limit the number of allowable calls (per day or per second). Inserting a slight delay with `Sys.sleep( )` might help. Add an if statement around the print to only print the information when res is NA. – Dave2e Jan 16 '20 at 19:35

1 Answers1

2

For what it's worth, I just ran your code without any issues. So as @Dave2e suspects they may be something wrong with your API calls, either timing-out or google not returning a result for some reason. So you'll have to keep debugging / printing the results to see if this is the case.

df_locations<-structure(list(origin = c("WARWICK", "EAST PROVIDENCE", "WARREN", 
                                        "CENTERDALE", "CENTRAL FALLS", "DAVISVILLE", "NORTH PROVIDENCE", 
                                        "EAST PROVIDENCE", "PROVIDENCE", "CHEPACHET"), destination = c("CENTERDALE", "EAST PROVIDENCE", "BRISTOL", "JOHNSTON", "CRANSTON", "WARWICK","NORTH PROVIDENCE", "EAST PROVIDENCE", "WARREN", "CHEPACHET")), class = "data.frame", row.names = c(NA, -10L))

library(googleway)

set_key("MYKEY")


## loop over each pair of locations, and extract the polyline from the result
lst_directions <- apply(df_locations, 1, function(x){
  res <- google_directions(
    origin = x[['origin']]
    , destination = x[['destination']]
  )

  df_result <- data.frame(
    origin = x[['origin']]
    , destination = x[['destination']]
    , route = res$routes$overview_polyline$points
  )
  return(df_result)
})

df_directions <- do.call(rbind, lst_directions)

## plot the map
google_map() %>%
  add_polylines(data = df_directions, polyline = "route")

enter image description here

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139