0

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)
code_cowboy
  • 596
  • 5
  • 18
  • The small detail is a widget`s`. Try `widget <- ns("widgets")` instead of `widget <- ns("widget")` in your `observeEvent`. – stefan Oct 26 '22 at 20:33
  • Ergh!! Of course. Thank you so much...I think I just needed another pair of eyes to see that. – code_cowboy Oct 27 '22 at 12:22
  • :D This happens to all of us. And took me a while to realize where the issue is. Especially with shiny I find it even harder to find typos as we don't get an error about an object not found. – stefan Oct 27 '22 at 12:35

0 Answers0