Joe Crozier
Joe Crozier

Reputation: 1036

Drawing driving routes in R

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.

Upvotes: 1

Views: 855

Answers (2)

walkabilly
walkabilly

Reputation: 5

If you break up the function into two parts it's easier to see the error.

    lst_directions <- apply(df_locations, 1, function(x){
    res <- google_directions(
      origin = x[['origin']], 
      destination = x[['destination']]
  )
})

The results create a list and you can see which failed to geocode. When I ran it got the following errors for example.

lst_directions[[1]]

$geocoded_waypoints
  geocoder_status                    place_id               types
1    ZERO_RESULTS                        <NA>                NULL
2              OK ChIJfbgEZoJG5IkRezxu2nfHggk locality, political

$routes
list()

$status
[1] "NOT_FOUND"

One that works has the following output

lst_directions[[2]]

$geocoded_waypoints
  geocoder_status                    place_id               types
1              OK ChIJNYT8S5ha5IkRgDDkgjwJHgc locality, political
2              OK ChIJNYT8S5ha5IkRgDDkgjwJHgc locality, political

$routes
  bounds.northeast.lat bounds.northeast.lng bounds.southwest.lat bounds.southwest.lng            copyrights
1             41.81361            -71.37006             41.81361            -71.37006 Map data ©2024 Google
                                                                                                                                                                                                                                        legs
1 1 m, 0, 1 min, 0, East Providence, RI, USA, 41.8136127, -71.3700603, East Providence, RI, USA, 41.8136127, -71.3700603, 1 m, 0, 1 min, 0, 41.8136127, -71.3700603, Head on <b>Warren Ave</b>, ave~FzmrrL, 41.8136127, -71.3700603, DRIVING
      points    summary warnings waypoint_order
1 ave~FzmrrL Warren Ave     NULL           NULL

$status
[1] "OK"

Can still be a geocoding problem as suggested but at least this helps debug.

Upvotes: 0

SymbolixAU
SymbolixAU

Reputation: 26258

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

Upvotes: 2

Related Questions