spies006
spies006

Reputation: 2927

shinyjs::enable UI widget on shiny app launch

I'm using shinyjs to enable and disable a UI widget (inputId="check_buttons"). Currently the widget is only enabled after the user clicks to go button (input$go). Is it possible to have the UI widget check_buttons enabled be default when the app is launched?

observeEvent(input$go, 
  {
  shinyjs::enable("check_buttons")
})

observeEvent({input$select_box_input
              input$radio_button_input
              },
  {
  shinyjs::disable("check_buttons")
})

Upvotes: 1

Views: 241

Answers (1)

Adam Spannbauer
Adam Spannbauer

Reputation: 2757

The issue is that the observeEvent to disable the check_buttons widget is being triggered when the input$select_box_input/input$radio_button_input widgets are initialized. You can use the ignoreInit argument of observeEvent to bypass this behavior:

ignoreInit

If TRUE, then, when this observeEvent is first created/initialized, ignore the handlerExpr (the second argument), whether it is otherwise supposed to run or not. The default is FALSE. See Details.

Minimal example with the behavior implemented:

library(shiny)
library(shinyjs)

shinyApp(
  shinyUI(
    fluidPage(
      shinyjs::useShinyjs(),
      actionButton('go', 'go'),
      radioButtons('radio_button_input', 'radio', choices = c('a','b')),
      checkboxInput('select_box_input', 'check', value=TRUE),
      checkboxGroupInput('check_buttons', 'boxes', choices = c('1','2'))
    )
  ),
  shinyServer(function(input, output, session) {
    observeEvent(input$go, {
                 shinyjs::enable("check_buttons")
    })
    
    observeEvent({
      input$radio_button_input
      input$select_box_input
      }, {
        shinyjs::disable("check_buttons")
      }, ignoreInit = TRUE)
  })
)

Upvotes: 1

Related Questions