conditionalPanel selectively ignore conditions when multiple sidebar input IDs are set

shinydashboard

#1

Hi,

I am seeing some weird behavior in conditionalPanel and I am not sure why. It happens when I assign two sidebar input IDs for conditionalPanel, one for menuItem, one for tabPanel, then conditionalPanel starts to selectively ignoring the conditions I set in some cases.

Here is an example: Link for the example shinydashboard

I am trying to have the default selection of comparison date picker change based on the menuItems and tabs selected. In all of the cases, there should only be one comparison date picker, based on the conditions I set for conditionalPanel. However, in menu3, comparison date picker for menu2 also appears (depend on which tab is last selected in menu2, the extra date picker will be either last year or last week); in menu1, if tab2 in menu2 is selected before clicking on menu1, an additional comparison date picker will also be shown.

Here is the app.R:

library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "contidionalPanel issue",
                  titleWidth = 300),
  
  dashboardSidebar(width = 300,
                   sidebarMenu(id="menu",
                               
                               menuItem("Menu1", tabName = "menu1"),
                               
                               menuItem("Menu2", tabName = "menu2"),
                               
                               menuItem("Menu3", tabName = "menu3"),
                               
                               hr(),
                               
                               dateRangeInput("dateRange1", "Date range:",
                                              start = "2018-01-01",
                                              end = "2018-01-07",
                                              min = "2018-01-01",
                                              max = "2018-01-30",
                                              weekstart = 1),
                               
                               conditionalPanel(
                                 condition = "input.menu == 'menu1' || input.tab == 'tab1'",
                                 uiOutput("dateRange_compare1")
                                 ),
                               
                               conditionalPanel(
                                 condition = "input.tab == 'tab2'",
                                 uiOutput("dateRange_compare2")
                                 ),
                               
                               conditionalPanel(
                                 condition = "input.menu == 'menu3'",
                                 uiOutput("dateRange_compare3")
                                 )
                               )
                   ),
  
  dashboardBody(
    tabItems(
      tabItem(tabName = "menu1",
              h2("Menu 1"),
              p("Comparison date range defaults to last year. (Should only see one comparison date picker.)")
              ),
      
      tabItem(tabName = "menu2",
              h2("Menu 2"),
              
              tabBox(id="tab",
                     width = 12,
                     
                     tabPanel("Tab 1", value = "tab1",
                              h2("Comparison date range defaults to last year.")
                     ),
                     
                     tabPanel("Tab 2", value = "tab2",
                              h2("Comparison date range defaults to last week.")
                              )
                     )
              ),
      
      tabItem(tabName = "menu3",
              h2("Menu 3"),
              p("Comparison date range defaults to previous day. (Should only see one comparison date picker.)")
              )
      )
  )
)

server <- function(input, output) {
  output$dateRange_compare1 <- renderUI({
    
    start <- input$dateRange1[1] - 52L * 7L
    end <- input$dateRange1[2] - 52L * 7L
    min <- "2017-01-01"
    max <- Sys.Date() - 1
    
    dateRangeInput("dateRange_compare1",
                   "Date range comparison (last year): ",
                   start = start,
                   end = end,
                   min = min,
                   max = max,
                   weekstart = 1
    )
  })
  
  output$dateRange_compare2 <- renderUI({
    
    start <- input$dateRange1[1] - 7L
    end <- input$dateRange1[2] - 7L
    min <- "2017-01-01"
    max <- Sys.Date() - 1
    
    dateRangeInput("dateRange_compare2",
                   "Date range comparison (last week): ",
                   start = start,
                   end = end,
                   min = min,
                   max = max,
                   weekstart = 1
    )
  })
  
  output$dateRange_compare3 <- renderUI({
    
    start <- input$dateRange1[1] - 1
    end <- input$dateRange1[2] - 1
    min <- "2017-01-01"
    max <- Sys.Date() - 1
    
    dateRangeInput("dateRange_compare3",
                   "Date range comparison (previous day): ",
                   start = start,
                   end = end,
                   min = min,
                   max = max,
                   weekstart = 1
    )
  })
}

shinyApp(ui = ui, server = server)

Can someone help me please?


#2

Hi @huiyan

I'd update the condition to be more restrictive.

Maybe something like the conditions below for each of the condition panels

  • input.menu == 'menu1' || (input.menu == 'menu2' && input.tab == 'tab1')
  • input.menu == 'menu2' && input.tab == 'tab2'
  • input.menu == 'menu3'

Hope this helps!

- Barret


#3

Ha! I see the problem now! Thank you so much!!! :smiley: