【问题标题】:Shinyjs: How to detect if a user clicked on a tab?Shinyjs:如何检测用户是否单击了选项卡?
【发布时间】: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


    【解决方案1】:

    感谢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)   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 1970-01-01
      • 2011-06-10
      相关资源
      最近更新 更多