A shiny app which I am building is growing in terms of lines and I am trying to understand how to use modules. This is how my code looks like below. What I am trying to do is add a couple of dropdowns. The first drop-down class_level
is a straightforward value. But the second one selected_product
depends upon what the user selects in the first dropdown. As of now, everything renders for me except for the second drop down. I want to know if what I am doing is the right way of doing things in terms of R shiny modules and what is wrong in my code that the second dropdown doesn't show up. Thank you.
EDIT: Modified the code for a reproducible code.
library(shiny)
library(shinydashboard)
library(shinyWidgets)
dropDownUI <- function(id, div_width = "col-xs-12 col-md-8") {
ns <- NS(id)
div(column(3, uiOutput(ns("class_level"))),
column(
width = 3,
pickerInput(
inputId = ns("selected_product"),
label = h4("Product Family"),
choices <- c("22","33","44"),
width = "100%"
)
))
}
dropDown <- function(input, output, session) {
ns <- session$ns
output$class_level <- renderUI({
selectInput(
ns("selected_class"),
label = h4("Classification Level"),
choices = list(
"apple " = "apple",
"orange " = "orange"),
selected = "orange"
)})
observeEvent(input$class_level, {
ns <- session$ns
if (input$selected_class == "apple") {
choices <- c("foo","zoo","boo")
} else if (input$selected_class == "orange") {
choices <- c("22","33","44")
} else {
choices <- c("aa","bb","cc")
}
updateSelectInput(session,
inputId = ns("selected_product"),
label = h4("Product Family"),
choices = choices)
})
}
sidebar <- dashboardSidebar(sidebarMenu(
menuItem("aaa",tabName = "aaa"),
menuItem("bbb", tabName = "bbb"),
menuItem("ccc", tabName = "ccc")
))
body <- ## Body content
dashboardBody(tabItems(
tabItem(tabName = "aaa",
fluidRow(dropDownUI(id = "dropdown")),
fluidRow(chartTableBoxUI(id = "ATC_Topline"))) # render the tabBox inside a fluidRow
))
# Put them together into a dashboardPage
ui <- dashboardPage(
dashboardHeader(title = "Loyalty Monthly Scorecard"),
sidebar,
body
)
server = {
shinyServer(function(input, output, session) {
MyData <- read.csv(file="ldb_data.csv", header=TRUE, sep=",")
callModule(chartTableBox, id = "ATC_Topline", data = MyData)
callModule(dropDown, id = "dropdown")
})
}
shinyApp(ui = ui, server = server)