本文介绍了突出显示由选择项指向的R小叶多边形(无斜率)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在R Shiny应用程序上,是否可以有一个传单地图突出显示由select Item指向的多边形(它应该仅将慕斯移动到列表上方,而无需单击它即可工作)?
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) ?
在下面的可复制示例中,我希望此Shiny应用程序突出显示与鼠标光标位置相对应的多边形,而无需单击它.
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)
非常感谢!
推荐答案
这可以解决问题:
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)
这篇关于突出显示由选择项指向的R小叶多边形(无斜率)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!