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

如何在Shiny Dashboard右侧侧边栏根据标签页选择动态切换内容,并仅在指定标签页显示controlbarMenu()

Got it, let's adjust your Shiny app so the right controlbar updates dynamically with each tab, and only shows the controlbarMenu when you're on the "Tax Loss Harvesting" tab. Here's the step-by-step solution:

Key Changes Made:

  1. Conditional Controlbar Content: We wrap the entire controlbar content in conditionalPanel elements that check the current selected tab (input.tabs). This ensures only the relevant content appears for each tab.
  2. Simplified Server Logic: Removed redundant conditionalPanel calls inside the renderUI functions—since the controlbar's top-level conditionals already restrict when those UI elements are visible, we don't need to double up on checks.
  3. Added ControlbarMenu for Third Tab: Placed the controlbarMenu inside its own conditionalPanel that only triggers when the "Tax Loss Harvesting" tab is active.

Modified Full Code:

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

shinyApp(
  ui = tags$body(class="skin-blue sidebar-mini control-sidebar-open",
                 dashboardPage(
                   options = list(sidebarExpandOnHover = TRUE),
                   header = dashboardHeader(title = "Investment Advisor Monitoring - Insider Trading", titleWidth = 450),
                   sidebar = dashboardSidebar(minified = F, collapsed = F, h4("Investment Selected") ),
                   body = dashboardBody(
                     h3('Results'),
                     tabsetPanel(id = "tabs",
                                 tabPanel("InsiderTraining"),
                                 tabPanel("Switching"),
                                 tabPanel("Tax Loss Harvesting")
                     )
                   ),
                   controlbar = dashboardControlbar(width = 300,
                     # Content for InsiderTraining tab
                     conditionalPanel(
                       condition = "input.tabs === 'InsiderTraining'",
                       h4("Insider Trading Parameters"),
                       uiOutput("mytab21")
                     ),
                     # Content for Switching tab
                     conditionalPanel(
                       condition = "input.tabs === 'Switching'",
                       h4("Switching Parameters"),
                       uiOutput("mytab22")
                     ),
                     # Content for Tax Loss Harvesting tab (controlbarMenu only here)
                     conditionalPanel(
                       condition = "input.tabs === 'Tax Loss Harvesting'",
                       controlbarMenu(
                         id = "tlh_menu",
                         controlbarItem(
                           "TLH Configuration",
                           tagList(
                             selectInput("tlh_strategy", "Harvesting Strategy", c("Aggressive" = "aggressive", "Conservative" = "conservative")),
                             sliderInput('tlh_threshold', 'Loss Threshold (%)', min=1, max=20, value=5)
                           )
                         ),
                         controlbarItem(
                           "TLH History",
                           "View past tax loss harvesting transactions here."
                         )
                       )
                     )
                   ),
                   title = "DashboardPage"
                 )),
  server = function(input, output) {
    # InsiderTraining tab controlbar elements
    output$mytab21 <- renderUI({
      tagList(
        selectInput("InsiderTradingModel", "Insider Trading Model", c("Dynamic" = "Dynamic", "AI based" = "AIbased")),
        selectInput("ivar", "Choose a variable", choices = colnames(iris))
      )
    })
    
    # Switching tab controlbar elements
    output$mytab22 <- renderUI({
      tagList(
        selectInput("InsiderTradingModel2", "Insider Trading Model 2", c("Dynamic" = "Dynamic", "BI based" = "BIbased")),
        sliderInput('periodss','Periods',min=1,max=100,value=30),
        selectInput("pvar", "Choose a variable", choices = colnames(pressure))
      )
    })
  }
)

How It Works:

  • When you switch to "InsiderTraining", the controlbar shows the insider trading parameters.
  • Switching to "Switching" displays the switching-specific controls.
  • Only when you select "Tax Loss Harvesting" does the controlbarMenu appear with its two tabs and custom components.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 23:27:35