Is it possible to pass leaflet map as reactive to other module?

I added leaflet map to my app's body (in main module) and what I want to do is to write some other modules which refer to my main map (showing/hiding points on a map and other spatial operations). I try to refer to this map (being in main module) from other modules. In example below I passed map as reactive expression from main module but when I press button showing points on the map then the error shows up:

Error in if: missing value where TRUE/FALSE needed

Is it possible to pass map to another module at all? And use leafletProxy there?

Here is reproducible example:

library(shiny)
library(dplyr)
library(sf)
library(leaflet)



moduleServer <- function(id, module) {
    callModule(module, id)
}

# Main module - UI 1 #
mod_btn_UI1 <- function(id) {
    
    ns <- NS(id)
    tagList(
        leafletOutput(ns("map")),
        mod_btn_UI2(ns("other"))
    )
}

# Main module - Server 1 #
mod_btn_server1 <- function(id){
    moduleServer(id, function(input, output, session) {
        
      ns <- NS(id)
      
      # here I pass map as reactive
      passMap = reactive({input$map})
      
      coords <- quakes %>%
        sf::st_as_sf(coords = c("long","lat"), crs = 4326)
      
    
        output$map <- leaflet::renderLeaflet({
          leaflet::leaflet() %>% 
            leaflet::addTiles() %>% 
            leaflet::setView(172.972965,-35.377261, zoom = 4) %>%
            leaflet::addCircleMarkers(
              data = coords,
              stroke = FALSE,
              radius = 6)
        })
        
        mod_btn_server2("other", passMap)  
        
             
    })
}


# Other module - UI 2 #
mod_btn_UI2 <- function(id) {
  
  ns <- NS(id)
  tagList(
    actionButton(inputId = ns("btn"), label = "show points")
  )
}


# Other module - Server 2 #
mod_btn_server2 <- function(id, passMap){
  moduleServer(id, function(input, output, session) {
    
    ns <- NS(id)
    
    coords <- quakes %>%
      sf::st_as_sf(coords = c("long","lat"), crs = 4326)
    
    observeEvent(input$btn, {
      leaflet::leafletProxy(passMap()) %>%
        leaflet::addCircleMarkers(
          data = coords,
          stroke = TRUE,
          color = "red",
          radius = 6)

    })
    
  })
}



# Final app #

ui <- fluidPage(
    
    tagList(
        mod_btn_UI1("test-btn"))

)

server <- function(input, output, session) {
    
    mod_btn_server1("test-btn")
    
}

shinyApp(ui = ui, server = server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.