I am working on a Shiny application with a modular structure. The application involves a main UI that includes a module UI and a tab for testing inputs. The server logic initializes and renders the module UI and observes inputs from the module.
However, I am encountering an issue where the req(input$testModule_inputNumber) check in the output$testOutput render function never prints the message confirming the existence of the input. Despite the module UI being rendered and inputs being observed within the module server, the testOutput render function does not acknowledge the presence of input$testModule_inputNumber.
Simplified Code Here's a simplified version of the code:
library(shiny)
# Main UI
ui <- fluidPage(
navbarPage(
title = "Main App",
id = "navbar",
tabPanel("Module",
fluidRow(
column(width = 12,
uiOutput("moduleUI")
)
)
),
tabPanel("Test Inputs",
fluidRow(
column(12, textOutput("testOutput"))
)
)
)
)
# Server logic
server <- function(input, output, session) {
output$moduleUI <- renderUI({
print("Rendering module UI")
moduleUI("testModule")
})
observe({
print("Initializing testServer")
testServer("testModule")
})
# Debugging Test Inputs tab
output$testOutput <- renderText({
print("Checking testOutput input availability")
req(input$testModule_inputNumber) # <--- The problem is that the input is not loaded as expected
paste("inputNumber input exists:", !is.null(input$testModule_inputNumber))
})
}
# Simple Module UI for testing
moduleUI <- function(id) {
ns <- NS(id)
tagList(
fluidRow(
column(6,
numericInput(ns("inputNumber"), "Enter Number", value = 123, min = 1)
)
)
)
}
testServer <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
print("Inside testServer module")
observe({
print("Inside testServer observe")
req(input$inputNumber)
print(paste("Input number:", input$inputNumber))
})
})
}
shinyApp(ui, server)
Why is the req(input$testModule_inputNumber) check in the output$testOutput render function not acknowledging the presence of the inputNumber input from the module? How can I ensure that the input is recognized and the message is printed as expected?
R version: 4.3.2 shiny version: 1.8.0
Here's a working example that displays the value returned by your module in the main UI.
library(shiny)
# Simple Module UI for testing
moduleUI <- function(id) {
ns <- NS(id)
tagList(
fluidRow(
column(
6,
numericInput(ns("inputNumber"), "Enter Number", value = 123, min = 1)
)
)
)
}
testServer <- function(id) {
moduleServer(
id,
function(input, output, session) {
ns <- session$ns
reactive({ input$inputNumber })
}
)
}
# Main UI
ui <- fluidPage(
navbarPage(
title = "Main App",
id = "navbar",
tabPanel(
"Module",
moduleUI("testModule")
),
tabPanel(
"Test Inputs",
textOutput("testOutput")
)
)
)
# Server logic
server <- function(input, output, session) {
moduleData <- testServer("testModule")
output$testOutput <- renderText({
moduleData()
})
}
shinyApp(ui, server)