How to use the input from one module in another module

I'm trying to make a shiny app with modules, and I'm having trouble making the different modules talk to each other.

Basically, I want to have a module that creates a map, and then I want to place that module within the outer ui/server modules. However, the map needs to get an input that should live outside of the map module, because that same input will be used elsewhere in the app.

In the example below, the issue seems to be that map_server_module does not know where to get input$selected_common_name (which is created in ui_module). How can I access input$selected_common_name in the map_server_module?

Thank you!

library(shiny)
library(shinydashboard)
library(leaflet)
library(tidyverse)

# create data
df <- data.frame("common_name" = c('a', 'b', 'c'),
                 "latitude" = c(21.936, 21.694, 22.042),
                 "longitude" = c(201.184, 201.938, 200.069))

# make list object for checkbox
common_name_list <- setNames(as.list(unique(df$common_name)), unique(df$common_name))


# make inner ui and server modules for map
map_ui_module <- function(id) {
  ns <- NS(id)
  leafletOutput(ns("plot"))
}

map_server_module <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      df_subset <- reactive({
        df %>% filter(common_name %in% input$selected_common_name)
      })
      output$plot <- renderLeaflet({
        leaflet(df_subset()) %>% 
          addTiles() %>%
          addCircles(lng = ~longitude, lat = ~latitude)
      })
    }
  )
}


# make outer ui and server modules
ui_module <- function(id) {
  ns <- NS(id)
  tagList(
    checkboxGroupInput(inputId = ns("selected_common_name"), 
                       label = "Select Common Name", 
                       choices = common_name_list,
                       selected = "a"),
    map_ui_module(ns("inner_module"))
  )
}

server_module <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      map_server_module("inner_module")
    }
  )
}


# call modules
ui <- fluidPage(
  ui_module(id = "outer_module")
)

server <- function(input, output, session) {
  server_module(id = "outer_module")
}

shinyApp(ui, 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.