skip to Main Content

Below I have provided the skeleton of a Shiny Dashboard that I am trying to create. I’ve fought with this for quite a while and cannot figure out how to make it work.

App Current Behavior: Right now the "Select Date Range" date input is visible when the Menu Item 1 sidebar item is selected.

Desired Behavior: I would like the "Select Date Range" input to only be visible when "Menu Item 1" and "Tab 1" are selected simultaneously.

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("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(
              tabPanel("Tab 1"),
              tabPanel("Tab 2")
            )),
    tabItem(tabName="menu2")
  )
)

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

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

shinyApp(ui,server)

I have tried many different attempts involving different conditions in the conditionalPanel but nothing has worked.

2

Answers


  1. If your inner tabs an ID

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

    and then use that in the condition for rendering

    conditionalPanel(
          condition="input.sidebarID =='menu1' & input.innerTab=='Tab 1'",
          ...
    )
    
    Login or Signup to reply.
  2. To work conditional on your tabsetPanel add an id and use this in the condition like here:

    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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search