Search code examples
rshinyreactive-programmingr-leafletshiny-reactivity

Is it possible to pass leaflet map to another module?


I write an app which I try to modularize. In general, 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)

Solution

  • It seems that you need to pass down the leaflet proxy as a reactive to the module for it to work.

    Added a leaflet proxy instead of input name for the variable passMap in module mod_btn_server1:

    proxymap <- reactive(leafletProxy('map'))  
    
    mod_btn_server2("other", proxymap)  
    

    In mod_btn_server2 you have then to change Passmap to be the leafletproxy itself removing the call to leafletproxy:

        observeEvent(input$btn, {
      passMap() %>%
        leaflet::addCircleMarkers(
          data = coords,
          stroke = TRUE,
          color = "red",
          radius = 6)
      
    })
    

    The full code here:

    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)
        })
        proxymap <- reactive(leafletProxy('map'))  
    
        mod_btn_server2("other", proxymap)  
        
        
      })
    }
    
    
    # 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, {
          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)