rgoogleway

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.


Solution

  • 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