R shiny dashboard,仅当特定侧边栏menuItem中的特定tabPanel被选中时才显示conditionalPanel

lh80um4z  于 2024-01-03  发布在  其他
关注(0)|答案(2)|浏览(109)

下面我提供了一个闪亮的 Jmeter 板,我试图创建的 backbone 。我已经与此斗争了相当长的一段时间,不知道如何使它工作。

**应用程序当前行为:**现在,当选择菜单项1侧边栏项时,“选择日期范围”日期输入可见。
**期望的行为:**我希望“选择日期范围”输入仅在同时选择“菜单项1”和“选项卡1”时可见。

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(lubridate)

generate_dates <- function(start_date, end_date) {
  all_dates <- seq(start_date, end_date, by = "days")
  all_mondays <- all_dates[weekdays(all_dates) == "Monday"]
  return(all_mondays)
}

start_date <- floor_date(as.Date("2023-07-01"), unit = "week", week_start = 1)
end_date <- floor_date(as.Date("2023-12-06"), unit = "week", week_start = 1)
dates <- generate_dates(start_date, end_date)

df <- data.frame(
  Week = dates,
  Value_1 = sample(c("A", "B", "C", "D"), length(dates), replace = TRUE),
  Value_2 = sample(c("X", "Y", "Z", "W"), length(dates), replace = TRUE)
)

sidebar <- dashboardSidebar(
  sidebarMenu(
    id= "sidebarID",
    conditionalPanel(
      condition="input.sidebarID =='menu1'",
      dateRangeInput("date_range","Select Date Range",
                     start=max(df$Week),
                     end=max(df$Week),
                     min=max(df$Week),
                     max=max(df$Week),
                     format="yyyy-mm-dd"
                     )
    ),
    menuItem("Menu Item 1",tabName="menu1"),
    menuItem("Menu Item 2",tabName="menu2")
  )
)

body<- dashboardBody(
  tabItems(
    tabItem(tabName="menu1",
            tabsetPanel(
              tabPanel("Tab 1"),
              tabPanel("Tab 2")
            )),
    tabItem(tabName="menu2")
  )
)

ui<-dashboardPage(
  dashboardHeader(title="Navigation"),
  sidebar,
  body
)

server<-function(input,output,session){
  
  
  
}

shinyApp(ui,server)

字符串
我已经尝试了许多不同的尝试,包括在conditionalPanel中的不同条件,但没有任何工作。

rkttyhzu

rkttyhzu1#

如果你的内标签是一个ID

body<- dashboardBody(
  tabItems(
    tabItem(tabName="menu1",
            tabsetPanel(id="innerTab",
              tabPanel("Tab 1"),
              tabPanel("Tab 2")
            )),
    tabItem(tabName="menu2")
  )
)

字符串
然后在条件中使用它来渲染

conditionalPanel(
      condition="input.sidebarID =='menu1' & input.innerTab=='Tab 1'",
      ...
)

9ceoxa92

9ceoxa922#

要在tabsetPanel上有条件地工作,请添加一个id,并在条件中使用它,如下所示:

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(lubridate)

generate_dates <- function(start_date, end_date) {
  all_dates <- seq(start_date, end_date, by = "days")
  all_mondays <- all_dates[weekdays(all_dates) == "Monday"]
  return(all_mondays)
}

start_date <- floor_date(as.Date("2023-07-01"), unit = "week", week_start = 1)
end_date <- floor_date(as.Date("2023-12-06"), unit = "week", week_start = 1)
dates <- generate_dates(start_date, end_date)

df <- data.frame(
  Week = dates,
  Value_1 = sample(c("A", "B", "C", "D"), length(dates), replace = TRUE),
  Value_2 = sample(c("X", "Y", "Z", "W"), length(dates), replace = TRUE)
)

sidebar <- dashboardSidebar(
  sidebarMenu(
    id = "sidebarID",
    conditionalPanel(
      condition = "input.sidebarID === 'menu1' && input.tabsetID === 'tab1'",
      dateRangeInput("complaints_date_range", "Select Date Range",
                     start = max(df$Week),
                     end = max(df$Week),
                     min = max(df$Week),
                     max = max(df$Week),
                     format = "yyyy-mm-dd"
      )
    ),
    menuItem("Menu Item 1", tabName = "menu1"),
    menuItem("Menu Item 2", tabName = "menu2")
  )
)

body <- dashboardBody(
  tabItems(
    tabItem(tabName = "menu1",
            tabsetPanel(
              id = "tabsetID",  # add ID to the tabsetPanel
              tabPanel("Tab 1", value = "tab1"),  # add value to Tab 1
              tabPanel("Tab 2", value = "tab2")   # add value to Tab 2
            )
    ),
    tabItem(tabName = "menu2")
  )
)

ui <- dashboardPage(
  dashboardHeader(title = "Navigation"),
  sidebar,
  body
)

server <- function(input, output, session) { 
  
}

shinyApp(ui,server)

字符串

相关问题