0
votes

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)
1

1 Answers

3
votes

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).