【问题标题】:How to trigger action on clicking in menuItem?如何触发点击menuItem的动作?
【发布时间】:2021-06-30 03:19:52
【问题描述】:

我想在点击menuItem 时触发一些动作。我在这里使用observe:当我单击“驱动程序”项目时,我希望在控制台中返回一些文本。不幸的是,当我运行该应用程序时,出现了错误:if 中的错误:参数长度为 0。我的menuItem 存在,id 也可以,所以不知道为什么会出现这个错误。 这是可重现的代码(observe 在我的代码底部):

library(shiny)
library(bs4Dash)
library(leaflet)


bodyTag <- dashboardBody(
  tags$head(
    tags$style(
      "#map {
          height: calc(100vh - 57px) !important;
        }"
    )
  ),
  
  tabItems(
    tabItem(
      tabName = "live",
      box(
        title = "LIVE",
        id = "panel",
        height = 450,
        collapsible = TRUE
      )
    ),
    
    tabItem(
      tabName = "drivers",
      box(
        title = "Drivers",
        id = "panel",
        height = 450,
        collapsible = TRUE
      )
    ),
    tabItem(
      tabName = "customers",
      box(
        title = "Customers",
        id = "panel",
        height = 450,
        collapsible = TRUE
      )
    )    
  ),

  leafletOutput("map")
)



ui <- dashboardPage(
  dark = TRUE,
  
  header = dashboardHeader(
    title = h5("DEMO app")
  ),
  
  
  sidebar = dashboardSidebar(
    fixed = TRUE,
    collapsed = TRUE,
    expandOnHover = FALSE,
    status = "purple",
    
    customArea = fluidRow(
      actionButton(
        inputId = "myAppButton",
        label = NULL,
        icon = icon("users"),
        width = NULL,
        status = "primary",
        style = "margin: auto",
        dashboardBadge(1, color = "danger")
      )
    ),
    
    sidebarMenu(
      id = "sidebarID",
      menuItem("Live", tabName = "live", icon = icon("circle")),
      menuItem("Drivers", tabName = "drivers", icon = icon("user-friends")),
      menuItem("Customers", tabName = "customers", icon = icon("building"))
    )
  ),
  body = bodyTag
)



server <- function(input, output) {
  observeEvent(input$sidebarID, {
    updateBox("panel", action = "toggle")
  })
  

  output$map <- renderLeaflet({
    leaflet() %>%
      setView(lng = -73.98928, lat = 40.75042, zoom = 6) %>%
      addProviderTiles("CartoDB.Positron")
  })
  
   
  # the problem is here
  observe({
    
    if(input$tabs == "drivers") {
      print("Drivers")
      #print(input$tabs)
    } else {
      print("other tabs")
    }
  })

}

shinyApp(ui = ui, server = server)

我很确定 input$tabs 是我应该如何到达给定的 menuItem 但也许我错了。

【问题讨论】:

    标签: r shiny reactive-programming shinydashboard


    【解决方案1】:

    你错了。此问题的许多其他已发布解决方案使用tabs 作为侧边栏菜单的 id,但您不这样做:

        sidebarMenu(
          id = "sidebarID",
          menuItem("Live", tabName = "live", icon = icon("circle")),
          menuItem("Drivers", tabName = "drivers", icon = icon("user-friends")),
          menuItem("Customers", tabName = "customers", icon = icon("building"))
        )
    
    

    所以你需要

      observe({
        if(input$sidebarID == "drivers") {
          print("Drivers")
          #print(input$tabs)
        } else {
          print("other tabs")
        }
      })
    

    这是一个简单的错字。

    【讨论】:

    • 好的,所以我需要使用侧边栏 ID 而不是标签 ID。感谢您的澄清!
    猜你喜欢
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多