【问题标题】:Disable an item in selectInput/selectizeInput禁用 selectInput/selectizeInput 中的项目
【发布时间】:2020-08-11 21:04:09
【问题描述】:

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

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

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

【问题讨论】:

    标签: r shiny selectinput


    【解决方案1】:

    你可以这样做:

    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 会更容易;)

    【讨论】:

    • 哈哈哈,确实如此。但是非常感谢您提供的代码,看起来完全符合我的需要,并且应该很容易适应我!
    猜你喜欢
    • 2019-01-24
    • 1970-01-01
    • 2019-03-20
    • 2015-05-12
    • 2019-02-13
    • 1970-01-01
    • 2017-12-06
    • 2021-11-01
    • 2018-03-13
    相关资源
    最近更新 更多