I have experience with Shiny programming, but am new to Shiny modules and am having trouble getting updateTabItems
in shinydashboard to work properly. I'm pretty sure I understand the concept of modules generally, but must be missing something within the server module.
I am trying to modularize the example in ?shinydashboard::updateTabItems
using golem, and I have tried passing the parent session as suggested here, here, and here. I'm guessing I'm just missing a small detail somewhere, but can't seem to pinpoint it. Thanks for any help.
library(shiny)
library(shinydashboard)
mod_sidebar_ui <- function(id){
ns <- NS(id)
tagList(
shinydashboard::sidebarMenu(
id = ns("sidebar"),
shinydashboard::menuItem(
"Dashboard", tabName = ns("dashboard"), icon = icon("dashboard")
),
shinydashboard::menuItem(
"Widgets", tabName = ns("widgets"), icon = icon("th")
),
shiny::actionButton(ns("switchtab"), label = "Switch Tabs")
)
)
}
mod_tab_items_ui <- function(id){
ns <- NS(id)
tagList(
shinydashboard::tabItems(
shinydashboard::tabItem(
tabName = ns("dashboard"), h2("Dashboard tab content")
),
shinydashboard::tabItem(
tabName = ns("widgets"), h2("Widgets tab content")
)
)
)
}
mod_sidebar_server <- function(id, parent){
moduleServer( id, function(input, output, session){
ns <- session$ns
observeEvent(input$switchtab, {
dashboard <- ns("dashboard")
widget <- ns("widget")
newtab <- ifelse(input$sidebar == dashboard, widget, dashboard)
shinydashboard::updateTabItems(session = parent, ns("sidebar"), newtab)
})
})
}
app_ui <- function(request) {
tagList(
shinydashboard::dashboardPage(
shinydashboard::dashboardHeader(),
shinydashboard::dashboardSidebar(
mod_sidebar_ui("test")
),
shinydashboard::dashboardBody(
mod_tab_items_ui("test")
)
)
)
}
app_server <- function(input, output, session) {
mod_sidebar_server("test", parent = session)
}
shinyApp(app_ui, app_server)