Thomas
Thomas

Reputation: 1302

updateTabItems not working in module if input passed

I want to create a function/a module to update tabset items.

I'm aware of this and this question, but my issue relates to how the button input is handled in updateTabItems.

Here you can find an example:

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

mod_updateTabset <- function(
  input, output, session, triggerId, dashboardId, tab, parent
) {
  observeEvent(triggerId, {
    updateTabItems(parent, dashboardId, selected = tab)
  })
}

ui <- dashboardPagePlus(
  header = dashboardHeaderPlus(),
  sidebar = dashboardSidebar(
    sidebarMenu(
      id = 'Tabs', 
      menuItem("Tab 01", tabName = "tab01", icon = icon("dice-one")),
      menuItem("Tab 02", tabName = "tab02", icon = icon("dice-two"))
    )
  ),
  body = dashboardBody(
    tabItems(
      tabItem(
        tabName = "tab01",
        actionButton("updateButton", label = "To Tab02")
      ),
      tabItem(
        tabName = "tab02",
        h4("New Tab")
      )
    )
  )
)

server <- function(input, output, session) {
  callModule(
    mod_updateTabset,
    "updateLink",
    triggerId = input$updateButton,
    dashboardId = "Tabs", 
    tab = "tab02",
    parent = session
  )
}

shinyApp(ui = ui, server = server)

I know it's working when creating mod_updateTabset_UI, shifting the actionButton to the module. That's why I suppose the issue lies in the button handling.

Still, I'd like to have a function/module which can handle all kind of links, buttons etc. to update Tab items, not just one button

Upvotes: 1

Views: 313

Answers (1)

Thomas
Thomas

Reputation: 1302

For anyone who runs into the same problem: You need to use reactive() for triggerId when calling the module

callModule(
  mod_updateTabset,
  "updateLink",
  triggerId = reactive(input$updateButton),
  dashboardId = "Tabs", 
  tab = "tab02",
  parent = session
)

The module then needs to handle a reactive value:

mod_updateTabset <- function(
  input, output, session, triggerId, dashboardId, tab, parent
) {
  observeEvent(triggerId(), {
    updateTabItems(parent, dashboardId, selected = tab)
  })
}

Upvotes: 1

Related Questions