Main app.R
header <- dashboardHeader(title = "R shiny Dashboard")
source("projects/customers/ui.R", local = TRUE)
customers_server <-
source("projects/customers/server.R", local = TRUE)$value
sidebar <- dashboardSidebar(
sidebarMenu(
id = "sidebar",
menuItem("Home", tabName = "home"),
menuSubItem("Customers", tabName = "customers")
)
)
body <- dashboardBody(tabItems(
tabItem(tabName = "home",
tags$div(
HTML(
'<h2 style="font-family:Arial; color:black;">Welcome</h2>'
)
)),
tabItem(tabName = "customers",
customers_ui("customers_ui"))
))
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output, session) {
observe({
req(input$sidebar) # Ensure 'sidebar' input is available before executing the rest of the code
tab <- input$sidebar
if (tab == "home") {
# do nothing for home page
}
if (tab == "customers") {
shinyjs::show("loading")
callModule(customers_server, "customers_ui", session = session)
shiny::removeModal() # Remove the modal once the data is loaded
shinyjs::hide("loading")
}
})
}
# Run the application
shinyApp(
ui = ui,
server = server,
options = list(host = "0.0.0.0", port = 9999)
)
projects/customers/ui.R
source("projects/customers/modules.R")
customers_ui <- function(id) {
ns <- NS(id)
fluidPage(
shinyjs::useShinyjs(),
# Set the title of the page
titlePanel(" Dashboard"),
# Create tabs for Drift Metrics and Model Monitoring
tabsetPanel(
id = "tabs",
tabPanel("Customer Stats", customerStatsUi(ns("customer_stats")))
)
)
}
Projects/customers/server.R
source("projects/customers/modules.R")
customers_server <- function(input, output, session) {
ns <- session$ns
timer <- reactiveTimer(2000) # 2 second delay
observeEvent(input$tabs, {
shinyjs::show("loading")
timer() # Trigger the timer
})
customersStats(ns("customer_stats"))
observeEvent(timer(), {
shinyjs::hide("loading")
})
}
projects/customers/modules.R
customerStatsUi = function(id) {
ns = NS(id) # Create a namespaced ID function
fluidPage(
fluidRow(
tags$head(tags$style(
HTML(
"
.skin-blue .main-header .navbar {
background-color: #1a8cff;
}
.skin-blue .main-header .logo {
background-color: #1a8cff;
}
.box.box-primary {
border-top-color: #1a8cff;
}
.sidebar-menu>li.active>a {
border-left-color: #1a8cff;
}
.selectize-input {
border-color: #1a8cff;
}
"
)
)),
useShinyjs(), # Enable shinyjs
box(
title = "Settings",
width = 12,
status = "primary",
solidHeader = TRUE,
column(
4,
dateInput(
ns("date"),
"Date:",
value = Sys.Date()
),
actionButton(ns("submit"), "Submit"), # Add submit button
textOutput(ns("selectedDate"))
)
)
)
)
}
customersStats = function(id) {
moduleServer(id, function(input, output, session) {
ns = session$ns # Create a namespaced ID function
print(input$date)
output$selectedDate <- renderText({
req(input$submit)
paste("Selected date:", input$date)
})
})
}
The print(input$date) prints NULL. I'm unable to read any input$ values. If I call the server function directly instead of using callModule, everything works fine.
Could someone help me identify what I'm doing wrong here.
Tried experimenting with the code by print(ns("date")) which prints - [1] "customers_ui-customers_ui-customer_stats-date"
Does it mean I cannot access the date as input$date?`