Search code examples
rshinyr-leaflet

Is it possible to show polygon label when polygon is not clickable?


I need to show a label of polygon (with the name of the region) when moving the mouse over the polygon. However the polygon can not be clickable since I this property for another part of my code. Is this possible?

If this is not possible, another solution could be to write a name on the polygon. I don't know how to do that either.

Here is how my code looks so far

library(shiny)
library(leaflet)

ui <- bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("map", width = "100%", height = "100%")
)

# Load shapefiles
shp_Tajo <- readOGR(dsn = "./Data/ES", layer = "Aqueduct_river_basins_TEJO", verbose = FALSE)

server <- function(input, output, session){ 
output$map <- renderLeaflet({
      # Arguments controlling the looks of the basin polygon
      dopacity = 0.3
      dsmoothFactor = 1
      dfillOpacity = 0.5
      vcolors = c("red")
      leaflet() %>% addTiles() %>% addPolygons(data = shp_Tajo, stroke = F,
opacity = dopacity, smoothFactor = dsmoothFactor, color = vcolors[1], fillColor = vcolors[1], 
fillOpacity = dfillOpacity, highlightOptions = highlightOptions(color = "white", weight = 2, 
bringToFront = FALSE), options = pathOptions(clickable = FALSE), label = "Tajo",
labelOptions = labelOptions(noHide = T, clickable = T, textOnly = TRUE, opacity=0.5,textsize='15px'))
  })
}

The shape file can be downloaded from http://riverbasins.wateractionhub.org/#find_lat_lng , where you have to choose spain in the drop down menu.


Solution

  • By commenting out your pathOptions settings, it works as you intended: you can click anywhere except for the polygon and the label of the polygon is displayed on mouse-over:

    Working example:

    enter image description here

    library("shiny")
    library("rgdal")
    library("leaflet")
    library("ggmap")
    
    ui <- bootstrapPage(
      tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
      leafletOutput("map", width = "100%", height = "100%")
    )
    
    # Load shapefiles
    shp_Tajo <- readOGR(dsn = "./Data/ES", layer = "Aqueduct_river_basins_TEJO", verbose = FALSE)
    
    server <- function(input, output, session){ 
      output$map <- renderLeaflet({
        # Arguments controlling the looks of the basin polygon
        dopacity = 0.3
        dsmoothFactor = 1
        dfillOpacity = 0.5
        vcolors = c("red")
        leaflet() %>% 
          addTiles() %>% 
          addPolygons(
            data = shp_Tajo
            , stroke = FALSE
            , opacity = dopacity
            , smoothFactor = dsmoothFactor
            , color = vcolors[1]
            , fillColor = vcolors[1]
            , fillOpacity = dfillOpacity
            , highlightOptions = highlightOptions(color = "white", weight = 2, bringToFront = FALSE)
            # , options = pathOptions(clickable = FALSE)
            , label = "Tajo"
            , labelOptions = labelOptions(noHide = TRUE, textOnly = TRUE, opacity = 0.5 , textsize='15px'))
      })
    
      observeEvent(input$map_click, {
        click <- input$map_click
        clat <- click$lat
        clng <- click$lng
    
        leafletProxy('map') %>% 
          addCircles(
            lng = clng
            , lat = clat
            , group = 'circles'
            , weight = 1
            , radius = 5000
            , color = 'black'
            , fillColor = 'orange'
            , fillOpacity = 0.5
            , opacity = 1
            )
      })
    }
    
    shinyApp(ui, server)