简体   繁体   English

单击复选框输入后将 selectInput 选项变灰 - R Shiny

[英]Grey out a selectInput choice after clicking a checkboxInput - R Shiny

I am trying to grey out a selectInput choice from the dropdown after selecting it from the dropdown and clicking a checkboxInput.从下拉列表中选择并单击复选框输入后,我试图将下拉列表中的 selectInput 选项变灰。 Below is some example code where I use shinyjs::disable.下面是一些我使用 shinyjs::disable 的示例代码。 However, the disable function disables the whole selectInput before I have even selected the checkbox.但是,在我选择复选框之前,禁用 function 会禁用整个 selectInput。 I would like the selectInput current choice to be disabled after the checkbox is selected:我希望在选中复选框后禁用 selectInput 当前选项

library(shiny)

ui <- fluidPage(

  shinyjs::useShinyjs(),

  sidebarLayout(

    sidebarPanel(
      selectInput("name","Pick Name:",c("Alice","Austin"),selected = "Alice"),
      br()
      ,width = 3),

    mainPanel(

      tabsetPanel(

        tabPanel(
          "Tab",
          br(),
          checkboxInput("complete","Complete?"),
          br()
        )

      )

    )
  )
)

server <- function(input, output) {

   observeEvent(input$complete, shinyjs::disable("name"))

}

# Run the application 
shinyApp(ui = ui, server = server)

Also, does anyone know how this functionality can be applied across multiple sessions?另外,有谁知道如何在多个会话中应用此功能?

To disable the select options we can using shinyWidgets package and its pickerinput , such as:要禁用 select 选项,我们可以使用shinyWidgets package 及其pickerinput ,例如:

library(shiny)
library(shinyWidgets)

names <- c("Alice","Austin","Alex")

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            pickerInput("myname","Pick Name:",choices = names),
            br(),width = 3),

        mainPanel(
            tabsetPanel(
                tabPanel(
                    "Tab",
                    br(),
                    checkboxInput("complete","Complete?",FALSE),
                    br()
                )
            )
        )
    )
)

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

    observeEvent(input$complete, {
        print(input$complete)
        disabled_choices <- names==c("Alex")
        updatePickerInput(
            session = session, inputId = "myname",
            choices = names,
            choicesOpt = list(
                disabled = disabled_choices,
                style = ifelse(disabled_choices,
                               yes = "color: rgba(119, 119, 119, 0.5);",
                               no = "")
            )
        )
    }, ignoreInit = TRUE)

}

# Run the application 
shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM