rshinyselectize.js

How to programmatically disable an individual entry in a selectizeInput?


I'm aware of almost identical questions that were already raised here:

However I want to use only plain shiny and no shinyWidgets and I want to have a generic solution (not only a more or less involved patch).

Inspired by this thread: How to make a specific option unselectable programmatically with Selectize.js? and after having read the selectize.js relevant manual page, I came up with this quite naive, but also somehow logical IMO, snippet:

library(shiny)

YE_Range <- 2020:2025

ui <- fluidPage(
    selectizeInput(
      inputId = "YE", 
      label = "Year End", 
      choices = list(
        'text' = YE_Range, 
        'disabled' = c(TRUE, FALSE, FALSE, FALSE, TRUE, TRUE)
      ), 
      multiple = FALSE, 
      options = list(placeholder = 'Choose a year end'))
    )

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

runApp(list(ui = ui, server = server), launch.browser = TRUE)

But it doesn't work. Do you know why?


Solution

  • It's not working because you are trying to set different choices with disabled. Instead, you can pass an option list into the options parameter of selectizeInput() and apply the disabledField property as explained in your linked question:

    library(shiny)
    
    opts <- c(TRUE, FALSE, FALSE, FALSE, TRUE, TRUE) |> 
      lapply(\(x) list(disabled = x))
    
    ui <- fluidPage(
      selectizeInput(
        inputId = "YE", 
        label = "Year End", 
        choices = list(
          'text' = 2020:2025
        ), 
        multiple = FALSE, 
        options = list(
          placeholder = 'Choose a year end', 
          disabledField = "disabled",
          options = opts
        )
      )
    )
    
    shinyApp(ui, \(...){})
    

    enter image description here