Highlight R leaflet polygon pointed by select Item (without cliking on it)

早过忘川 提交于 2019-12-11 02:47:37

问题


On R shiny app, is it possible to have a leaflet map that highlights polygons pointed by select Item (it should work just moving the mouss above the list and without having clicking on it) ?

In the following reproductible example, I would like this Shiny app to highlight the polygon corresponding to the mouse cursor location but without having to click on it.

library(shiny)
library(shinyjs)
library(leaflet)
library(sf)

download.file(url = "http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip", destfile = "TM_WORLD_BORDERS-0.3.zip")
unzip( zipfile = "TM_WORLD_BORDERS-0.3.zip" )

world.borders <-read_sf( dsn = getwd(), layer = "TM_WORLD_BORDERS-0.3" )
world.borders <- world.borders[world.borders$NAME %in% c("Australia","United States","Brazil","Ireland","India","Kenya"),]

server <- function(input, output, session) {

output$mymap <- renderLeaflet({
    leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
       addPolygons( data = world.borders, fill =  "#D24618", color = "blue")
 }) 
}

 ui <- fluidPage(
     leafletOutput("mymap"),
     selectInput(inputId = "country_choice",label = "Select a country",choices = unique(world.borders$NAME))
)

shinyApp(ui, server)

Thanks a lot !


回答1:


That should do the trick:

library(shiny)
library(shinyjs)
library(leaflet)
library(sf)

### Note had to download by hand as this did not work
## download.file(url = "http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip", 
##               destfile = "TM_WORLD_BORDERS-0.3.zip")
## unzip( zipfile = "TM_WORLD_BORDERS-0.3.zip" )

world.borders <- read_sf( dsn = getwd(), layer = "TM_WORLD_BORDERS-0.3" )
world.borders <- world.borders[world.borders$NAME %in% 
                               c("Australia", "United States", "Brazil", 
                                 "Ireland", "India", "Kenya"), ]

ui <- fluidPage(
  useShinyjs(),
  leafletOutput("mymap"),
  selectInput(inputId = "country_choice",
              label   = "Select a country",
              choices = c("Please Select..." = "", unique(world.borders$NAME)))
)

server <- function(input, output, session) {
runjs(glue::glue("$('.selectize-control').on('mouseenter', ",
                 "'.selectize-dropdown-content div', ",
                 "function() {{",
                 "    Shiny.setInputValue('selected', $(this).data('value'));}}); ",
                 "$('.selectize-control').on('mouseleave', ",
                 "'.selectize-dropdown-content div', ",
                 "function() {{",
                 "    Shiny.setInputValue('selected', null);}})"))

  output$mymap <- renderLeaflet({
    myBorders <- world.borders[world.borders$NAME == input$selected, ]
    leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
      addPolygons(data = myBorders, fill =  "#D24618", color = "blue")
  }) 
}

shinyApp(ui, server)


来源:https://stackoverflow.com/questions/52316235/highlight-r-leaflet-polygon-pointed-by-select-item-without-cliking-on-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!