Shinyjs:如何检测用户是否单击了选项卡?

Shinyjs:如何检测用户是否单击了选项卡?,shiny,shinydashboard,shinyjs,Shiny,Shinydashboard,Shinyjs,我想在一个闪亮的应用程序中跟踪用户的旅程,为此我需要检测用户是否单击了某个选项卡。为此,我使用了shinyjs库和一个自定义js跟踪函数。但目前我还不知道如何在onclick()函数中使用input$tabs而不是id。当我将选项卡的tabName用作id时,该函数不会在单击时做出反应 library(shiny) library(shinydashboard) library(shinyjs) ui = dashboardPage( dashboardHeader(title =

我想在一个闪亮的应用程序中跟踪用户的旅程,为此我需要检测用户是否单击了某个选项卡。为此,我使用了
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) 
库(闪亮)
图书馆(shinydashboard)
图书馆(shinyjs)
ui=仪表板页面(
仪表板标题(title=“shinny”),
仪表板侧栏(
侧边栏菜单(id=“tabs”,
menuItem(“Section_1”,tabName=“Section_1”,icon=icon(“对齐对齐”),
startExpanded=TRUE,selected=TRUE,
menuSubItem(“第1小节”,tabName=“报告1”,selected=TRUE),
menuSubItem(“第2小节”,tabName=“报告2”),
菜单项(“Section_2”,tabName=“Section_2”,icon=icon(“对齐对齐”))
)
),
仪表板主体(
useShinyjs(),
tabItems(
选项卡项(“报告1”,h1(id=“a”,“a”),
选项卡项(“报告2”,h1(id=“b”,“b”),
选项卡项(“第2节”,h1(id=“c”,“c”))
)
)
服务器多亏了这一点,我找到了问题的答案。根本不需要使用
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)   
库(闪亮)
图书馆(shinydashboard)
图书馆(shinyjs)
ui=仪表板页面(
仪表板标题(title=“shinny”),
仪表板侧栏(
侧边栏菜单(id=“tabs”,
menuItem(“Section_1”,tabName=“Section_1”,icon=icon(“对齐对齐”),
startExpanded=TRUE,selected=TRUE,
menuSubItem(“第1小节”,tabName=“报告1”,selected=TRUE),
menuSubItem(“第2小节”,tabName=“报告2”),
菜单项(“Section_2”,tabName=“Section_2”,icon=icon(“对齐对齐”))
)
),
仪表板主体(
useShinyjs(),
tabItems(
选项卡项(“报告1”,h1(id=“a”,“a”),
选项卡项(“报告2”,h1(id=“b”,“b”),
选项卡项(“第2节”,h1(id=“c”,“c”))
)
)
服务器