shinydashboard 的主要目的是幫助用戶更方便構(gòu)建Shiny app的布局筹陵。
#shinydashboard安裝
install.packages("shinydashboard")
#基本情況
shinydashboard最基本的組成有3個(gè)部分刽锤,
- a header
- a sidebar
- a body
##基本面板
## ui.R ##
library(shinydashboard)
dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody()
)
## app.R ##
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody()
)
server <- function(input, output) { }
shinyApp(ui, server)
##添加內(nèi)容
###添加內(nèi)容到主頁(yè)面
## app.R ##
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
# Boxes need to be put in a row (or column)
fluidRow(
box(plotOutput("plot1", height = 250)),
box(
title = "Controls",
sliderInput("slider", "Number of observations:", 1, 100, 50)
)
)
)
)
server <- function(input, output) {
set.seed(122)
histdata <- rnorm(500)
output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
}
shinyApp(ui, server)
[站外圖片上傳中...(image-2c8c0-1602061295182)]
###添加內(nèi)容到側(cè)邊欄
shinydashboard 中添加menu items與shiny中tabPanel
作用類似:當(dāng)單擊一個(gè)側(cè)邊欄的一個(gè)對(duì)象時(shí),它會(huì)在主頁(yè)面顯示一組不同的內(nèi)容朦佩。
## Sidebar content
dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", tabName = "widgets", icon = icon("th"))
)
)
在主頁(yè)面需要頁(yè)面與menuItem一一進(jìn)行對(duì)應(yīng)并思,通過(guò)設(shè)置tabItems中的tabItem
對(duì)應(yīng)各個(gè)menuItem的tabName。
## Body content
dashboardBody(
tabItems(
# First tab content
tabItem(tabName = "dashboard",
fluidRow(
box(plotOutput("plot1", height = 250)),
box(
title = "Controls",
sliderInput("slider", "Number of observations:", 1, 100, 50)
)
)
),
# Second tab content
tabItem(tabName = "widgets",
h2("Widgets tab content")
)
)
)
#原文
系列文章:
R shiny教程-1:一個(gè) 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安裝