開始使用免費開始

建立你的第一個儀表板

資料已經存成名為 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 列,補上輸出的空格。
  • 在第二個頁面補上圖形輸出的空格,這些輸出分別命名為「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
___
編輯並執行程式碼