Shiny Basic系列:

参考教程:https://mastering-shiny.org/

(1)IO控件

(2)Layout布局

(3)Reactive用法

(4)Feedback提醒

(5)Module模块

Shiny Package系列:

(1)shinyWidgets

(2)shinyJS

(3)shinydashboard

(4)shinydashboardPlus

(5)bslib

(6)Other pkgs


shinyJS包主要提供了些个性化控制控件的函数

Tips: 使用该包提供的功能时,需要在ui部分声明useShinyjs()语句,对于Shiny Dashboards或者navbarPage layout有特殊的放置要求,详见教程文档的Advanced部分。

1. 控件失效/激活

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
disable()  #控件(包括输入控件/动作按钮等)失效
enable()   #控件恢复

## 示例:当文本输入为空时,submit按钮为失效状态
ui = fluidPage(
  useShinyjs(),  # Set up shinyjs
  textInput("name","Give your name"),
  actionButton("submit", "Click me")
)

server = function(input, output, session) {
  observe({
    if (is.null(input$name) || input$name == "") {
      shinyjs::disable("submit")
    } else {
      shinyjs::enable("submit")
    }
  })
}

shinyApp(ui, server)
 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)

2. UI的显示/隐藏

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
hide()
show()

# 示例
ui = fluidPage(
  useShinyjs(),  # Set up shinyjs
  actionButton("btn", "Click me"),
  textInput("text", "Text")
)
server = function(input, output, session) {
  observe({
    if(input$btn %% 2 ==0){ #点击1次消失,再点击一次出现
      show("text")
    } else {
      hide("text")
    }
  })
}

shinyApp(ui, server)
 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
toggle() #可以进行更简单的控件显示与隐藏

## 示例1
ui = fluidPage(
  useShinyjs(),  # Set up shinyjs
  actionButton("btn", "Click me"),
  textInput("text", "Text")
)
server = function(input, output, session) {
  observeEvent(input$btn, {
    toggle("text")
  })
}

shinyApp(ui, server)

## 示例2:hidden() 设置UI初始的隐藏状态
ui = fluidPage(
  useShinyjs(),  # Set up shinyjs
  actionButton("btn", "Click me"),
  hidden(
    div(  #包裹在div或其它等
      id = "this_div",
      textInput("text", "Text")
    )
  )
)
server = function(input, output, session) {
  observeEvent(input$btn, {
    toggle("this_div")
  })
}

shinyApp(ui, server)