Calling nested module ui functions from the main app

32 views Asked by At

I am writing an R package that contains a shiny module (server & ui) I want to reuse in many projects. That package module may be implemented as a nested module in case I want to reuse the calling module across different apps as well.

The package contains ui functions (that consume outputs from the module server).

For example, the package has an admin_ui function that returns the full admin console ui. When I call that function from the main app ui, I need to pass both module ids to get correct output from the NS function:

admin_ui(id = c("module_id", "nested_module_id"))

?NS suggests 'length 2 will be interpreted as multiple namespaces' but I don't find a lot of examples using multiple namespaces.

What's the best option to do that? - are multiple namespaces an acceptable option?

Nested module server

nested_module_server <- function(id,) {
  moduleServer(id, function(input, output, session) {
    
    # -- sample_output
    output$nested_module_output <- renderText("From the nested module")
    
  })
}

Nested module ui

nested_module_UI <- function(id) {
  
  # namespace
  ns <- NS(id)
  
  textOutput(ns("nested_module_output"))
  
}

Module server

module_server <- function(id) {
  moduleServer(id, function(input, output, session) {

    # -- call nested module
    nested_module_server(id = "nested_module_id")
    
  })
}

Main app server

shinyServer(
    function(input, output){
            
      # -- call module
      module_server(id = "module_id")
        
    }
)

Main app ui

# -- nested module output
nested_module_UI(c("module_id", "nested_module_id")))
0

There are 0 answers