页眉和侧边栏

使用 shinydashboard 构建仪表板

Png Kee Seng

Researcher

页眉

  • 页眉就像餐馆的招牌
  • 可包含仪表板标题
  • 可包含下拉菜单
    • 如消息、通知、任务

这是一个简单店面的示例。

页眉类似餐馆的招牌。它可包含实用或美观元素。下图展示了更复杂的门面。

1 Image by upklyak on Freepik
使用 shinydashboard 构建仪表板

shinydashboard 标题

  • dashboardHeader() 定义页眉
  • 标题过长怎么办?
    • 设置 titleWidth 参数
    • 默认 230 像素
header <- dashboardHeader(title = "Soccer tournament")
header <- dashboardHeader(title = "Analysis for global soccer tournament")
header <- dashboardHeader(title = "Analysis for global soccer tournament")
                            titleWidth = 400)

可调整页眉宽度以容纳较长标题。

使用 shinydashboard 构建仪表板

添加含消息的下拉菜单

  • 可在页眉添加下拉菜单
  • 三种下拉菜单
    • 消息
    • 通知
    • 任务
  • 添加 type 为 "messages" 的 dropdownMenu()
    • 添加一个 messageItem()
    • 默认图标为 icon("user")
    • 也可设置 time
header <- dashboardHeader(
  title = "Analysis for FIFA world cup",
  titleWidth = 300,
  dropdownMenu(type = "messages",

messageItem("Data division", "Keep up the good work!",
time = "5 mins"
)))

包含一个消息项的下拉菜单。

使用 shinydashboard 构建仪表板

添加含多条消息的下拉菜单

  • dropdownMenu() 中再添加一个名为 "Twitter" 的 messageItem()

包含两个消息项的下拉菜单。

header <- dashboardHeader(
  title = "Analysis for global soccer tournament",
  titleWidth = 400,
  dropdownMenu(type = "messages",
    messageItem("Data division",
      "Keep up the good work!",
      time = "5 mins"
    ),

messageItem("Twitter", "You have a Tweet!", time = "1 hour", icon=icon("twitter") ) ) )
使用 shinydashboard 构建仪表板

添加含多种项目的下拉菜单

  • 还可添加其他类型的项目
    • messageItem()
    • notificationItem()
    • taskItem()
  • 外观有所不同
    • messageItem():默认图标为 icon("user")
    • notificationItem():默认图标为 icon("exclamation-triangle")
    • taskItem():包含进度条
header <- dashboardHeader(
  title = "Analysis for global soccer tournament",
  titleWidth = 400,
  dropdownMenu(type = "messages",

messageItem("Data division", "Keep up the good work!", time = "5 mins"), messageItem("Twitter", "You have a Tweet!", time = "1 hour", icon=icon("twitter")),
notificationItem("This is a notification." ),
taskItem(value = 30, color = "blue", "Dashboard construction") ))
使用 shinydashboard 构建仪表板

添加含多种项目的下拉菜单

包含多种项目的下拉菜单

使用 shinydashboard 构建仪表板

添加更多下拉菜单

  • 也可再添加一个下拉菜单

包含多个下拉菜单的页眉。

header <- dashboardHeader(
  title = "Analysis for global soccer tournament",
  titleWidth = 400,

dropdownMenu(type = "messages", messageItem("Data division", "Keep up the good work!", time = "5 mins"), messageItem("Twitter", "You have a Tweet!", time = "1 hour", icon=icon("twitter")), notificationItem("This is a notification."), taskItem(value = 30, color = "blue", "Dashboard construction")),
dropdownMenu(type = "notifications", notificationItem(icon = icon("users"), "This is another notification." )))
使用 shinydashboard 构建仪表板

侧边栏

  • dashboardSidebar() 定义侧边栏
  • 通过设置 width 调整宽度
  • sidebarMenu() 可在 shinydashboard 中放置多页

包含两个按钮的侧边栏

sidebar <- dashboardSidebar()
sidebar <- dashboardSidebar(width=400)
sidebar <- dashboardSidebar(width=400,
  sidebarMenu(
    id = "pages",
    menuItem("Many charts", tabName = "charts", 
        icon = icon("chart-line")),
    menuItem("Statistics", icon = icon("file-excel"), 
        tabName = "stats")
  )
)
sidebar <- dashboardSidebar(disable = TRUE)
使用 shinydashboard 构建仪表板

添加徽章

  • 还可添加徽章
    • 设置 badgeLabelbadgeColor

带有额外徽章的侧边栏

sidebar <- dashboardSidebar(width=400,
  sidebarMenu(
    id = "pages",
    menuItem("Many charts", tabName = "charts", 
             icon = icon("chart-line"),

badgeLabel = "New content!", badgeColor = "green"),
menuItem("Statistics", icon = icon("file-excel"), tabName = "stats",
badgeLabel = "urgent", badgeColor = "red")
) )
使用 shinydashboard 构建仪表板

添加子选项卡

  • 通过 menuSubItem() 添加子选项卡
  • 含子选项卡的选项卡无法添加徽章

包含子选项卡的侧边栏

sidebar <- dashboardSidebar(width=400,
  sidebarMenu(
    id = "pages",
    menuItem("Many charts", tabName = "charts", 
             icon = icon("chart-line"),
             badgeLabel = "New content!", 
             badgeColor = "green"),
    menuItem("Statistics", icon = icon("file-excel"), 
            tabName = "stats", 
            menuSubItem("Team 1", tabName = "team1", 
                     icon=icon("user")),
            menuSubItem("Team 2", tabName = "team2", 
                     icon=icon("user")))
  )
)
使用 shinydashboard 构建仪表板

在侧边栏添加输入与输出

  • 可在侧边栏添加输入与输出
  • 还需在 server 中定义输入与输出

包含 checkboxGroupInput() 的侧边栏。

sidebar <- dashboardSidebar(width=400,
  sidebarMenu(
    id = "pages",
    menuItem("Many charts", tabName = "charts", 
             icon = icon("chart-line"),
             badgeLabel = "New content!", 
             badgeColor = "green"),
    menuItem("Statistics", icon = icon("file-excel"), 
            tabName = "stats", 
            menuSubItem("Team 1", tabName = "team1", 
            icon=icon("user")),
            menuSubItem("Team 2", tabName = "team2", 
            icon=icon("user"))),

menuItem("A couple of checkboxes", checkboxGroupInput("checkboxes", "Day of the week", choices = c("Mon","Tue","Wed","Thu","Fri","Sat","Sun"), selected = c("Mon","Tue","Wed","Thu","Fri"))) ) )
使用 shinydashboard 构建仪表板

让我们练习!

使用 shinydashboard 构建仪表板

Preparing Video For Download...