You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RShiny中无法移除侧边栏下拉箭头的问题咨询

解决Shinydashboard侧边栏无下拉箭头的固定标题问题

问题核心

  1. 包含menuSubItem的menuItem会默认显示下拉箭头,常规样式调整无法直接隐藏
  2. 替换menuSubItem为menuItem后出现"body is missing"错误,是因为dashboardPage必须完整包含dashboardHeader、dashboardSidebar和dashboardBody三个核心组件,此前代码未将dashboardBody正确嵌入到dashboardPage内部

两种可行解决方案

方案1:用CSS隐藏下拉箭头(保留原有菜单层级)

通过自定义CSS规则精准定位侧边栏的箭头元素,直接隐藏:

app_ui <- shiny::fluidPage(
  theme = shinythemes::shinytheme("united"),
  tags$head(
    tags$style(".navbar {margin-bottom: 0px;}"),
    tags$style(".container-fluid {padding-right: 0px; padding-left: 0px;}"),
    # 隐藏带子项的menuItem下拉箭头
    tags$style(".sidebar-menu li.treeview > a > .pull-right-container {display: none;}")
  ),
  navbarPage(
    title = div(img(src = "www/bftb_logo_v8_bare.png", height = "30px"), "AZ Oncology Bioinformatics Toolbox"),
    windowTitle = "BFTB Landing Page",
    tabPanel("Toolbox", icon = icon("wrench"), disable = TRUE,
             shinydashboard::dashboardPage(
               header = shinydashboard::dashboardHeader(title = "   ", titleWidth = 300, disable = TRUE),
               shinydashboard::dashboardSidebar(
                 width = 300 ,
                 shinydashboard::sidebarMenu(
                   shinydashboard::menuItem(
                     "Tools",
                     tabName = "tools_app",
                     icon = icon("wrench"),
                     shinydashboard::menuSubItem(
                       "Gene Expression/Signature/Pathways",
                       tabName = "gene_app",
                       icon = icon("chart-line")
                     )
                   )
                 )
               ),
               # 正确嵌入dashboardBody到dashboardPage中
               shinydashboard::dashboardBody(
                 shinydashboard::tabItems(
                   shinydashboard::tabItem("tools_app", mod_tools_path_ui("tools_path_ui_1")),
                   shinydashboard::tabItem("gene_app", mod_gene_expressions_sign_path_ui("gene_expression_sign_path_ui"))
                 )
               )
             )
    )
  )
)

方案2:重构菜单结构(将Tools设为纯标题)

如果不需要Tools作为可点击的tab项,直接用自定义标签实现和menuItem风格一致的固定标题:

app_ui <- shiny::fluidPage(
  theme = shinythemes::shinytheme("united"),
  tags$head(
    tags$style(".navbar {margin-bottom: 0px;}"),
    tags$style(".container-fluid {padding-right: 0px; padding-left: 0px;}"),
    # 自定义标题样式,匹配默认menuItem的视觉效果
    tags$style(".sidebar-title {padding: 12px 15px; font-size: 14px; font-weight: bold; color: #b8c7ce;}")
  ),
  navbarPage(
    title = div(img(src = "www/bftb_logo_v8_bare.png", height = "30px"), "AZ Oncology Bioinformatics Toolbox"),
    windowTitle = "BFTB Landing Page",
    tabPanel("Toolbox", icon = icon("wrench"), disable = TRUE,
             shinydashboard::dashboardPage(
               header = shinydashboard::dashboardHeader(title = "   ", titleWidth = 300, disable = TRUE),
               shinydashboard::dashboardSidebar(
                 width = 300 ,
                 shinydashboard::sidebarMenu(
                   # 纯标题元素,无点击事件和箭头
                   tags$div(class = "sidebar-title", icon("wrench"), "Tools"),
                   # 直接将原子项设为一级menuItem
                   shinydashboard::menuItem(
                     "Gene Expression/Signature/Pathways",
                     tabName = "gene_app",
                     icon = icon("chart-line")
                   )
                 )
               ),
               shinydashboard::dashboardBody(
                 shinydashboard::tabItems(
                   shinydashboard::tabItem("gene_app", mod_gene_expressions_sign_path_ui("gene_expression_sign_path_ui"))
                 )
               )
             )
    )
  )
)

关键注意点

  • dashboardPage的三个组件(header/sidebar/body)必须作为参数完整传入,缺失任何一个都会触发"body is missing"错误
  • 方案1的CSS规则仅针对带子项的menuItem箭头,不会影响其他正常箭头元素
  • 方案2的自定义标题更简洁,适合不需要Tools作为功能tab的场景

内容的提问来源于stack exchange,提问作者GaB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 09:07:11