Learn R Programming

shinyjs (version 0.1.0)

stateFuncs: Enable/disable an input element

Description

Enable or disable an input element. A disabled element is not usable and not clickable, while an enabled element (default) can receive user input. Any shiny input tag can be used with these functions. enable enables an input, disable disabled an input,toggleState enables an input if it is disabled and disables an input if it is already enabled. If condition is given to toggleState, that condition will be used to determine if to enable or disable the input. The element will be enabled if the condition evalutes to TRUE and disabled otherwise. If you find yourself writing code such as if (test()) enable(id) else disable(id) then you can use toggleState instead: toggleState(id, test()).

Usage

enable(...)

disable(...)

toggleState(...)

Arguments

...
The following parameters are available: ll{ id The id of the input element/Shiny tag condition An optional argument to toggleState,

See Also

useShinyjs, runExample disabled

Examples

Run this code
if (interactive()) {
  shiny::shinyApp(
    ui = shiny::fluidPage(
      useShinyjs(),  # Set up shinyjs
      shiny::actionButton("btn", "Click me"),
      shiny::textInput("element", "Watch what happens to me")
    ),
    server = function(input, output) {
      shiny::observeEvent(input$btn, {
        # Change the following line for more examples
        toggleState("element")
      })
    }
  )
}
# The shinyjs function call in the above app can be replaced by
  # any of the following examples to produce similar Shiny apps
  toggleState(id = "element")
  enable("element")
  disable("element")

  # Similarly, the "element" text input can be changed to many other
  # input tags, such as the following examples
  shiny::actionButton("element", "I'm a button")
  shiny::fileInput("element", "Choose a file")
  shiny::selectInput("element", "I'm a select box", 1:10)

## toggleState can be given an optional `condition` argument, which
## determines if to enable or disable the input
if (interactive()) {
  shiny::shinyApp(
    ui = shiny::fluidPage(
      useShinyjs(),
      shiny::textInput("text", "Please type at least 3 characters"),
      shiny::actionButton("element", "Submit")
    ),
    server = function(input, output) {
      shiny::observe({
        toggleState(id = "element", condition = nchar(input$text) >= 3)
      })
    }
  )
}

Run the code above in your browser using DataLab