Leaflet map not displaying when deployed as module in shinydashboard

Hello,

For reason I do not understand, leaflet maps are not displaying when wrapped into a module. They display just fine when in the main ui.R and server.R files. It's a big app, so it's my preference to modularize this, if possible.

Any ideas what is going on?

Here is a reprex:

### global conditions ####
library(shiny); library(shinydashboard); library(leaflet)

### modular functions ####

# UI file
extra_UI <- function(id) {
  ns = NS(id)
  fluidRow(leafletOutput("myMap")) 
}

# server file
extra_server <- function(input, output, session) {
  ns <- session$ns
  output$myMap <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      setView(lng = -114.37, lat = 43.11, zoom = 6)
  })
}

##### standard UI and server functions ####

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      extra_UI(id = "map")
    )
  )
)

server <- function(input, output, session) {
  callModule(module = extra_server, id = "map") 
}

shinyApp(ui = ui, server = server)

This code works (it produces the desired output), but, it is not modular. I am flummoxed.

### global conditions ####
library(shiny); library(shinydashboard); library(leaflet)

##### standard UI and server functions ####

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
       leafletOutput("myMap")
    )
  )
)

server <- function(input, output, session) {
  output$myMap <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      setView(lng = -114.37, lat = 43.11, zoom = 6)
  }) 
}

shinyApp(ui = ui, server = server)

hey there, I think you just forgot to use ns() in your leafletOutput() outputId arg

fluidRow(leafletOutput(ns("myMap")))

Oh my goodness, you are right! :woman_facepalming:

This topic was automatically closed 7 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.