【发布时间】:2019-04-22 09:41:42
【问题描述】:
我想跟踪用户在 Shiny 应用中的旅程,为此我需要检测用户是否点击了选项卡。为此,我使用了shinyjs 库和自定义 js 跟踪功能。但是目前我想不出如何在onclick() 函数中使用input$tabs 而不是id。当我将标签的tabName 用作id 时,该功能不会对点击做出反应。
library(shiny)
library(shinydashboard)
library(shinyjs)
ui = dashboardPage(
dashboardHeader(title = "Shiny"),
dashboardSidebar(
sidebarMenu(id = "tabs",
menuItem("Section_1", tabName = "section_1", icon = icon("align-justify"),
startExpanded = TRUE, selected = TRUE,
menuSubItem("Subsection 1", tabName = "report_1", selected = TRUE),
menuSubItem("Subsection 2", tabName = "report_2")),
menuItem("Section_2", tabName = "section_2", icon = icon("align-justify"))
)
),
dashboardBody(
useShinyjs(),
tabItems(
tabItem("report_1", h1(id = "a", "a")),
tabItem("report_2", h1(id = "b", "b")),
tabItem("section_2", h1(id = "c", "c")))
)
)
server <- function(input, output, session) {
onclick("report_1", alert("tab = report_1"))
onclick("report_2", alert("tab = report_2"))
onclick("section_2", alert("tab = section_2"))
onclick("a", alert("tab = report_1"))
onclick("b", alert("tab = report_2"))
onclick("c", alert("tab = section_2"))
}
shinyApp(ui=ui, server=server)
【问题讨论】:
标签: shiny shinydashboard shinyjs