0

I am trying to use an actionbutton in a leaflet popup into a shiny module

When trying to use an action button into a leaflet popup in a Shiny module, button do not work.

See the exemple below :

library(shiny)
library(leaflet)
library(DT)

map_ui <- function(id) {
  ns <- NS(id)
  tagList(
    
    leafletOutput(ns("mymap"))
    
  )
}

map_Server <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      
      mapdata <- datasets::quakes
      mapdata$latitude <- as.numeric(mapdata$lat)
      mapdata$longitude <- as.numeric(mapdata$long)
      mapdata$id <- 1:nrow(mapdata)
      
      output$mymap <- renderLeaflet({
        leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
          addMarkers(lat = ~ latitude, lng = ~ longitude,
                     data = mapdata,
                     layerId = mapdata$id,
                     popup= ~paste("<b>", mag, "</b></br>", actionLink(inputId = "modal", label = "Modal", onclick = 'Shiny.setInputValue(\"button_click\", this.id, {priority: \"event\"})')))
      })

      observeEvent(input$button_click, {
        showModal(modalDialog(
          title = "TEST MODAL"
        ))      })
      

      
    }
  )
}

ui <- fluidPage(
  
  map_ui('ex1')
  
)

server <- function(input, output){
  map_Server('ex1')
  
}

shinyApp(ui, server)

Is there any way to make work that button into the module ? I think that it comes that the button is not ns() but i don't find a way to make it works.

Thanks

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225

1 Answers1

1

Yes, you have to add the ns:

function(input, output, session) {
  
  ns <- session$ns  
  
  ...... 
  
  output$mymap <- renderLeaflet({
    leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
      addMarkers(
        lat = ~ latitude, lng = ~ longitude,
        data = mapdata,
        layerId = mapdata$id,
        popup = 
          ~paste(
            "<b>", mag, "</b></br>", 
            actionLink(
              inputId = "modal", label = "Modal", 
              onclick = sprintf(
                'Shiny.setInputValue(\"%s\", this.id, {priority: \"event\"})',
                ns("button_click")
              )
            )
          )
      )
  })
  
  ......
  
}
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225