I am working within a shiny app and I want to be able to access information on the current tab a user is on in a session.
I have a observe event that listens for a parti
Since you haven't provided a minimal reproducible example, I have to make some guesses to produce an appropriate example - but it's fine :) It seems that you're using shinydashboard
and in the app you have a sidebarMenu
with at least two tabs.
I want to be able to access information on the current tab a user is on in a session.
You can give sidebarMenu
an ID
, say, tabs
and then you can access the information on the current tab via input$tabs
.
Let's take a look at an example below which highlights these two aspects
First, we "award" sidebarMenu
with an unique ID
sidebarMenu(id = "tabs",
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Help", tabName = "help", icon = icon("h-square"))
)
and then spy on it on the server side with
observe({
print(input$tabs)
})
Full example:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Example"),
dashboardSidebar(
sidebarMenu(id = "tabs", # note the id
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Help", tabName = "help", icon = icon("h-square"))
),
br(),
# Teleporting button
actionButton("teleportation", "Teleport to HELP", icon = icon("h-square"))
),
dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
h2("Dashboard tab content")
),
tabItem(tabName = "help",
h2("Help tab content")
)
)
)
)
server <- function(input, output, session) {
# prints acutall tab
observe({
print(input$tabs)
})
observeEvent(input$teleportation, {
# if (USER$Logged == TRUE) {
if (input$tabs != "help") {
# it requires an ID of sidebarMenu (in this case)
updateTabItems(session, inputId = "tabs", selected = "help")
}
#}
})
}
shinyApp(ui, server)