Thanks for reply @nirgrahamuk
I was working on this issue today and saw a resolution from the googleway package. I believe that I have solved the vast majority, the only thing that is not working is that for the generation of the second map I manually put the coordinates of the two properties ( df2<-google_directions(origin = df[1,2:3], destination = df[2,2:3], mode = "driving")), however, I would like that from the cluster selected in selectInput, the two properties were already recognized to generate the route. Do you happen to know how to adjust this? I'll insert the code and a figure below.
Note: Remember that the API key is required.
library(shiny)
library(rdist)
library(geosphere)
library(shinythemes)
library(leaflet)
library(googleway)
set_key( "API KEY")
function.cl<-function(df,k,Filter1){
#database df
df<-structure(list(Properties = c(1,2,3,4),
Latitude = c(-24.930473, -24.924361,-24.95575,-24.95575),
Longitude = c(-49.994889, -50.004343,-49.990162, -50.007371),
Waste = c(526,350,525,523)), class = "data.frame", row.names = c(NA, -4L))
#clusters
coordinates<-df[c("Latitude","Longitude")]
d<-as.dist(distm(coordinates[,2:1]))
fit.average<-hclust(d,method="average")
clusters<-cutree(fit.average, k)
nclusters<-matrix(table(clusters))
df$cluster <- clusters
#specific cluster and specific propertie
df1<-df[c("Latitude","Longitude")]
df1$cluster<-as.factor(clusters)
df_spec_clust <- df[df$cluster == Filter1,]
#Table to join df and df1
data_table <- Reduce(merge, list(df, df1))
#Color and Icon for map
ai_colors <-c("red","gray","blue")
clust_colors <- ai_colors[df$cluster]
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = clust_colors)
# Map for all clusters:
m1<-leaflet(df1) %>% addTiles() %>%
addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~as.character(df$Properties)) %>%
addLegend( position = "topright", title="Cluster", colors = ai_colors[1:max(df$cluster)],labels = unique(df$cluster))
plot1<-m1
# Map for specific cluster and propertie
df2<-google_directions(origin = df[1,2:3], destination = df[2,2:3],
mode = "driving")
df_routes <- data.frame(polyline = direction_polyline(df2))
m2<-google_map(df_spec_clust) %>%
add_polylines(data = df_routes, polyline = "polyline")
plot2<-m2
return(list(
"Plot1" = plot1,
"Plot2" = plot2,
"Data" = data_table
))
}
ui <- bootstrapPage(
navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
"Cl",
tabPanel("Map of all clusters",
sidebarLayout(
sidebarPanel(
tags$b(h3("Choose the cluster number?")),
sliderInput("Slider", h5(""),
min = 2, max = 2, value = 2),
),
mainPanel(
tabsetPanel(
tabPanel("Solution", (leafletOutput("Leaf1",width = "95%", height = "600")))))
))),
tabPanel("",
sidebarLayout(
sidebarPanel(
selectInput("Filter1", label = h4("Select just one cluster to show"),""),
h4("The distance is (km):"),
textOutput("dist"),
),
mainPanel(
tabsetPanel(
tabPanel("Map of specific cluster", ( google_mapOutput("G2",width = "95%", height = "600")))))
)))
server <- function(input, output, session) {
Modelcl<-reactive({
function.cl(df,input$Slider,input$Filter1)
})
output$Leaf1 <- renderLeaflet({
Modelcl()[[1]]
})
output$G2 <- renderGoogle_map({
Modelcl()[[2]]
})
# output$dist <- renderText({
# Modelcl()[[3]]
# })
observeEvent(input$Slider, {
abc <- req(Modelcl()$Data)
updateSelectInput(session,'Filter1',
choices=sort(unique(abc$cluster)))
})
}
shinyApp(ui = ui, server = server)
