Home > Enterprise >  Show the property number and not the cluster number on a map made in shiny
Show the property number and not the cluster number on a map made in shiny

Time:03-11

I have a shiny code that generates clusters of properties and as you can see below, it is displayed on the map made in leaflet. If I place the mouse cursor on the properties, the number of the corresponding cluster appears, but what I wanted was for it to show which property number it is. For example, it is 1, 2, 3, 4, 5, 6 or 7? Can you help me to resolve this?

Executable code below:

library(shiny)
library(rdist)
library(geosphere)
library(shinythemes)
library(leaflet)

function.cl<-function(df,k){
  
  #database df
  df<-structure(list(Properties = c(1,2,3,4,5,6,7), 
                     Latitude = c(-23.8, -23.8, -23.9, -23.9, -23.9,-23.4,-23.5), 
                     Longitude = c(-49.6, -49.3, -49.4, -49.8, -49.6,-49.4,-49.2), 
                     Waste = c(526, 350, 526, 469, 285, 433, 456)), class = "data.frame", row.names = c(NA, -7L))
  
  
  #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 
  df1<-df[c("Latitude","Longitude")]

  #Color and Icon for map
  ai_colors <-c("red","gray","blue","orange","green","beige")
  
  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() %>%
    addMarkers(~Longitude, ~Latitude) %>%
    addAwesomeMarkers(lat=~df$Latitude, lng = ~df$Longitude, icon=icons, label=~as.character(df$cluster)) %>% 
    addLegend( position = "topright", title="Cluster", colors = ai_colors[1:max(df$cluster)],labels = unique(df$cluster))
  
  plot1<-m1
  
  
  return(list(
    "Plot1" = plot1
  ))
}

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      sidebarLayout(
                        sidebarPanel(
                          tags$b(h3("Choose the cluster number?")),
                          sliderInput("Slider", h5(""),
                                      min = 2, max = 5, value = 3),
                        ),
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", (leafletOutput("Leaf1",width = "95%", height = "600")))))
                        
                      ))),
               mainPanel(
               tabsetPanel(
               )))

server <- function(input, output, session) {
  
  Modelcl<-reactive({
    function.cl(df,input$Slider)
  })
  
  output$Leaf1 <- renderLeaflet({
    Modelcl()[[1]]
  })

}

shinyApp(ui = ui, server = server)

Example: enter image description here

CodePudding user response:

All you have to do is to change your leaflet map to:

m1 <- leaflet(df1) %>% addTiles() %>%
    addMarkers(~ Longitude, ~ Latitude) %>%
    addAwesomeMarkers(lat = ~ df$Latitude, lng = ~ df$Longitude, icon = icons, 
                            label = ~ as.character(df$Properties)) %>% 
    addLegend(position = "topright",
                 title = "Cluster", 
                 colors = ai_colors[1:max(df$cluster)],
                 labels = unique(df$cluster))

That is you specify df$Properties as label and not df$cluster.

  • Related