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
|
toggleState() #可以进行更简单的控件状态转换
## 示例1:当文本输入为空时,submit按钮为失效状态
ui = fluidPage(
useShinyjs(), # Set up shinyjs
textInput("name","Give your name"),
actionButton("submit", "Click me")
)
server = function(input, output, session) {
observe({
shinyjs::toggleState("submit", !is.null(input$name) && input$name != "")
})
}
shinyApp(ui, server)
## 示例2:控制输入栏的失效与恢复
ui = fluidPage(
useShinyjs(),
actionButton("btn", "Click me"),
textInput("element", "Watch what happens to me")
)
server = function(input, output, session) {
observeEvent(input$btn, {
toggleState("element")
})
}
shinyApp(ui, server)
## 示例3:disabled() 初始状态设置为失效状态
ui = fluidPage(
useShinyjs(),
actionButton("btn", "Click me"),
disabled(
textInput("element", "Watch what happens to me")
)
)
server = function(input, output, session) {
observeEvent(input$btn, {
toggleState("element")
})
}
shinyApp(ui, server)
|