R Shiny with leaflet:单击图标创建模态窗口

时间:2017-07-20 21:39:17

标签: r shiny leaflet

当我点击有光泽的传单地图中的图标时,我想创建一个模态窗口。这可行吗?我尝试了下面的代码,但 bsModal 没有做任何事情。

AND CUST_ID NOT IN (--here you drop in query 2--)

enter image description here

1 个答案:

答案 0 :(得分:2)

我会发布两个可能的解决方案。第一个是我认为最适合您需求的解决方案,第二个更适合您当前的代码。希望这有帮助!

解决方案1:

library(shiny)
library(leaflet)

points <- data.frame(cbind(id=seq(1,40),latitude = rnorm(40) * 2 + 13, longitude  = 
                             rnorm(40) + 48))


ui <- fluidPage(
  leafletOutput("mymap"),
  actionButton("action1","Show modal")
)

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

  observeEvent(input$mymap_marker_click, {
    id = input$mymap_marker_click$id
    showModal(modalDialog(
      title = "You selected a marker!",
      paste0("ID: ", id, ", lat: ", round(points$latitude[id==id],2),", lon: ", round(points$longitude[id==id],2))
    ))
  })

  output$mymap <- renderLeaflet({
    leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
      addMarkers(layerId =  ~ id,lat = ~ latitude, lng = ~ longitude,
                 data = points
      )
  })
}

shinyApp(ui, server)

解决方案2:

library(shiny)
library(leaflet)
library(shinyBS)

points <- data.frame(cbind(latitude = rnorm(40) * 2 + 13, longitude  = 
                             rnorm(40) + 48))


ui <- fluidPage(
  leafletOutput("mymap"),
    actionButton("action1","Show modal")
)

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

  observeEvent(input$button_click, {
    showModal(modalDialog(
      title = "Important message",
      "This is an important message!"
    ))
  })

  output$mymap <- renderLeaflet({
    leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
      addMarkers(lat = ~ latitude, lng = ~ longitude,
                 data = points,
                 popup= ~paste("<b>", latitude, longitude, "</b></br>", actionButton("showmodal", "Show modal", onclick = 'Shiny.onInputChange(\"button_click\",  Math.random())')))
  })
}

shinyApp(ui, server)