shinydashboard包提供了一組用于創(chuàng)建生成儀表板的HTML的函數(shù)。
#結(jié)構(gòu)概要
dashboardPage()函數(shù)包含三個組件:
- a header
- sidebar
- body
dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody()
)
或者
header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody()
dashboardPage(header, sidebar, body)
##Header
標(biāo)題可以有標(biāo)題和下拉菜單。
標(biāo)題:創(chuàng)建使用參數(shù)title
下拉菜單:創(chuàng)建使用函數(shù)dropdownMenu(),下拉菜單有三種:
- messages
- notifications
- tasks
###Message menus
下拉菜單中組成使用messageItem()
messageItem(from, message, icon = shiny::icon("user"), time = NULL,
href = NULL)
dropdownMenu(type = "messages",
messageItem(
from = "Sales Dept",
message = "Sales are steady this month."
),
messageItem(
from = "New User",
message = "How do I register?",
icon = icon("question"),
time = "13:45"
),
messageItem(
from = "Support",
message = "The new server is ready.",
icon = icon("life-ring"),
time = "2014-12-01"
)
)
###動態(tài)內(nèi)容
創(chuàng)建方法:
UI中:
dashboardHeader(dropdownMenuOutput("messageMenu"))
server中:
output$messageMenu <- renderMenu({
# Code to generate each of the messageItems here, in a list. This assumes
# that messageData is a data frame with two columns, 'from' and 'message'.
msgs <- apply(messageData, 1, function(row) {
messageItem(from = row[["from"]], message = row[["message"]])
})
# This is equivalent to calling:
# dropdownMenu(type="messages", msgs[[1]], msgs[[2]], ...)
dropdownMenu(type = "messages", .list = msgs)
})
###通知菜單
dropdownMenu(type = "notifications",
notificationItem(
text = "5 new users today",
icon("users")
),
notificationItem(
text = "12 items delivered",
icon("truck"),
status = "success"
),
notificationItem(
text = "Server load at 86%",
icon = icon("exclamation-triangle"),
status = "warning"
)
)
###任務(wù)菜單
dropdownMenu(type = "tasks", badgeStatus = "success",
taskItem(value = 90, color = "green",
"Documentation"
),
taskItem(value = 17, color = "aqua",
"Project X"
),
taskItem(value = 75, color = "yellow",
"Server deployment"
),
taskItem(value = 80, color = "red",
"Overall project"
)
)
### ###禁用標(biāo)題欄
dashboardHeader(disable = TRUE)
## ##Sidebar
###Sidebar menu items and tabs
當(dāng)單擊一個鏈接時誉己,它將在儀表板的主體中顯示不同的內(nèi)容。
與Shiny中的tabPanel類似缓熟。
注:必須保證dashboardSidebar中menuItem的tabName 與dashboardBody中tabItem的tabName 是一樣的犀勒,這樣才能對應(yīng)。
## ui.R ##
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", icon = icon("th"), tabName = "widgets",
badgeLabel = "new", badgeColor = "green")
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
h2("Dashboard tab content")
),
tabItem(tabName = "widgets",
h2("Widgets tab content")
)
)
)
# Put them together into a dashboardPage
dashboardPage(
dashboardHeader(title = "Simple tabs"),
sidebar,
body
)
menuItems的圖標(biāo)可以使用Shiny中函數(shù)icon()改變会油。
各種各樣的圖標(biāo)參考:
默認(rèn)情況下个粱,icon()函數(shù)使用來自Font-Awesome的圖標(biāo)。使用glyphicon翻翩,請使用lib="glyphicon")
"Calendar from Font-Awesome:", icon("calendar"),
"Cog from Glyphicons:", icon("cog", lib = "glyphicon")
menuItem中也可以提供鏈接都许,從而訪問外部網(wǎng)站:
menuItem("Source code", icon = icon("file-code-o"),
)
###Bookmarking and restoring selected tabs
Shiny可以添加書簽并恢復(fù)應(yīng)用程序的狀態(tài)。在使用shinydashboard構(gòu)建的應(yīng)用程序中嫂冻,要想添加書簽并恢復(fù)選中的選項(xiàng)胶征,你必須使用id調(diào)用sidebarMenu()。
sidebarMenu(id = "sidebar",
....
)
詳細(xì)操作查看: bookmark and restore
###動態(tài)內(nèi)容
可以使用renderMenu和sidebarMenuOutput動態(tài)生成側(cè)邊欄菜單桨仿。
ui <- dashboardPage(
dashboardHeader(title = "Dynamic sidebar"),
dashboardSidebar(
sidebarMenuOutput("menu")
),
dashboardBody()
)
server <- function(input, output) {
output$menu <- renderMenu({
sidebarMenu(
menuItem("Menu item", icon = icon("calendar"))
)
})
}
shinyApp(ui, server)
或
ui <- dashboardPage(
dashboardHeader(title = "Dynamic sidebar"),
dashboardSidebar(
sidebarMenu(
menuItemOutput("menuitem")
)
),
dashboardBody()
)
server <- function(input, output) {
output$menuitem <- renderMenu({
menuItem("Menu item", icon = icon("calendar"))
})
}
shinyApp(ui, server)
###側(cè)邊欄中的輸入
sidebarSearchForm(textId = "searchText", buttonId = "searchButton",
label = "Search...")
對于這個搜索睛低,server代碼中的對應(yīng)值將是inputsearchButton。
###禁用側(cè)邊欄
dashboardSidebar(disable = TRUE)
##Body
儀表板頁面的主體可以包含任何規(guī)則的shiny內(nèi)容服傍。然而钱雷,如果正在創(chuàng)建一個指示板,可能想要創(chuàng)建一些更結(jié)構(gòu)化的東西伴嗡。儀表板的基本構(gòu)造是box()急波。box()中可以包含任何內(nèi)容。
###Boxes
box是儀表板頁面的主要構(gòu)建塊瘪校。可以使用box()函數(shù)創(chuàng)建一個基本框名段,box的內(nèi)容可以是任何shiny UI內(nèi)容阱扬。
在典型的儀表板中,box將被放置在一個fluidRow()中.
# This is just the body component of a dashboard
dashboardBody(
fluidRow(
box(plotOutput("plot1")),
box(
"Box content here", br(), "More box content",
sliderInput("slider", "Slider input:", 1, 100, 50),
textInput("text", "Text input:")
)
)
)
####Basic boxes
box可以有標(biāo)題伸辟,并且標(biāo)題和標(biāo)題欄顏色可以通過title
和status
設(shè)置麻惶。
Statuses
status="primary"`, `status="success"
colors
color="red"`, `color="black"
注:狀態(tài)和顏色可以通過?validStatuses和?validColors查看。
box(title = "Histogram", status = "primary", plotOutput("plot2", height = 250)),
box(
title = "Inputs", status = "warning",
"Box content here", br(), "More box content",
sliderInput("slider", "Slider input:", 1, 100, 50),
textInput("text", "Text input:")
)
####Box header color and title
使用solidHeader=TRUE設(shè)置實(shí)標(biāo)頭信夫,并在右上角顯示一個按鈕窃蹋,該按鈕將以collapsible=TRUE折疊該框:
box(
title = "Histogram", status = "primary", solidHeader = TRUE,
collapsible = TRUE,
plotOutput("plot3", height = 250)
),
box(
title = "Inputs", status = "warning", solidHeader = TRUE,
"Box content here", br(), "More box content",
sliderInput("slider", "Slider input:", 1, 100, 50),
textInput("text", "Text input:")
)
####Solid header and collapse
如果希望方框頂部沒有灰色或有色條,請使用solidHeader=TRUE静稻,并且不要為狀態(tài)提供值:
box(
title = "Histogram", solidHeader = TRUE,
collapsible = TRUE,
plotOutput("plot3", height = 250)
),
box(
title = "Inputs", solidHeader = TRUE,
"Box content here", br(), "More box content",
sliderInput("slider", "Slider input:", 1, 100, 50),
textInput("text", "Text input:")
)
####No colored bar
還可以使用background選項(xiàng)創(chuàng)建一個背景警没。
box(
title = "Histogram", background = "maroon", solidHeader = TRUE,
plotOutput("plot4", height = 250)
),
box(
title = "Inputs", background = "black",
"Box content here", br(), "More box content",
sliderInput("slider", "Slider input:", 1, 100, 50),
textInput("text", "Text input:")
)
####tabBox
- tabBox
tabBox與Shiny 中tabsetPanel類似,tabPanels作為輸入振湾,允許您選擇選擇哪個tab杀迹,并可以分配一個id。
如果id是存在的押搪,可以訪問哪個選項(xiàng)卡被選擇從服務(wù)器;在下面的示例中树酪,使用輸入$tabset1訪問它浅碾。
tabBox與shinydashboard中box類似,可以修改height
, width
, 和title
.
還可以使用side參數(shù)選擇選項(xiàng)卡出現(xiàn)在哪一邊续语。注意垂谢,如果side="right",選項(xiàng)卡將以相反的順序顯示疮茄。
body <- dashboardBody(
fluidRow(
tabBox(
title = "First tabBox",
# The id lets us use input$tabset1 on the server to find the current tab
id = "tabset1", height = "250px",
tabPanel("Tab1", "First tab content"),
tabPanel("Tab2", "Tab content 2")
),
tabBox(
side = "right", height = "250px",
selected = "Tab3",
tabPanel("Tab1", "Tab content 1"),
tabPanel("Tab2", "Tab content 2"),
tabPanel("Tab3", "Note that when side=right, the tab order is reversed.")
)
),
fluidRow(
tabBox(
# Title can include an icon
title = tagList(shiny::icon("gear"), "tabBox status"),
tabPanel("Tab1",
"Currently selected tab from first box:",
verbatimTextOutput("tabset1Selected")
),
tabPanel("Tab2", "Tab content 2")
)
)
)
shinyApp(
ui = dashboardPage(
dashboardHeader(title = "tabBoxes"),
dashboardSidebar(),
body
),
server = function(input, output) {
# The currently selected tab from the first box
output$tabset1Selected <- renderText({
input$tabset1
})
}
)
####infoBox
這是一種特殊的框滥朱,用于顯示簡單的數(shù)字或文本值,并帶有圖標(biāo)娃豹。
第一行使用默認(rèn)設(shè)置fill=FALSE焚虱,而第二行使用fill=TRUE。
由于infoBox的內(nèi)容通常是動態(tài)的懂版,因此shinydashboard包含了用于動態(tài)內(nèi)容的幫助函數(shù)infoBoxOutput renderInfoBox鹃栽。
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Info boxes"),
dashboardSidebar(),
dashboardBody(
# infoBoxes with fill=FALSE
fluidRow(
# A static infoBox
infoBox("New Orders", 10 * 2, icon = icon("credit-card")),
# Dynamic infoBoxes
infoBoxOutput("progressBox"),
infoBoxOutput("approvalBox")
),
# infoBoxes with fill=TRUE
fluidRow(
infoBox("New Orders", 10 * 2, icon = icon("credit-card"), fill = TRUE),
infoBoxOutput("progressBox2"),
infoBoxOutput("approvalBox2")
),
fluidRow(
# Clicking this will increment the progress amount
box(width = 4, actionButton("count", "Increment progress"))
)
)
)
server <- function(input, output) {
output$progressBox <- renderInfoBox({
infoBox(
"Progress", paste0(25 + input$count, "%"), icon = icon("list"),
color = "purple"
)
})
output$approvalBox <- renderInfoBox({
infoBox(
"Approval", "80%", icon = icon("thumbs-up", lib = "glyphicon"),
color = "yellow"
)
})
# Same as above, but with fill=TRUE
output$progressBox2 <- renderInfoBox({
infoBox(
"Progress", paste0(25 + input$count, "%"), icon = icon("list"),
color = "purple", fill = TRUE
)
})
output$approvalBox2 <- renderInfoBox({
infoBox(
"Approval", "80%", icon = icon("thumbs-up", lib = "glyphicon"),
color = "yellow", fill = TRUE
)
})
}
shinyApp(ui, server)
###布局
布局方框需要了解一些引導(dǎo)網(wǎng)格布局系統(tǒng)的知識。主體可以視為被劃分為12個等寬列和任意數(shù)量的行躯畴,可變高度的區(qū)域民鼓。當(dāng)在網(wǎng)格中放置一個框(或其他項(xiàng))時,可以指定希望它占據(jù)12列中的多少列蓬抄。在這個屏幕截圖中丰嘉,第一行框的寬度各為4列,第二列框的寬度各為6列嚷缭。
一般來說饮亏,有兩種方式來布置方框:基于行的布局,或者基于列的布局阅爽。
####Row-based layout
在基于行的布局中路幸,框必須放在由fluidRow()創(chuàng)建的行中。行的網(wǎng)格寬度為12付翁,因此寬度=4的框占寬度的三分之一简肴,寬度=6(默認(rèn))的框占寬度的一半。
在基于行的布局中百侧,每行框的頂部將對齊砰识,但底部可能不對齊——這取決于每個框的內(nèi)容。
body <- dashboardBody(
fluidRow(
box(title = "Box title", "Box content"),
box(status = "warning", "Box content")
),
fluidRow(
box(
title = "Title 1", width = 4, solidHeader = TRUE, status = "primary",
"Box content"
),
box(
title = "Title 2", width = 4, solidHeader = TRUE,
"Box content"
),
box(
title = "Title 1", width = 4, solidHeader = TRUE, status = "warning",
"Box content"
)
),
fluidRow(
box(
width = 4, background = "black",
"A box with a solid black background"
),
box(
title = "Title 5", width = 4, background = "light-blue",
"A box with a solid light-blue background"
),
box(
title = "Title 6",width = 4, background = "maroon",
"A box with a solid maroon background"
)
)
)
# We'll save it in a variable `ui` so that we can preview it in the console
ui <- dashboardPage(
dashboardHeader(title = "Row layout"),
dashboardSidebar(),
body
)
# Preview the UI in the console
shinyApp(ui = ui, server = function(input, output) { })
通過設(shè)置height佣渴,可以強(qiáng)制所有框具有相同的高度辫狼。與使用12寬引導(dǎo)網(wǎng)格設(shè)置的寬度相反,高度是用像素指定的观话。
box(title = "Box title", height = 300, "Box content")
如果設(shè)置所有框的高度予借,可以得到這樣的儀表盤:
####Column-based layout
下面的代碼是這個基于列的布局的基本框架。注意,在fluidRow中灵迫,有指定寬度的列秦叛,列中的每個框的width=NULL。
body <- dashboardBody(
fluidRow(
column(width = 4,
box(
title = "Box title", width = NULL, status = "primary",
"Box content"
),
box(
title = "Title 1", width = NULL, solidHeader = TRUE, status = "primary",
"Box content"
),
box(
width = NULL, background = "black",
"A box with a solid black background"
)
),
column(width = 4,
box(
status = "warning", width = NULL,
"Box content"
),
box(
title = "Title 3", width = NULL, solidHeader = TRUE, status = "warning",
"Box content"
),
box(
title = "Title 5", width = NULL, background = "light-blue",
"A box with a solid light-blue background"
)
),
column(width = 4,
box(
title = "Title 2", width = NULL, solidHeader = TRUE,
"Box content"
),
box(
title = "Title 6", width = NULL, background = "maroon",
"A box with a solid maroon background"
)
)
)
)
# We'll save it in a variable `ui` so that we can preview it in the console
ui <- dashboardPage(
dashboardHeader(title = "Column layout"),
dashboardSidebar(),
body
)
# Preview the UI in the console
shinyApp(ui = ui, server = function(input, output) { })
####Mixed row and column layout
也可以混合使用行和列瀑粥。
body <- dashboardBody(
fluidRow(
box(
title = "Box title", width = 6, status = "primary",
"Box content"
),
box(
status = "warning", width = 6,
"Box content"
)
),
fluidRow(
column(width = 4,
box(
title = "Title 1", width = NULL, solidHeader = TRUE, status = "primary",
"Box content"
),
box(
width = NULL, background = "black",
"A box with a solid black background"
)
),
column(width = 4,
box(
title = "Title 3", width = NULL, solidHeader = TRUE, status = "warning",
"Box content"
),
box(
title = "Title 5", width = NULL, background = "light-blue",
"A box with a solid light-blue background"
)
),
column(width = 4,
box(
title = "Title 2", width = NULL, solidHeader = TRUE,
"Box content"
),
box(
title = "Title 6", width = NULL, background = "maroon",
"A box with a solid maroon background"
)
)
)
)
# We'll save it in a variable `ui` so that we can preview it in the console
ui <- dashboardPage(
dashboardHeader(title = "Mixed layout"),
dashboardSidebar(),
body
)
# Preview the UI in the console
shinyApp(ui = ui, server = function(input, output) { })
#原文:
Background: Shiny and HTML
shinydashboard
R shiny教程-1:一個 Shiny app的基本組成部分
R shiny教程-2:布局用戶界面
R shiny教程-3:添加小部件到Shiny App
R shiny教程-4:Shiny app響應(yīng)式結(jié)果展示
R shiny教程-5:調(diào)用R程序和導(dǎo)入數(shù)據(jù)
R shiny教程-6:使用響應(yīng)表達(dá)式reactive()
R shiny教程-7:共享Shiny app
Shiny Server安裝
shinydashboard安裝挣跋、使用指南