简体   繁体   中英

Shiny conditionalpanel not evaluating at startup

I'm using conditionalpanels to build up my Shiny app dynamically. It seems that the condition the panel is based on is not evaluated at startup. The panel is visible by default, and only disappears after some actions. The panel is visible, even though the condition is not met (input.select1.length = 0, which is less than 1...).

Minimal working example:

Server.R:

shinyServer(function(input, output,session){
  output$selectInput1 <- renderUI({
    selectInput(
      inputId = "select1",
      label = "Select",
      choices = c('1','2','3'),
      selected = NULL,
      multiple = TRUE
    )
  })
})

UI.R:

dashboardPage(
  title = "",

  ## Header content + dropdownMenu
  dashboardHeader(
    title = tags$b(""),
    titleWidth = 250
  ),

  ## Sidebar content
  dashboardSidebar(
    width = 250,
    sidebarMenu(
      id = "tabs",
      menuItem("tab1", tabName = "tab", icon = icon("table"))
    )
  ),

  ## Body content
  dashboardBody(
    tabItems(
      tabItem(tabName = "tab",
        div(
          div(
            style="float: left; padding-left: 20px; padding-right: 20px; width: 350px; background-color: #F4F4F4; height: calc(100vh - 50px) !important;",
            uiOutput('selectInput1')
          ),
          div(
            conditionalPanel(
              condition = "input.select1.length > 1",
              p('hi')
            )
          )
        )
      )
    )
  )
)

As @porkchop pointed out, you can create the condition in the renderUI function of server.R :

Server.R:

shinyServer(function(input, output,session){
  output$selectInput1 <- renderUI({
    selectInput(
      inputId = "select1",
      label = "Select",
      choices = c('1','2','3'),
      selected = NULL,
      multiple = TRUE
    )
  })
  output$panel1 <- renderUI({
    if(!is.null(input$select1) && length(input$select1) > 0){
      condition1 = 'true'
    } else {
      condition1 = 'false'
    }
    conditionalPanel(
      condition1,
      p('hi')
    )
  })
})

UI.R:

dashboardPage(
  title = "",

  ## Header content + dropdownMenu
  dashboardHeader(
    title = tags$b(""),
    titleWidth = 250
  ),

  ## Sidebar content
  dashboardSidebar(
    width = 250,
    sidebarMenu(
      id = "tabs",
      menuItem("tab1", tabName = "tab", icon = icon("table"))
    )
  ),

  ## Body content
  dashboardBody(
    tabItems(
      tabItem(tabName = "tab",
        div(
          div(
            style="float: left; padding-left: 20px; padding-right: 20px; width: 350px; background-color: #F4F4F4; height: calc(100vh - 50px) !important;",
            uiOutput('selectInput1')
          ),
          div(
            uiOutput('panel1')
          )
        )
      )
    )
  )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM