stateFuncs: Enable/disable an input element

Description Usage Arguments Note See Also Examples

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 evaluates 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

1
2
3
4
5
enable(id = NULL, selector = NULL, asis = FALSE)

disable(id = NULL, selector = NULL, asis = FALSE)

toggleState(id = NULL, condition = NULL, selector = NULL, asis = FALSE)

Arguments

id

The id of the input element/Shiny tag

selector

Query selector of the elements to target. Ignored if the id argument is given. For example, to disable all text inputs, use selector = "input[type='text']"

asis

If TRUE, use the ID as-is even when inside a module (instead of adding the namespace prefix to the ID).

condition

An optional argument to toggleState. The element will be enabled when the condition is TRUE, and disabled otherwise.

Note

shinyjs must be initialized with a call to useShinyjs() in the app's ui.

See Also

useShinyjs, runExample disabled

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
if (interactive()) {
  library(shiny)

  shinyApp(
    ui = fluidPage(
      useShinyjs(),  # Set up shinyjs
      actionButton("btn", "Click me"),
      textInput("element", "Watch what happens to me")
    ),
    server = function(input, output) {
      observeEvent(input$btn, {
        # Change the following line for more examples
        toggleState("element")
      })
    }
  )
}
## Not run: 
  # 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
  actionButton("element", "I'm a button")
  fileInput("element", "Choose a file")
  selectInput("element", "I'm a select box", 1:10)

## End(Not run)

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

shinyjs documentation built on Sept. 13, 2020, 5:14 p.m.