I have three modules:
- The first module creates a
numericInput
with value equal to 1. - The second module creates a
textInput
that is created withrenderUI
in the server function. The value is equal to the value of the first module + 1. - The third module should do the same as the second module, but with the value equal to the value of the second module + 1.
The choice for textInput
and renderUI
in the second and third module is deliberate. The code works without the third module, but throws the following error when the third module is included: Error in $: object of type 'closure' is not subsettable
. Below is the minimal example code. Help would be much appreciated!
first_module.R
#Define ui
first_module_ui <- function(id) {
ns <- NS(id)
tagList(numericInput(
inputId = ns("first_input"),
label = "First input:",
value = 1
))
}
#Define server logic
first_module_server <- function(input, output, session) {
return(input)
}
second_module.R
#Define ui
second_module_ui <- function(id) {
ns <- NS(id)
tagList(uiOutput(outputId = ns("second_input")))
}
#Define server logic
second_module_server <- function(input, output, session, first_module_res) {
ns <- session$ns
observe({
second_input <- first_module_res$first_input + 1
output$second_input <- renderUI({
disabled(textInput(
inputId = ns("second_input"),
label = "Second input:",
value = second_input
))
})
})
return(reactive({second_input}))
}
third_module.R
#Define ui
third_module_ui <- function(id) {
ns <- NS(id)
tagList(uiOutput(outputId = ns("third_input")))
}
#Define server logic
third_module_server <- function(input, output, session, second_module_res) {
ns <- session$ns
observe({
third_input <- second_module_res$second_input + 1
output$third_input <- renderUI({
disabled(textInput(
inputId = ns("third_input"),
label = "Third input:",
value = third_input
))
})
})
}
app.R
library(shiny)
library(shinyjs)
# Define UI
ui <- fluidPage(
useShinyjs(),
# Application title
titlePanel("Demo"),
# Sidebar
sidebarLayout(
sidebarPanel(
first_module_ui("first")
),
mainPanel(
second_module_ui("second"),
third_module_ui("third")
)
)
)
# Define server logic
server <- function(input, output, session) {
callModule(first_module_server, "first")
first_module_res <- callModule(first_module_server, "first")
callModule(second_module_server, "second", first_module_res)
second_module_res <- callModule(second_module_server, "second", first_module_res)
callModule(third_module_server, "third", second_module_res)
}
# Run the application
shinyApp(ui = ui, server = server)