我需要解決此問題的解決方法。 我在彈出窗口中有一個提交按鈕。當我點擊標記時,輸入$ map_marker_click $ id現在包含被點擊的標記的ID。單張彈出窗口中的提交按鈕不會觸發observeEvent閃亮
彈出窗口中有一個提交按鈕。當點擊提交按鈕時,我想將輸入$ map_marker_click $ id保存到變量中。使用oberserveEvent或eventReactive都不適合我。
下面是代碼保存爲app.R
library(shiny)
library(leaflet)
df <- data.frame("id" = c("1", "2"),
"lng" = c(-93.65, -93.655),
"lat" = c(42.0285, 42.03),
"Text" = c("Department of Statistics", "something else"))
ui <- fluidPage(
leafletOutput("map"),
textOutput("locationid1"),
textOutput("locationid2")
)
server <- function(input, output, session) {
output$map <- renderLeaflet({
df %>% leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
setView(-93.65, 42.0285, zoom = 15) %>%
addMarkers(layerId = ~id, popup = ~paste("<b>", Text, "</b></br>
<button id='selectlocation' type='button' class='btn btn-default action-button'>Select Location</button>"))
})
id1 <- reactive({
validate(
need(!is.null(input$map_marker_click), "Please select a location from the map above")
)
input$map_marker_click$id
})
id2 <- eventReactive(input$selectlocation, {
input$map_marker_click$id
})
output$locationid1 <- renderText({paste("Location Selected using marker click:", id1())})
output$locationid2 <- renderText({paste("Location Selected using popup select button click:", id2())})
}
shinyApp(ui, server)
請注意,您正在創建具有相同ID的按鈕,ID對於HTML元素應該是唯一的,因此您可以將標記號添加到ID中。 – NicE