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
shinydashboard包定制化一种规范的shiny排版布局,帮助快速、优雅地实现一个简单的app。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
library(shiny) # 1.7.4 version
library(shinydashboard) # 0.7.2 version
header = dashboardHeader() #标题栏
sidebar = dashboardSidebar() #侧边栏
body = dashboardBody() #主界面
ui <- dashboardPage(
header = header,
sidebar = sidebar,
body = body,
title = NULL,
skin = "blue"
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
|
1. 标题栏#
?dashboardHeader()
title
参数设置app的标题名
- 然后用
dropdownMenu()
设置3类下拉菜单,默认置于右侧,主要用于文本提醒
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
header <- dashboardHeader(
title = "Dashboard Demo",
# Dropdown menu for messages
dropdownMenu(type = "messages", badgeStatus = "success",
messageItem("Support Team",
"This is the content of a message.",
time = "5 mins"
)
),
# Dropdown menu for notifications
dropdownMenu(type = "notifications", badgeStatus = "warning",
notificationItem(icon = icon("users"), status = "info",
"5 new members joined today"
)
),
# Dropdown menu for tasks, with progress bar
dropdownMenu(type = "tasks", badgeStatus = "danger",
taskItem(value = 20, color = "aqua",
"Refactor code"
)
)
)
|
2. 侧边栏#
?dashboardSidebar()
类似于shiny包的navlistPanel()
函数
- 通过
sidebarMenu()
设置多个面板menuItem()
- 每个
menuItem()
可以进一步包括多个二级面板menuSubItem()
- 可通过server()部分的
renderMenu()
动态更新sidebarMenuOutput()
或者menuItemOutput()
1
2
3
4
5
6
7
8
9
10
11
12
13
|
sidebar = dashboardSidebar(
sidebarMenu(
id = "tabs",
menuItem(text = "Dashboard",
tabName = "dashboard",
icon = icon("dashboard"),
badgeLabel = "new",
badgeColor = "green"),
menuItem(text = "Widget", icon = icon("box"),
menuSubItem("Sub-item 1", tabName = "subitem1"),
menuSubItem("Sub-item 2", tabName = "subitem2"))
)
)
|
此外,sidebarSearchForm()可以设置搜索框,sidebarUserPanel()可以添加用户信息。甚至也可以添加Inut等UI元素。
3. 主界面#
?dashboardBody()
1
2
3
4
5
6
7
8
9
10
11
12
13
|
body <- dashboardBody(
tabItems(
tabItem("dashboard",
div(p("Dashboard tab content"))
),
tabItem("subitem1",
"Sub-item 1 tab content"
),
tabItem("subitem2",
"Sub-item 2 tab content"
)
)
)
|
- 在每个tab中可设置常规的UI控件,实现特定的分析目的。shinydashboard提供了
box()
,可以提供简洁的卡片式(card)面板,并基于shiny的fluidRow()
,column()
进行合理的布局。
?box()
1
2
3
4
5
6
7
8
9
10
|
box(
title = "", #左上角标题
status = "", #上边的颜色,不设置则为白色
solidHeader = FALSE, #上边是否加粗(标题)
collapsible = FALSE, #是否支持折叠
background = NULL, #填充颜色
width = 6, # 0~12
height = 300, # px
footer = NULL
)
|
此外还提供有其它3种特定样式类型的box
tabBox()
:类似与tabsetPanel,在1个box中嵌套多个panel
1
2
3
4
5
6
|
tabBox(
title = "tabBox",
id = "tabset1", height = "250px",
tabPanel("Tab1", "First tab content"),
tabPanel("Tab2", "Tab content 2")
)
|
1
2
3
4
5
6
7
8
9
|
infoBox(
title = ,
value = , # number/shorttext
icon = shiny::icon("bar-chart"),
color = , # validColors()
width = , # 0~12
fill = FALSE #右侧部分是否填充
)
|
可使用renderValueBox()
与valueBoxOutput()
动态更新。
valueBox()
:与上述info类似,只是主题样式不同
可使用renderInfoBox()
与infoBoxOutput()
动态更新。