Check for Missing ns() in Modules
Validate that all module input/output IDs are properly namespaced.
Why Namespacing Matters
In Shiny modules, all input and output IDs must be namespaced using
the ns() function. This prevents ID conflicts when the same
module is used multiple times in an app.
Correct vs Incorrect
✅ Correct
mod_analytics_ui <- function(id) {
ns <- NS(id)
tagList(
selectInput(
ns("type_filter"),
label = NULL,
choices = c("All", "None")
)
)
}❌ Incorrect
mod_analytics_ui <- function(id) {
ns <- NS(id)
tagList(
selectInput(
"type_filter", # Missing ns()!
label = NULL,
choices = c("All", "None")
)
)
}What to Check
In Module UI Functions
- All input IDs:
textInput(),selectInput(),actionButton(), etc. - All output IDs:
plotOutput(),tableOutput(),uiOutput(), etc. - All input control IDs used in
ns()calls
How Claude Checks
-
Locate all IDs in module files (files starting with
mod_)- Search for common patterns:
Input(),Output(), input/output IDs
- Search for common patterns:
-
Verify namespacing by checking each ID is wrapped
in
ns()- Exception: Only the first
NS(id)definition doesn’t need wrapping
- Exception: Only the first
-
Report findings
- If all IDs are properly namespaced: ✅ All good
- If missing IDs found: ❌ List them and ask for permission to fix
-
Fix if approved
- Wrap missing IDs in
ns() - Preserve code structure and formatting
- Wrap missing IDs in
Common Mistakes
| Mistake | Fix |
|---|---|
selectInput("myid", ...) |
selectInput(ns("myid"), ...) |
plotOutput("plot") |
plotOutput(ns("plot")) |
observeEvent(input$button) |
observeEvent(input$submit, ...) with ID properly
namespaced |
uiOutput("dynamic") |
uiOutput(ns("dynamic")) |
When to Run
- After creating or modifying module UI functions
- Before running
devtools::check() - When adding new inputs/outputs to modules
- Before deploying to production
Example Workflow
User: Check my modules for missing ns()
Claude:
1. Scans all mod_*.R files
2. Identifies 3 missing ns() wrappings
3. Shows the issues with line numbers
4. Asks: "Should I fix these?"
User: Yes, fix them
Claude:
- Updates the files
- Runs the check again to verify
- Confirms: "All fixed! ✅"