Tracks the active tab and exposes it as a reactive value. Optionally integrates with Shiny's bookmarking system so the active tab is preserved in bookmarked URLs.
Arguments
- id
Module id matching the
idpassed toglassTabsUI(). Do not wrap this inns()—glassTabsServer()handles namespacing internally viashiny::moduleServer().- bookmark
Logical. When
TRUE(default), registersshiny::onBookmark()andshiny::onRestored()hooks so the active tab is saved and restored automatically when Shiny bookmarking is enabled. Set toFALSEto opt out.
Details
glassTabsServer() follows the same calling convention as all Shiny module
server functions: pass the bare module id, not a namespaced one.
Inside a parent module, pair it with glassTabsUI(ns("tabs"), ...) in the
UI, and call glassTabsServer("tabs") (without ns()) in the server.
Examples
# --- Standalone app ---
if (interactive()) {
library(shiny)
ui <- fluidPage(
useGlassTabs(),
glassTabsUI(
"tabs",
glassTabPanel("a", "A", p("Tab A"), selected = TRUE),
glassTabPanel("b", "B", p("Tab B"))
)
)
server <- function(input, output, session) {
active <- glassTabsServer("tabs")
observe(print(active()))
}
shinyApp(ui, server)
}
# --- Bookmarking ---
if (interactive()) {
library(shiny)
ui <- function(request) {
fluidPage(
useGlassTabs(),
bookmarkButton(),
glassTabsUI(
"tabs",
glassTabPanel("a", "A", p("Tab A"), selected = TRUE),
glassTabPanel("b", "B", p("Tab B"))
)
)
}
server <- function(input, output, session) {
active <- glassTabsServer("tabs", bookmark = TRUE)
}
shinyApp(ui, server, enableBookmarking = "url")
}
# --- Inside a Shiny module ---
# UI side: use ns() to namespace the widget id
my_module_ui <- function(id) {
ns <- shiny::NS(id)
shiny::tagList(
useGlassTabs(),
glassTabsUI(
ns("tabs"), # <-- ns() wraps the id here
glassTabPanel("a", "A", p("Tab A"), selected = TRUE),
glassTabPanel("b", "B", p("Tab B"))
)
)
}
# Server side: pass the bare id — NOT ns("tabs")
my_module_server <- function(id) {
shiny::moduleServer(id, function(input, output, session) {
active <- glassTabsServer("tabs") # <-- bare id, no ns()
shiny::observe(print(active()))
})
}