The code below generates maps with the leaflet
library. Notice that I generate two maps, the first one shows all the clusters, in this case we have two. And the map further down shows a specific cluster that a given user wants to see. If you place the cursor on the points on the first map, you can see which are the properties of cluster 1 and which are the properties of cluster 2 (see Figure 1). In this case, properties 1 and 4 are from cluster 1 and 2 and 3 for the cluster 2. The code is working fine so far. What I would like to do:
- I would like to route between the properties of the selected cluster, so as I have the
lat
andlong
of property 1 andlat
andlong
of property 4 of cluster 1, for example, I would like to do the route through google maps (see Figure 2). So instead of putting this map 2 withleaflet
of just showing the cluster properties, I would like you to do the route in google maps. Is there any way to do this?
Code executable below:
library(shiny)
library(rdist)
library(geosphere)
library(shinythemes)
library(leaflet)
function.cl<-function(df,k,Filter1){
#database df
df<-structure(list(Properties = c(1,2,3,4),
Latitude = c(-23.8, -23.8, -23.9, -23.9),
Longitude = c(-49.6, -49.3, -49.4, -49.6),
Waste = c(526, 350, 526, 469)), 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
if(nrow(df_spec_clust)>0){
clust_colors <- ai_colors[df_spec_clust$cluster]
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = clust_colors)
m2<-leaflet(df_spec_clust) %>% addTiles() %>%
addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~cluster)
plot2<-m2} else plot2 <- NULL
return(list(
"Plot1" = plot1,
"Plot2" = plot2,
"Data" = data_table
))
}
ui <- bootstrapPage(
navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
"Cl",
tabPanel("",
sidebarLayout(
sidebarPanel(
tags$b(h3("Choose the cluster number?")),
sliderInput("Slider", h5(""),
min = 2, max = 2, value = 2),
),
mainPanel(
tabsetPanel(
tabPanel("Mapa of all clusters", (leafletOutput("Leaf1",width = "95%", height = "600")))))
))),
tabPanel("",
sidebarLayout(
sidebarPanel(
selectInput("Filter1", label = h4("Select just one cluster to show"),""),
),
mainPanel(
tabsetPanel(
tabPanel("Map of specific cluster", (leafletOutput("Leaf2",width = "95%", height = "600")))))
)))
server <- function(input, output, session) {
Modelcl<-reactive({
function.cl(df,input$Slider,input$Filter1)
})
output$Leaf1 <- renderLeaflet({
Modelcl()[[1]]
})
output$Leaf2 <- renderLeaflet({
Modelcl()[[2]]
})
observeEvent(input$Slider, {
abc <- req(Modelcl()$Data)
updateSelectInput(session,'Filter1',
choices=sort(unique(abc$cluster)))
})
}
shinyApp(ui = ui, server = server)