0

I need to restrict the user's ability to select radio buttons, yet show them at all times. I use shinyjs::disable to disable the button and updateRadioButtons to update the selection , but it doesn't seem to work correctly. Only the update seems to work, not the disabling My code:

library(shiny)
library(shinyjs)

server = shinyServer(function(input, output, session) {

observeEvent(input$toggle, {

if (input$toggle == "enable") {

  updateRadioButtons(session, "button1", "",
                     c("one" = "one", "two" = "two"),
                     inline = T, selected = "one")

  shinyjs::enable("button1")

}

if (input$toggle == "disable") {

  updateRadioButtons(session, "button1", "",
                     c("one" = "one", "two" = "two"),
                     inline = T, selected = "two")

  shinyjs::disable("button1")

}

 })

})

ui = shinyUI(

fluidPage(

shinyjs::useShinyjs(),

radioButtons("toggle", "",
           c("enable" = "enable", "disable" = "disable"), inline = T),

radioButtons("button1", "",
           c("one" = "one", "two" = "two"), inline = T)

))

shinyApp(ui=ui,server=server)
Xlrv
  • 469
  • 1
  • 6
  • 16

1 Answers1

3

This is happening because the update function is actually doing the update a bit later. So what's happening is that the update function is called, then disable is called , the button gets disabled, and a bit later the button is re-generated from the update. It's because of how the internals of shiny work. If you comment out the update function you'll see that the button does get disabled. It's kind of annoying, but it's just a side effect of how shiny works. One hacky solution you can try is instead of disable(id), use shinyjs::delay(100, disable(id)) (which means that you'll add a short delay of 100ms and only then disable).

DeanAttali
  • 25,268
  • 10
  • 92
  • 118
  • This is a great little work around that helped me with my UI as well - thanks! A note to others, you can of course change the delay to be much shorter (like 5ms), and then the delay is not noticeable to the user but still happens after the other things have occurred. – Jamie Jul 12 '21 at 23:06