Reputation: 125
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?
Upvotes: 1
Views: 666
Reputation: 3532
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)
Upvotes: 3