I try to understand the usage of modules and stratégie du petit r.
For this, I built a small application.
A navBarpage with 2 panels. For each tabPanel one module exists which creates the UI and contains the server logic for the panel.
tabPanel 1 / Module something
- numeric Input -> user can choose between 1,2,3
- action button. When pressed the panel View changes.
tabPanel 2 / Module happend
- shows the chosen number
I would like to receive feedback if I used the components correctly?
Also I'm interested: Does it make sense to structure an application in the way that each tabPanel is 1 Module? For Example File Upload, Overview, Clustering, and Reporting.
- app_ui (UI-structure and calls module UI)
- server_ui (calls Module Servers)
- mod_something (Numberinput and actionButton to change panel)
- mod_happend ( Displays number)
app_ui
app_ui <- function(request) {
tagList(
# Leave this function for adding external resources
golem_add_external_resources(),
# Your application UI logic
fluidPage(
navbarPage("navBarPage",
id = "navBarID",
tabPanel("title1",
mod_something_ui("something_1")),
tabPanel("title2",
mod_happend_ui("happend_1")),
)
)
)
}
app_server
app_server <- function(input, output, session) {
r <- reactiveValues()
mod_something_server("something_1", r = r,parent = session)
mod_happend_server("happend_1",r)
}
mod_something
tmod_something_ui <- function(id){
ns <- NS(id)
tagList(
selectInput(inputId = ns("numberInput"), label = "choose", choices = c(1,2,3)),
actionButton(inputId = ns("doSomething"),label = "Change Page!"),
)
}
mod_something_server <- function(id,r,parent){
moduleServer(id, function(input, output, session){
ns <- session$ns
observe({
r$numberInput <- input$numberInput
})
observeEvent(eventExpr = input$doSomething,
updateNavbarPage(session = parent, inputId = "navBarID", selected = "title2"))
})
}
mod_happend
mod_happend_ui <- function(id){
ns <- NS(id)
tagList(
p("the Number is"),
textOutput(ns("choosenNumber")),
)
}
mod_happend_server <- function(id,r){
moduleServer( id, function(input, output, session){
ns <- session$ns
number <- reactive(r$numberInput)
output$choosenNumber <- renderText(number())
})
}