daattali / shinyjs

💡 Easily improve the user experience of your Shiny apps in seconds
https://deanattali.com/shinyjs
Other
728 stars 119 forks source link

Disable radio button in a radio button group with shinyjs selector #231

Closed svasa closed 3 years ago

svasa commented 3 years ago

I was able to disable a radio button in a group of radio buttons before successfully. But with 2.0 version of shinyjs I m not able to disable irrespective of whether the code is in a module or not in a module. Am I missing something here? I posted the question on StackOverflow but couldn't get a response, so I m posting it here. Sorry if it had been addressed before I cannot find it.

With out module:

ui <- fluidPage(uiOutput("radiobox"))

server <- function(input, output, session) {
  output$radiobox <- renderUI({
    choices <- c("Choice1", "Choice2", "Choice3")
    radioButtons("radiobox", "Select Choice:", choices = choices)
  })
  observeEvent(input$radiobox, {
    shinyjs::disable(selector = "#radiobox > div > div > div > label > input[value='Choice1']")
  })

}

shinyApp(ui = ui, server = server)

With module:

modUI <- function(id) {
  ns <- NS(id)
  tagList(
    fluidPage(
      shinyjs::useShinyjs(),
      uiOutput(ns("radiobox"))
    ))
}

mod <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      ns <- session$ns
      output$radiobox <- renderUI({
        choices <- c("Choice1", "Choice2", "Choice3")
        radioButtons(ns("radiobox"), "Select Choice:", choices = choices)
      })
      observeEvent(input$radiobox, {
        browser()
        radiobox_id <- ns("radiobox")
        radiobox_selector_1 <- paste(paste0("#", radiobox_id), ">", "div", ">")
        radiobox_selector_2 <- paste("label", ">", "input[type=radio]")
        shinyjs::disable(selector = paste(radiobox_selector_1, "div:nth-child(2) >", radiobox_selector_2), asis = TRUE)
      })
    }
  )
} 

ui <- fluidPage(modUI('testModule'))

server <- function(input, output) { mod("testModule") }

shinyApp(ui = ui, server = server)
daattali commented 3 years ago

In your first example, you didn't include useShinyjs().

The second example works for me, the middle radio buton gets disabled.

svasa commented 3 years ago

Its working for me now too. I swear I saw the issue yesterday, and somehow its working now. I m not yet at all sure what was happening then? Closing this issue. Please delete this issue if it is OK. Thanks.