设置颜色
除了用颜色编码的 status 设置外,您还可以为 valueBox() 和 infoBox() 等对象设置更多颜色。

接下来,您将为一个 shinydashboard 的主体部分设置不同的颜色。尽管这些对象也可以像之前的示例那样在 server() 中定义。
在本练习中,shiny 和 shinydashboard 库已为您加载完成。另有 header 和 sidebar 分别存为 header 和 sidebar,且 server() 是一个空函数。
本练习是课程的一部分
使用 shinydashboard 构建仪表板
交互式实操练习
通过完成这段示例代码来试试这个练习。
library(shiny)
library(shinydashboard)
header <- dashboardHeader(
title = "Portfolio dashboard for Sally",
titleWidth = 300,
dropdownMenu(type = "notifications",
notificationItem("Sell alert", status = "danger"),
notificationItem("Buy alert", status = "success"))
)
sidebar <- dashboardSidebar(
width = 300,
sidebarMenu(
id = "pages",
menuItem("Historical trends",
tabName = "historical"),
menuItem("Profits and Losses (PnLs)",
tabName = "profit",
icon = icon("money-bill-alt"),
badgeLabel = "+2.3%", badgeColor = "green")
)
)
body <- dashboardBody(tabItems(
tabItem(tabName = "historical",
fluidRow(box(selectInput("stock", "Select stock symbol",
choices=c("AAPL", "DIS"))),
box("Stock name",
title="Stock name title",
width = 6,
status = "info", solidHeader = TRUE)),
# Set the color of each infoBox to black
fluidRow(infoBox("Open", "value", width = 2, color = ___),
infoBox("High", "value", width = 2,___),
infoBox("Low", "value", width = 2, ___),
infoBox("Close", "value", width = 2, ___),
# Set the color of the last infoBox to navy and set its width to 4
infoBox("Volume", "value", ___)),
fluidRow(valueBox("Open (%)", "0%", width = 2),
valueBox("High (%)", "+1%", width = 2),
valueBox("Low (%)", "-4%", width = 2),
valueBox("Close (%)", "-2%", width = 2),
valueBox("Volume (%)", "+30%", width = 2)),
fluidRow(box("Candlestick chart", width = 12, height = 350)),
fluidRow(box("Volume chart", width = 12, height = 200))),
tabItem(tabName = "profit",
fluidRow(box("valueBox", "Account balance", width = 3),
box("valueBox", "Value at Risk (Var)", width = 3),
box("valueBox", "Returns", width = 3),
box("valueBox", "Profit-to-loss ratio", width = 3)),
fluidRow(box("PnL chart", width = 12, height = 400)))
)
)
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output){}
shinyApp(ui, server)