开始使用免费开始使用

构建您的第一个仪表板

数据已存为名为 soccer 的数据框。已预定义了 3 个图表:

  • goals_plot:各球队进球数
  • yellow_plotred_plot:各球队黄牌/红牌数

这里使用了一些自定义辅助函数:

  • daytime_fn():获取某场比赛的日期与时间
  • venue_fn():获取某场比赛的球场
  • grp_fn():获取对阵所属的小组
  • team1_fn()team2_fn():获取球队 1/2 的名称
  • score1_fn()score2_fn():获取球队 1/2 的进球数

本练习是课程的一部分

使用 shinydashboard 构建仪表板

查看课程

练习说明

  • 在第一页的第 1 至第 3 行中,为各输出填空。
  • 在第二页为图形输出填空,其输出 ID 分别为 "goals"、"yellow" 和 "red"。
  • 将 UI 与 server 放在一起以渲染 shinydashboard。

交互式实操练习

通过完成这段示例代码来试试这个练习。

body <- dashboardBody(tabItems(
  tabItem(tabName = "matchtab", 
  fluidRow(selectInput("match", "Match number", choices = 1:nrow(soccer)),
           infoBoxOutput("daytime"), infoBoxOutput("venue"), infoBoxOutput("grp")),
  fluidRow(valueBoxOutput("team1", width = 3), valueBoxOutput("score1", width = 3), 
           valueBoxOutput("team2", width = 3), valueBoxOutput("score2", width = 3)),
  fluidRow(plotOutput("histogram"))),
  tabItem(tabName = "statstab", 
          tabBox(tabPanel("Goals", plotOutput("goals", height = "700px")),
                 tabPanel("Yellow cards", plotOutput("yellow", height = "700px")),
                 tabPanel("Red cards", plotOutput("red"))))
))

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output) {
  # Fill in outputs in first to third rows of the first page
  output$daytime <- renderInfoBox(infoBox("Day and time", 
                                          daytime_fn(input$match), 
                                          icon = icon("calendar"), 
                                          color = "green"))
  output$venue <- ___(infoBox("Venue", 
                                        venue_fn(input$match), 
                                        icon = icon("map"), 
                                        color = "green"))
  output$___ <- ___(___("Group", 
                                      grp_fn(input$match), 
                                      color = "green"))
  output$team1 <- ___(valueBox("Team 1", team1_fn(input$match), color = "blue"))
  output$score1 <- ___(___("# of goals", score1_fn(input$match), color = "blue"))
  output$___ <- ___(valueBox("Team 2", team2_fn(input$match), color = "red")) 
  ___ <- ___(valueBox("# of goals", score2_fn(input$match), color = "red"))
  output$histogram <- renderPlot(plot_histogram(input$match))
  # Fill in outputs in the second page
  output$goals <- ___(goals_plot)
  output$___ <- ___(yellow_plot)
  ___ <- ___(red_plot)
}

# Put the UI and server together
___
编辑并运行代码