Enable/Disable Radio Buttons

1,617 views
Skip to first unread message

Alex P

unread,
Jun 1, 2015, 5:44:30 AM6/1/15
to shiny-...@googlegroups.com
Dear all,
I am building an app that depends a lot on radion buttons and sliders. I would like to be able to enable disable (grey out?) those based on the current step of the user.

Do you have any idea how I can do that in Shiny?

An example of how my radion buttons and sliders look like below

I would like to thank you for your reply
Regards
Alex
 sidebarLayout(
    sidebarPanel(
   
      radioButtons("FreqRadio", label = h3("Frequency"),
        choices = list("485" = 485, "2600" = 2600,
                       "60000" = 3),selected = 2600),
    
      radioButtons("EnvRadio", label = h3("Environment"),
        choices = list("Urban" = "urban", "Suburban" = "suburban",
                       "Space" = 3),selected = "urban"),
   
    selectInput("PropModel",
          label = "Choose a Propagation Model",
          choices = c("Log Distance", "Erceg HL/FM","Walfish-Ikegami", "Okumura-Hata", "COST 231", "Load"),
          selected = "Percent White"),
    sliderInput("maxDist",
                  "Maximum Distance [m]",
                  min = 10,
                  max = 400,
                  value = 300),   
   

   
conditionalPanel(
      condition = "input.EnvRadio == 'suburban' && input.FreqRadio == '485'",
      sliderInput(
         "TrainingPoints", "Number of Training Points",min=2,max=474,value=100,step=20
        )),

      # Only show this panel if Custom is selected
      conditionalPanel(
         condition = "input.EnvRadio == 'urban'|| input.FreqRadio == '2600'",
         sliderInput("TrainingPoints", "Number of Training Points", min=2, max=924, value=200,step=20)
      )

    ),

Dean Attali

unread,
Jun 1, 2015, 1:27:01 PM6/1/15
to shiny-...@googlegroups.com
Hi Alex, since this is a very common question, I built a package called shinyjs that can disable/enable buttons.

Here's how to use it to disable/enable a radio 

library(shiny)
library(shinyjs)

runApp(shinyApp(
  ui = fluidPage(
    useShinyjs(),
    radioButtons("radios", "Options", 1:5),
    actionButton("off", "Disable"),
    actionButton("on", "Enable")
  ),
  server = function(input, output, session) {
    observeEvent(input$off, {
      disable("radios")
    })
    observeEvent(input$on, {
      enable("radios")
    })
  }
))


Or if you want to enable/disable based on a condition, you can use something like this

library(shiny)
library(shinyjs)

runApp(shinyApp(
  ui = fluidPage(
    useShinyjs(),
    radioButtons("radios", "Options", 1:5),
    textInput("text", "Buttons will be enabled when more than 3 characters are typed")
  ),
  server = function(input, output, session) {
    observeEvent(input$text, {
      toggleState("radios", nchar(input$text) > 3)
    })
  }
))

I apologize I didn't read your specific code but I hope you can use these simple examples to make it work on your code

Alex P

unread,
Jun 2, 2015, 1:34:55 AM6/2/15
to shiny-...@googlegroups.com
Thanks.
This has helped considerably.
The problem is how to add the events that enable disable components.
For example the following gives me a syntax error

 tabPanel("All_Data", fluidRow(
                     column(12,
               observeEvent(input$off, {
                disable("radios")
                }),
                           plotOutput("ShadowDistPlot",height=900,width=900), tableOutput('table'),
                           plotOutput("Variog",height=900), actionButton('goTable', 'Go table')))),


I wanted when the user clicks on the specific tabPanel that action to disable me specific options from my left panel.
Is that possible?
Regards
Alex

Dean Attali

unread,
Jun 2, 2015, 1:41:47 AM6/2/15
to shiny-...@googlegroups.com
The code to enable/disable the buttons is server-side logic, it should be in the server function, not in the UI.
I don't remember the code off the top of my head, but for you case you just need to write a reactive that gets fired when the tab changes. I'm sure there are many examples of people doing that.  Something like "observe({ if(input$all_tabs == "All_Data") disable("radios") }). That code won't work, you have to find out how to perform an action when a tab changes, there should be plenty online
Reply all
Reply to author
Forward
0 new messages