Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leafltet clickable markers do not trigger input$MAPID_marker_click when data is reactive #1

Open
clbenoit opened this issue Mar 2, 2024 · 0 comments

Comments

@clbenoit
Copy link
Owner

clbenoit commented Mar 2, 2024

I don"t get why this code works :

box::use(
  reactable,
  shiny[h3, moduleServer, NS, tagList, fluidRow, column,observe,req,icon,
        uiOutput,renderUI,img,observeEvent],
  leaflet[makeIcon,leafletOutput,renderLeaflet,addTiles,setView,addProviderTiles,addMarkers,leaflet],
  dplyr[`%>%`,filter],
)
#' @export
ui <- function(id) {
  ns <- NS(id)
  tagList(
    fluidRow(column(width = 12,
            leafletOutput(outputId = ns("leafletMap")),
            uiOutput(ns("imageOutput"))
    ))
  )
}

#' @export
server <- function(id, data, session) {
  moduleServer(id, function(input, output, session) {

    data <- data.frame(
       id = c(1, 2, 3),
       latitudeDecimal = c(51.505, 51.51, 51.52),
       longitudeDecimal = c(-0.09, -0.1, -0.12),
       individualCount = c("Marker 1", "Marker 2", "Marker 3")
     )

    output$leafletMap <- renderLeaflet({
      leaflet(data = data) %>%
        addTiles() %>%
        addMarkers(
          ~longitudeDecimal, ~latitudeDecimal,
          icon = icon("location-dot"), popup = ~individualCount, label = ~individualCount,
          layerId = ~id
        )
    })

    ##### THIS WORKS ONLY WHEN THE DATA PROVIDED TO LEAFLET IS NOT REACTIVE I DON'T GET WHY ####
    observeEvent(input$leafletMap_marker_click, {
      # Get the information about the clicked marker
      click_info <- input$leafletMap_marker_click
      print(click_info)

      # Check if a marker was clicked
      if (!is.null(click_info)) {
        # Extract the id of the clicked marker
        clicked_id <- data$id[data$longitudeDecimal == click_info$lng & data$latitudeDecimal == click_info$lat]

        # Print the id to the console (you can use it as needed)
        print(clicked_id)
      }
    })
  })

While this one don't...

box::use(
  reactable,
  shiny[h3, moduleServer, NS, tagList, fluidRow, column,observe,req,icon,
        uiOutput,renderUI,img,observeEvent],
  leaflet[makeIcon,leafletOutput,renderLeaflet,addTiles,setView,addProviderTiles,addMarkers,leaflet],
  dplyr[`%>%`,filter],
)
#' @export
ui <- function(id) {
  ns <- NS(id)
  tagList(
    fluidRow(column(width = 12,
            leafletOutput(outputId = ns("leafletMap")),
            uiOutput(ns("imageOutput"))
    ))
  )
}

#' @export
server <- function(id, data, session) {
  moduleServer(id, function(input, output, session) {

    # data <- data.frame(
    #   id = c(1, 2, 3),
    #   latitudeDecimal = c(51.505, 51.51, 51.52),
    #   longitudeDecimal = c(-0.09, -0.1, -0.12),
    #   individualCount = c("Marker 1", "Marker 2", "Marker 3")
    # )

    output$leafletMap <- renderLeaflet({
      leaflet(data = data()) %>%
      #leaflet(data = data) %>%
        addTiles() %>%
        addMarkers(
          ~longitudeDecimal, ~latitudeDecimal,
          icon = icon("location-dot"), popup = ~individualCount, label = ~individualCount,
          layerId = ~id
        )
    })

    ##### THIS WORKS ONLY WHEN THE DATA PROVIDED TO LEAFLET IS NOT REACTIVE I DON'T GET WHY ####
    observeEvent(input$leafletMap_marker_click, {
      # Get the information about the clicked marker
      click_info <- input$leafletMap_marker_click
      print(click_info)

      # Check if a marker was clicked
      if (!is.null(click_info)) {
        # Extract the id of the clicked marker
        clicked_id <- data$id[data$longitudeDecimal == click_info$lng & data$latitudeDecimal == click_info$lat]

        # Print the id to the console (you can use it as needed)
        print(clicked_id)
      }
    })
  })

So I skip displayed picture from clickable markers for now... 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant