首页 > 解决方案 > 禁用 selectInput/selectizeInput 中的项目

问题描述

我的问题和这个问题差不多:Disable an item in selectinput dropdown

我想禁用 selectInput 菜单中的项目,类似于 pickerInput 所做的。但是我不想使用pickerInput,我想使用selectInput/selectizeInput。

其中一个回复提到您可以使用 HTML 将禁用的选项与您要禁用的特定值相关联。任何人都可以在下面的代码外壳中提供如何在 Shiny 中执行此操作的示例代码吗?它会类似于这个线程中的条件格式吗?R Shiny:条件格式选择输入项

library(shiny)

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

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

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

shinyApp(ui = ui, server = server)

标签: rshinyselectinput

解决方案


你可以做:

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)

但是使用shinyWidgets::pickerInput会容易得多;)


推荐阅读