0

I'm trying to build an app in R shiny where users can learn details about points on a map. When users click on a point, a popup shows some initial details and then an ActionButton (or ActionLink). When the user clicks on that ActionButton, ObserveEvent reactive launches a modal dialog displaying more information about that point. I have the maps, action buttons, and modal dialogs in place. However, I can't figure out how to "assign" each ActionButton to its point/popup on the map. Is there a way to have each ActionButton inherit its ID from its respective point on the map and then pass that information to the ObserveEvent that launches the Modal Dialogue? Working example below:

library(shiny)
library(leaflet)
library(DT)

##Setup##
mapdata <- quakes
mapdata$latitude <- as.numeric(mapdata$lat)
mapdata$longitude <- as.numeric(mapdata$long)

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

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

    observeEvent(input$button_click, {
        showModal(modalDialog(
            title = "More Details",
            renderDataTable({
                df <- mapdata[1,]
                x <- as.data.frame(cbind(colnames(df),t(df)),row.names = F);colnames(x) <- c("Field","Value")
                x
            })
        ))
    })

    output$mymap <- renderLeaflet({
        leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
            addMarkers(lat = ~ latitude, lng = ~ longitude,
                       data = mapdata,
                       popup= ~paste("<b>", mag, "</b></br>", actionLink(inputId = "modal", label = "View Details", onclick = 'Shiny.setInputValue(\"button_click\", this.id, {priority: \"event\"})')))
    })
}

###How can I assign an inputID to each action link that matches the id for that action links point?

shinyApp(ui, server)
user3786999
  • 1,037
  • 3
  • 13
  • 24
  • You could use `input$mymap_marker_click` and save it in a reactiveValue to remember which marker was clicked last. In the given sample data, the markers does not seem to have ids though,... – Tonio Liebrand Nov 02 '19 at 13:05
  • Thanks! That was an idea I had on my list to explore. Can I set the id for markers or circlemarkers equal to the id field in the dataset? – user3786999 Nov 02 '19 at 16:14
  • First you would need an id col in your data. Then you set the layer id with the `layerId` argument for `addMarkers`. Then you would use something like `clicked_point <- reactiveVal(input$mymap_marker_click) print(clicked_point()$id)` to get the point clicked Id – Mxblsdl Oct 21 '22 at 19:50

1 Answers1

1

I left a comment above, but I believe this should be a working example. Also thanks as I was struggling with this question and this led me to the answer.

library(shiny)
library(leaflet)
library(DT)

##Setup##
mapdata <- quakes
mapdata$latitude <- as.numeric(mapdata$lat)
mapdata$longitude <- as.numeric(mapdata$long)
mapdata$id <- 1:nrow(mapdata)

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

server <- function(input, output, session) {
  
  observeEvent(input$button_click, {
    
    clicked_point <- reactiveVal(input$mymap_marker_click)
    showModal(modalDialog(
      title = "More Details",
      renderDataTable({
        df <- mapdata[mapdata['id'] == clicked_point()$id,]
        x <- as.data.frame(cbind(colnames(df),t(df)),row.names = F);colnames(x) <- c("Field","Value")
        x
      })
    ))
  })
  
  output$mymap <- renderLeaflet({
    leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
      addMarkers(lat = ~ latitude, lng = ~ longitude,
                 data = mapdata,
                 layerId = mapdata$id,
                 popup= ~paste("<b>", mag, "</b></br>", actionLink(inputId = "modal", label = "View Details", onclick = 'Shiny.setInputValue(\"button_click\", this.id, {priority: \"event\"})')))
  })
}

shinyApp(ui, server)
Mxblsdl
  • 484
  • 6
  • 16