1

My question is pretty much the same as this question: Disable an item in selectinput dropdown

I would like to disable items in the selectInput menu, similar to what pickerInput does. However I do not want to use pickerInput, I want to use selectInput/selectizeInput.

One of the responses mentions that you could use HTML to associate a disabled option with the particular value you want to disable. Would anyone be able to provide example code of how to do that in Shiny in the code shell below? Would it be similar to conditional formatting in this thread? R Shiny: Conditional formatting selectInput items

library(shiny)

choices <- c("x", "y", "z")

ui <- fluidPage(
    selectizeInput("choices","Choices", choices = choices)
)

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

shinyApp(ui = ui, server = server)
PeterJ
  • 79
  • 5

1 Answers1

1

You can do:

library(shiny)

choices <- c("x", "y", "z")

ui <- fluidPage(
  selectizeInput(
    "choices", "Choices", choices = choices,
    options = list(
      render = I(
        "{
           option: function(item, escape) {
                      if (item.value === 'y') {
                        return '<div style=\"pointer-events: none; color: #aaa;\">' + escape(item.label) + '</div>';
                      }
                      return '<div>' + escape(item.label) + '</div>';
                 }
        }"
      )
    )
  )
)

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

shinyApp(ui = ui, server = server)

But it's much easier with shinyWidgets::pickerInput ;)

Victorp
  • 13,636
  • 2
  • 51
  • 55
  • Hahaha, indeed it is. But thank you very much for that code, looks to do exactly what I need and should be easily adaptable for me! – PeterJ Apr 28 '20 at 08:13
  • This is a good simple solution for those who want to avoid pulling in shinyWidgets complexity. Worked like a charm, thank you! – Danny Jun 01 '22 at 02:41