我想跟踪用户使用Shiny应用程序的过程,为此,我需要检测用户是否单击了选项卡。为此,我使用了shinyjs
库和自定义js跟踪功能。但是目前,我无法提出在input$tabs
函数中如何使用id
而不是onclick()
的方法。当我将标签的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)
答案 0 :(得分:0)
感谢answer,我找到了解决我问题的方法。完全不需要使用onclick()
函数。
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) {
observe({
if(input$tabs == "report_1") {
alert("tab = report_1")
} else if(input$tabs == "report_2"){
alert("tab = report_2")
} else {
alert("tab = section_2")
}
})
}
shinyApp(ui=ui, server=server)