Add mouseover event on Leaflet R Shiny

1,323 views
Skip to first unread message

Abdur Rehman Raja

unread,
Feb 17, 2017, 10:07:23 AM2/17/17
to Shiny - Web Framework for R
Hi Guys,

I've made an interactive leaflet map using R Shiny. I was wondering if it was possible to hover over a polygon on the map and it displays certain information inside that polygon. 

Ideally I would like something like this: http://leafletjs.com/examples/choropleth/

Here is my code:

library(shinydashboard)
library(shiny)
library(leaflet.extras)
library(maps)
library(shinyjs)
library(rgdal)

library(leaflet.extras)
library(leaflet)
setwd("D:\\ASMA\\Work\\R scripts\\Dashboard")

global<-function(){
  regionn<-read.csv("region.csv")
  regiongeo<- readOGR(dsn = "region.geojson", layer = "OGRGeoJSON")
  regiongeo<<-regiongeo
  regionn<<-regionn
}

server <- function(input, output) {
output$map = renderLeaflet({
    leaflet() %>% addTiles(urlTemplate = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png") %>% #,options = tileOptions()) %>%
      setView(lng = -79.3832, lat = 43.6532 , zoom = 6, options = list(maxzoom = 5)) %>%
      addPolygons(data = regiongeo, layerId = "region",fillOpacity = 0, weight = 2.5)})

  labels<- paste("Region: " ,regionn$region_cd,"\n",
                 "Maximum CVA: ",regionn$max_cva, sep ="\n")  
  observeEvent(input$map_zoom,{
    proxy<- leafletProxy("map")
    if(input$map_zoom == 6)
    {proxy %>% addPolygons(data = regiongeo , fillOpacity = 0,weight = 2.5,highlightOptions = highlightOptions(
      color='#ff0000', opacity = 1, weight = 5.2, fillOpacity = 0,
      bringToFront = TRUE, sendToBack = TRUE),label=labels,labelOptions= labelOptions(direction = 'auto'))}})

}
 
     
body<-navbarPage("MPAC Analytics", id="nav", 
           tabPanel("Interactive map",
                    div(class="outer",
                        tags$head(
                          includeCSS("styles.css"),
                          includeScript("gomap.js")
                        ),
                        leafletOutput("map", height = "100%",width = "100%"),
                        absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
                                      draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
                                      width = 330, height = "auto",
                                      h2("Analysis Box")))))
ui2 <-body

    
shinyApp(ui = ui2, server = server,onStart=global)
    
Reply all
Reply to author
Forward
0 new messages