我在一个 Shiny 的应用程序中工作,我希望能够访问用户在 session 中所在的当前选项卡上的信息。
我有一个观察事件,它监听要单击的特定按钮。简单地说,我想存储/打印用户单击此按钮时所在的当前选项卡。单击此按钮后,该选项卡将更改为“帮助”,其中 updateTabItems 将 session 、inputId 和选定值作为参数。
# Observe event when someone clicks a button
observeEvent(input$help, {
# if they are logged in
if(USER$Logged == TRUE) {
# current_tab <- ???
shiny_session <<- session
updateTabItems(session, "sidebar", selected = "help")
}
})
由于 session 具有一些值(value),因此我尝试对其进行探索。
> class(shiny_session)
[1] "ShinySession" "R6"
> names(shiny_session)
[1] ".__enclos_env__" "session"
[3] "groups" "user"
[5] "singletons" "request"
[7] "closed" "downloads"
[9] "files" "token"
[11] "clientData" "output"
[13] "input" "progressStack"
[15] "clone" "decrementBusyCount"
[17] "incrementBusyCount" "outputOptions"
[19] "manageInputs" "manageHiddenOutputs"
[21] "registerDataObj" "registerDownload"
[23] "fileUrl" "saveFileUrl"
[25] "handleRequest" "@uploadEnd"
[27] "@uploadInit" "@uploadieFinish"
[29] "reload" "reactlog"
[31] "onFlushed" "onFlush"
[33] "sendInputMessage" "sendCustomMessage"
[35] "dispatch" "sendProgress"
[37] "showProgress" "flushOutput"
[39] "defineOutput" "setShowcase"
[41] "isEnded" "isClosed"
[43] "wsClosed" "close"
[45] "unhandledError" "onInputReceived"
[47] "onEnded" "onSessionEnded"
[49] "ns" "makeScope"
[51] "initialize"
我试图探索 Shiny session 的这些元素,它们大多以函数的形式构建,在当前选项卡上找不到任何内容。
UpdateTabItems 似乎接受值并将它们发送到 sendInputMessage。
> updateTabItems
function (session, inputId, selected = NULL)
{
message <- dropNulls(list(value = selected))
session$sendInputMessage(inputId, message)
}
这似乎是在 Shiny 的应用程序中执行的某种命令堆栈,因此我停止探索它。
> shiny_session$sendInputMessage
function (inputId, message)
{
data <- list(id = inputId, message = message)
private$inputMessageQueue[[length(private$inputMessageQueue) +
1]] <- data
}
关于如何在给定时间点访问变量中的当前选项卡信息的任何建议?
谢谢。
最佳答案
由于您尚未提供 minimal reproducible example ,我必须做一些猜测才能产生一个合适的例子 - 但很好:) 看来你正在使用 shinydashboard
在应用程序中,您有一个 sidebarMenu
至少有两个标签。
I want to be able to access information on the current tab a user is on in a session.
你可以给
sidebarMenu
ID
,比如说,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)
关于r - 如何在 Shiny 应用程序中访问/打印/跟踪当前选项卡选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38863215/