Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable an item in selectinput dropdown

Tags:

shiny

How do I disable an item in a selectInput() dropdown menu?

For example;

library(shiny)
ui <- fluidPage(selectInput("id1","Select",choices=c("A","B","C")))
server <- function(input, output) {}
shinyApp(ui, server)

enter image description here

Say, for whatever reason, option C cannot be selected due to some logic. I would like the user to be able to see all the options, but have option C disabled/unselectable.

like image 273
rmf Avatar asked Oct 19 '25 18:10

rmf


1 Answers

You can do this using pickerInput from package shinyWidgets :

library(shiny)
library(shinyWidgets)
ui <- fluidPage(
  pickerInput(
    inputId = "id1",
    label = "Select :",
    choices = c("A", "B", "C"),
    multiple = FALSE,
    choicesOpt = list(
      disabled = c("A", "B", "C") %in% c("C")
    )
  ),
  verbatimTextOutput(outputId = "result")
)
server <- function(input, output) {
  output$result <- renderPrint(input$id1)
}
shinyApp(ui, server)

That's also possible to do that from the server, see example in ?updatePickerInput

like image 59
Victorp Avatar answered Oct 23 '25 18:10

Victorp