R闪烁鼠标悬停行为因addCircles与addCircleMarkers而异

时间:2017-11-14 18:47:44

标签: r shiny leaflet

我有一张地图,根据鼠标悬停坐标绘制图标。但是,当我将标记从addCircles()更改为addCircleMarkers时,它已停止运行。这是一个显示问题的代码段。注释掉addCircles行以查看行为如何变化。

library(shiny)
library(leaflet)

#create test data
dataset <- structure(list(LATITUDE = c(37.09719065, 37.11063138, 37.1132722, 
                                 37.0749196, 37.02980937, 36.98644663, 
                                 36.94062755, 36.89560073, 36.85363852), 
                    LONGITUDE = c(283.6828216, 283.7335099, 283.7892219, 
                                  283.813835, 283.812904, 283.7935855, 
                                  283.7740784, 283.7531182, 283.7280719), 
                    ALTP = c(0.3963670049, 0.8659926907, 1.328491136, 
                             1.855292223, 2.358125869, 2.792399835, 
                             3.24886324, 3.708547366, 4.146854851)), 
                    .Names = c("LATITUDE", "LONGITUDE", "ALTP"), 
                    row.names = c(NA, 9L), 
                    class = "data.frame")


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

# create base map
output$flightpath <- renderLeaflet({
leaflet() %>% addTiles() %>% setView(lng = 283.6828216,
                                              lat = 36.94062755,
                                              zoom = 10)
  })

# add markers - this is where the problem is
observe({
  leafletProxy("flightpath", data = dataset) %>%
     addCircles(weight = 6, radius = 40) 
     #addCircleMarkers(weight = 6, radius = 6)
})


# get map mouseover coordinates
map_lat_lng <- reactive ({
  req(input$flightpath_shape_mouseover)
  map_hover  <- input$flightpath_shape_mouseover
  x <- data.frame(map_hover$lat, map_hover$lng)
  colnames(x) <- c("Latitude", "Longitude")
  x
})

# add icon to map
observe({
  leafletProxy("flightpath", data = map_lat_lng()) %>%
    clearGroup("hover") %>%
    addMarkers(group = "hover")
})
}

ui <- fluidPage(leafletOutput("flightpath"))

shinyApp(ui, server)

任何帮助解决这个问题将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

您正在将其从形状更改为标记,因此您需要更改对象类别:flightpath_shape_mouseover应该变为flightpath_marker_mouseover。那应该解决它。