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

如何在R Shiny的navbarMenu所有tabPanel中插入通用内容?

Fix: Show Common Content Only in navbarMenu Tabs

Got it, let's fix this issue for you! The problem here is that you've placed your common fluidRow(column(3,"hello")) directly as a child of navbarMenu, instead of nesting it inside each tabPanel under that menu. Shiny treats any non-tabPanel elements directly inside navbarMenu as global content that shows up across all tabs in the entire navbarPage—that's why you're seeing it in "Home" and "Histogram" too.

Solution 1: Reuse a Common UI Component

First, define your common content as a reusable object outside the UI. This keeps your code DRY (Don't Repeat Yourself):

# Define your shared content once
common_exploratory_content <- fluidRow(column(3, "hello"))

Then, add this component to each tabPanel inside your navbarMenu:

ui <- fluidPage(
  sidebarPanel(
    selectInput("variable", "Select an SKU:", materials, selected = '')
  ),
  mainPanel(
    navbarPage("DEMAND PREDICTION!", theme = shinytheme("flatly"),
      tabPanel("Home", h1("Time series forecasting"), verbatimTextOutput("summary_1") ),
      tabPanel("Histogram", 
        fluidRow(column(3, wellPanel(selectInput("variable", "Select an SKU:", materials, selected = ''), selectInput("market", "Select a market:", choices = NULL) )) ),
        mainPanel( plotlyOutput("hist"), h4("Sales per Country"),plotOutput("map") )
      ),
      navbarMenu("Exploratory Analysis",
        tabPanel("Line graph", plotlyOutput("exploratory"), common_exploratory_content),
        tabPanel("Box plot", plotlyOutput("boxplot"), common_exploratory_content),
        tabPanel("Seasonal plot", plotlyOutput("seasonal"), common_exploratory_content)
      )
    )
  )
)

Now the "hello" content will only appear in the three tabs under Exploratory Analysis, and won't show up in other tabs like "Home" or "Histogram".

Solution 2: Use a Helper Function for Maintainability

If you plan to add more tabs to the navbarMenu later, create a helper function to generate tabPanels with the common content automatically:

# Helper function to build exploratory tabs with shared content
exploratory_tab <- function(tab_title, plot_output_id) {
  tabPanel(
    tab_title,
    plotlyOutput(plot_output_id),
    fluidRow(column(3, "hello")) # Your common content embedded here
  )
}

Then use this function in your navbarMenu:

navbarMenu("Exploratory Analysis",
  exploratory_tab("Line graph", "exploratory"),
  exploratory_tab("Box plot", "boxplot"),
  exploratory_tab("Seasonal plot", "seasonal")
)

This makes your code easier to update—any changes to the common content only need to be made in one place.

Quick Side Note

You have duplicate selectInput("variable") elements in both the sidebar and the Histogram tab. While Shiny will handle the duplicate input ID, it's better to remove one to avoid user confusion and keep your code clean.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 11:52:41