4

I have RShiny code, with which i want to disable/enable number input with checkbox. However, it works only for disable.

library(shiny)
    runApp(shinyApp(
      ui = fluidPage(
        shinyjs::useShinyjs(),
        numericInput("test", "Test", 5),
        checkboxInput("submit", label="Choose")
      ),
      server = function(input, output, session) {
        observeEvent(input$submit, {
          shinyjs::disable("test")
        })
      }
    ))

How could I fix that?

Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
neringab
  • 613
  • 1
  • 7
  • 16

1 Answers1

9

Your code is mostly correct. The bug is in what you are observing. Your code would work fine if you are using an action button. But for the checkbox, you need to disable the input when the checkbox is unchecked, and enable when checked, and not just observe the event.

library(shiny)
runApp(shinyApp(
  ui = fluidPage(
    shinyjs::useShinyjs(),
    numericInput("test", "Test", 5),
    checkboxInput("submit", label="Choose")
  ),
  server = function(input, output, session) {
    observeEvent(input$submit, {
      if(input$submit == F){
        shinyjs::disable("test")
      } else {
        shinyjs::enable("test")
      }
    })
  }
))
Deena
  • 5,925
  • 6
  • 34
  • 40