R Shiny with elevator: Create modal when icon is clicked

I want to create a modal window when I click the icon on the booklet card in shiny shape. Is it doable? I've tried the code below but bsModal doesn't do anything.

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

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


ui <- fluidPage(
  leafletOutput("mymap"),
    bsModal("modalExample", "This will open a modal", "assign_task", size = 
      "large",
    HTML(""))
    )

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



   output$mymap <- renderLeaflet({
   leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
    addMarkers(lat = ~ latitude, lng = ~ longitude,
             data = points,
             popup=~ sprintf(
               '<button type="button" id="assign_task">Open Modal </button>'
             ))
   })
  }

shinyApp(ui, server)

      

enter image description here

+3


source to share


1 answer


I will post two possible solutions. The first is the solution that I think best suits your needs, the second is the one that suits your current code better. Hope this helps!


Solution 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)

      




Solution 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)

      

+2


source







All Articles