问题
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