rstudio / shinydashboard

Shiny Dashboarding framework
https://rstudio.github.io/shinydashboard/
Other
887 stars 299 forks source link

conditionalPanel selectively ignore conditions when multiple sidebar input IDs are set #296

Closed wanhuiyan closed 5 years ago

wanhuiyan commented 5 years ago

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 the link for the example dashboard: link

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.

And here is the code: 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)
schloerke commented 5 years ago

Answered here: https://community.rstudio.com/t/conditionalpanel-selectively-ignore-conditions-when-multiple-sidebar-input-ids-are-set/15188/2?u=barret

wanhuiyan commented 5 years ago

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