由于您还没有提供最小可重复示例 https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example,我必须做出一些猜测才能生成适当的示例 - 但这很好:)看来您正在使用shinydashboard
在应用程序中你有一个sidebarMenu
至少有两个选项卡。
我希望能够访问用户在会话中当前所在选项卡上的信息。
你可以给sidebarMenu
an ID
, say, tabs
然后您可以通过以下方式访问当前选项卡上的信息input$tabs
.
让我们看一下下面的一个例子,它突出了这两方面
首先,我们“颁奖”sidebarMenu
具有独特的ID
sidebarMenu(id = "tabs",
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Help", tabName = "help", icon = icon("h-square"))
)
然后在服务器端监视它
observe({
print(input$tabs)
})
完整示例:
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)