I am working with google-way in a Shiny app.
I started from this example: Drawing journey path using leaflet in R and modified it a little to include 2 extra waypoints between the Destination and Origin and have google_directions render the optimal route between them.
The route plotted is indeed optimal and I am able to replicate this for 5-6-7 or 10 waypoints. However in each case for the route to be plotted I need to put addresses for all waypoints. If some of the waypoints are left blank the app returns an error and I'm not sure how to solve this. I suppose some kind of "if" operator should be used but not sure how and where exactly.
This is my code:
library(shiny)
library(googleway)
ui <- navbarPage("APP NAME", position = c("static-top"),
tabPanel("MAP",
google_mapOutput(outputId = "mapWarsaw"),
textInput(inputId = "origin", label = "Departure point"
),
textInput(inputId = "waypoint", label = "Waypoint 1"
),
textInput(inputId = "waypoint2", label = "Waypoint 2",
),
textInput(inputId = "destination", label = "Destination point"
),
actionButton(inputId = "getRoute", label = "Get Route")
)
)
server <- function(input, output, session) {
map_key <- '<APIKEY>'
api_key <- '<APIKEY>'
df_route <- eventReactive(input$getRoute,{
print("getting route")
o <- input$origin
w <- input$waypoint
q <- input$waypoint2
d <- input$destination
return(data.frame(origin = o, waypoint = w, waypoint2 = q, destination = d, stringsAsFactors = F))
})
output$mapWarsaw <- renderGoogle_map({
df <- df_route()
print(df)
if(df$origin == "" | df$waypoint == ""| df$waypoint2 == "" | df$destination == "")
return()
res <- google_directions(key = api_key,
origin = df$origin,
waypoints = list(stop = df$waypoint,
stop = df$waypoint2),
destination = df$destination,
optimise_waypoints = TRUE,
mode = "driving")
df_route <- data.frame(route = res$routes$overview_polyline$points)
google_map(key = map_key, search_box = TRUE, scale_control = TRUE, height = 1000) %>%
add_traffic()%>%
add_polylines(data = df_route,
polyline = "route",
stroke_colour = "#FF33D6",
stroke_weight = 7,
stroke_opacity = 0.7,
info_window = "New route",
load_interval = 100)
})
}
shinyApp(ui, server)
Data file:
popupcontent;label;lat;lng
Location 1;Location Label 1;52.2572126;20.9845778
Location 2;Location Label 2;52.2423875;21.0244197
Location 3;Location Label 3;52.238209;21.0202826
Location 4;Location Label 4;52.23048060000001;21.0108891
Location 5;Location Label 5;52.231755;21.006482
Location 6;Location Label 6;52.2376719;21.0158829A7
Location 7;Location Label 7;52.2540379;21.0347079
Location 8;Location Label 8;52.2550605;21.052299
Location 9;Location Label 9;52.247861;21.049922
Location 10;Location Label 10;52.2720819;21.0151038
Location 11;Location Label 11;52.2582368;21.0396915
Location 12;Location Label 12;52.2569792;21.0294134
Location 13;Location Label 13;52.2549106;21.0475752
Location 14;Location Label 14;52.206521;20.999852
Location 15;Location Label 15;52.2937449;21.0333861
Location 16;Location Label 16;52.2878479;21.0412171
Location 17;Location Label 17;52.305039;21.0584014