How can I make use of leaflet's addLabelOnlyMarkers(), labelOptions() argument interactive = TRUE? I do not have experience integrating JS with R.
If I can't, I have 2 roundabout options.
Use the Shiny Leaflet input$MAPID_click event to find the closest possible POINT, and use that to drive interaction.
Make the label into a marker? Then just use addMarkers() instead of addLabelOnlyMarkers(), and input$MAPID_marker_click to drive interaction.
vedoa
April 16, 2024, 5:50pm
2
Hi @its.me.adam ,
this should get you going:
library(shiny)
library(leaflet)
points <- data.frame(lng = rnorm(40) * 2 + 13,
lat = rnorm(40) + 48,
label = letters[1:40]
)
ui <- fluidPage(
leafletOutput("itsmeadam")
)
server <- function(input, output, session) {
output$itsmeadam <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addLabelOnlyMarkers(data = points,
lng = ~lng,
lat = ~lat,
label = ~label,
labelOptions = labelOptions(noHide = T,
direction = 'top',
textOnly = T,
interactive = TRUE)
)
})
observeEvent(input$itsmeadam_marker_click, {
click <- input$itsmeadam_marker_click
text<-paste("Lattitude ", click$lat, "Longtitude ", click$lng)
proxy <- leafletProxy("itsmeadam")
proxy %>%
clearPopups() %>%
addPopups(click$lng, click$lat, text)
})
}
shinyApp(ui, server)
Oh dear! I assumed input$MAPID_marker_click would only be for addMarkers().
What if I have both addMarkers() and addLabelOnlyMarkers()? How do I signal that I have clicked one instead of the other?
vedoa
April 18, 2024, 4:05pm
4
They would be layered one over another - so it would not make a real difference - i would not use both. But if you want to have the markers use the addMarkers() option label and it will show up when you hover over.