I have a simple Shiny app which calls 2 modules - this works.
However, I'm trying to transform it so that Shiny app calls a module, which in turn calls a sub-module. Please see code below, I can't work out why it won't work:
I'm trying to solve this problem so that I understand how to translate another application into Golem.
################## Sub-module ###########
mod_b_ui <- function(id) {
ns <- NS(id)
tagList(
actionButton(ns("validate"), "Print")
)
}
mod_b_server <- function(id, react) {
moduleServer(id, function(input, output, session) {
observeEvent( input$validate , {
print(react())
})
})
}
################## Module ############
mod_ui <- function(id) {
ns <- NS(id)
tagList(
sliderInput(ns("choice"), "Choice", 1, 10, 5),
mod_b_ui("mod_ui_2")
)
}
mod_server <- function(id) {
moduleServer(id, function(input, output, session) {
res <- reactive({input$choice})
mod_b_server("mod_ui_2", react = res)
})
}
################### Application ############
library(shiny)
app_ui <- function() {
fluidPage(
mod_ui("mod_ui_1")
)
}
app_server <- function(input, output, session) {
res <- mod_server("mod_ui_1")
}
shinyApp(app_ui, app_server)
Whenever you are calling a submodule, you need to namespace the id of the submodule.
In other words, you should do mod_b_ui(ns("mod_ui_2"))
inside mod_ui
:
mod_ui <- function(id) {
ns <- NS(id)
tagList(
sliderInput(ns("choice"), "Choice", 1, 10, 5),
mod_b_ui(ns("mod_ui_2"))
)
}
Here is the full working app.
################## Sub-module ###########
mod_b_ui <- function(id) {
ns <- NS(id)
tagList(
actionButton(ns("validate"), "Print")
)
}
mod_b_server <- function(id, react) {
moduleServer(id, function(input, output, session) {
observeEvent( input$validate , {
print(react())
})
})
}
################## Module ############
mod_ui <- function(id) {
ns <- NS(id)
tagList(
sliderInput(ns("choice"), "Choice", 1, 10, 5),
mod_b_ui(ns("mod_ui_2"))
)
}
mod_server <- function(id) {
moduleServer(id, function(input, output, session) {
res <- reactive({input$choice})
mod_b_server("mod_ui_2", react = res)
})
}
################### Application ############
library(shiny)
app_ui <- function() {
fluidPage(
mod_ui("mod_ui_1")
)
}
app_server <- function(input, output, session) {
res <- mod_server("mod_ui_1")
}
shinyApp(app_ui, app_server)