Bắt đầu ngayBắt đầu miễn phí

Xây dựng dashboard đầu tiên của bạn

Dữ liệu đã được lưu trong một data frame tên là soccer. Có 3 biểu đồ được định nghĩa sẵn:

  • goals_plot: Số bàn thắng của mỗi đội
  • yellow_plot, red_plot: Số thẻ vàng/đỏ mỗi đội nhận

Một số hàm trợ giúp tùy chỉnh được dùng ở đây:

  • daytime_fn(): Lấy ngày & giờ của một trận đấu cụ thể
  • venue_fn(): Lấy sân thi đấu của một trận cụ thể
  • grp_fn(): Lấy bảng đấu của cặp đấu
  • team1_fn(), team2_fn(): Tên của đội 1/2
  • score1_fn(), score2_fn(): Số bàn thắng của đội 1/2

Bài tập này là một phần của khóa học

Xây dựng Dashboard với shinydashboard

Xem khóa học

Hướng dẫn bài tập

  • Điền vào chỗ trống cho các đầu ra ở hàng thứ nhất đến hàng thứ ba của tab/trang đầu tiên.
  • Điền vào chỗ trống cho các đầu ra biểu đồ ở trang thứ hai, với các tên "goals", "yellow" và "red".
  • Ghép UI và server lại với nhau để hiển thị shinydashboard.

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

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
___
Chỉnh sửa và Chạy Mã