Adding custom ActionButtons to popups in leaflet map within shiny app

若如初见. 提交于 2021-01-29 10:49:57

问题


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)

来源:https://stackoverflow.com/questions/58671227/adding-custom-actionbuttons-to-popups-in-leaflet-map-within-shiny-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!