简体   繁体   中英

Show pickerInput depending on checkBoxInput result R shiny

I am trying to make a dynamic UI for my shiny dashboard. Here, I want to show a pickerInput field only when the input in a checkboxGroup is a specific value. For example, when the input from the checkboxGroup field is A , I want to show the pickerInput field, otherwise I want to show a different input field.

Currently, the part of my code looks, using conditionalPanel , like the following:

output$UI_selection <- renderUI({


tagList(
  p(tags$i("Define the network")),

  checkboxGroupInput(inputId = "choice1", 
                   label = "Make a choice", 
                   choices = list("A", "B")
  ),
  conditionalPanel(condition = "input$choice1 == 'A'",
  pickerInput(inputId = "select1",
              label = "Select first:",
              choices = list(
                "Hierarchies" = grouplist_1),
              selected = NULL,
              options = list(`actions-box` = TRUE, `none-selected-text` = "Select hierarchy", `live-search` = TRUE, title = "Select hierarchy"),
              multiple = FALSE
   )
 ) 

 )
})

However, this doesn't work and shows both the checkboxGroupInput as well as the PickerInput . Does anyone know how to fix this?

The shiny package functions (such as conditionalPanel ) translate all of the R language code you supply into JS. Conditions you supply in conditionalPanel need to be interpretable in JS, which uses . in place of $ .

You need to replace your condition = "input$choice1 == 'A'" with condition = "input.choice1 == 'A'" .

Full working app is here:

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  uiOutput("UI_selection")
)

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

  output$UI_selection <- renderUI({
    tagList(
      p(tags$i("Define the network")),

      checkboxGroupInput(inputId = "choice1", 
                         label = "Make a choice", 
                         choices = list("A", "B")
      ),
      conditionalPanel(condition = "input.choice1 == 'A'",
                       pickerInput(inputId = "select1",
                                   label = "Select first:",
                                   choices = list(
                                     "Hierarchies" = c("X","Y","Z")),
                                   selected = NULL,
                                   options = list(`actions-box` = TRUE, `none-selected-text` = "Select hierarchy", `live-search` = TRUE, title = "Select hierarchy"),
                                   multiple = FALSE
                       )
      ) 
    )
  })
}

shinyApp(ui, server)

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