-
Notifications
You must be signed in to change notification settings - Fork 4
/
2019 NFL Team Ratings.Rmd
513 lines (465 loc) · 15.2 KB
/
2019 NFL Team Ratings.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
---
title: '2020 RStudio Table Contest Submission'
author: "Kyle Cuilla"
date: "9/19/2020"
output: html_document
---
```{r setup, warnings = FALSE, message = FALSE, include = FALSE}
knitr::opts_chunk$set(warnings = FALSE, message = FALSE, echo = FALSE)
library(rvest)
library(dplyr)
library(tidyr)
library(stringr)
library(htmltools)
library(htmlwidgets)
library(reactable)
```
```{r data, echo = FALSE}
url <- "https://www.pro-football-reference.com/years/2019/"
### Get AFC conference table from pro-football-reference site
AFC_table <- url %>%
xml2::read_html() %>%
html_nodes(xpath = '//*[@id="AFC"]') %>%
html_table()
AFC_table <- AFC_table[[1]]
### Get NFC conference table from pro-football-reference site
NFC_table <- url %>%
xml2::read_html() %>%
html_nodes(xpath = '//*[@id="NFC"]') %>%
html_table()
NFC_table <- NFC_table[[1]]
### Combine AFC and NFC tables together
NFL_table <- rbind(AFC_table, NFC_table)
NFL_table_clean <- NFL_table %>%
### Create NFL divisions column
mutate(Division = ifelse(str_detect(Tm, "FC"), Tm, NA)) %>%
fill(Division, .direction = "down") %>%
### Remove team division names from Tm column
filter(str_detect(Tm, "FC", negate = TRUE)) %>%
### Add column to say if team made playoffs based off of pre-existing asterisks
mutate(Playoffs = ifelse(str_detect(Tm, "[*+]"), "Yes", "No")) %>%
### Remove asterisks and plus signs next to team names
mutate(Tm = gsub("[*+]", "", Tm)) %>%
rename(Record = `W-L%`) %>%
### Convert W, L, T into one column named "Record"
unite(Record, W, L, T, sep = "-") %>%
### Extract team name
mutate(Team = word(Tm, -1)) %>%
mutate(
Team = case_when(
Team == "Redskins" ~ "Washington",
TRUE ~ Team
)
) %>%
### Convert columns containing numbers from character to numeric
mutate_at(c("SRS", "OSRS", "DSRS", "PF", "PA", "MoV", "SoS"),
as.numeric) %>%
### Rank SoS column from 1 to 4 with 1 containing the lowest values and 4 containing the highest
mutate(SoS_rating = ntile(SoS, 4)) %>%
select(
Division,
Team,
Record,
Playoffs,
SoS_rating,
PF,
PA,
MoV,
OSRS,
DSRS,
SRS,
)
```
```{r table, echo = FALSE}
### Horizontal bar chart to display points scored and points against
bar_chart <-
function(label,
width = "100%",
height = "13px",
fill = "#00bfc4",
background = NULL) {
bar <-
div(style = list(
background = fill,
width = width,
height = height
))
chart <-
div(style = list(
flexGrow = 1,
marginLeft = "8px",
background = background
),
bar)
div(style = list(display = "flex", alignItems = "center"), label, chart)
}
### Create orange-blue color palette for Team Rating SRS columns
make_color_pal <- function(colors, bias = 1) {
get_color <- colorRamp(colors, bias = bias)
function(x)
rgb(get_color(x), maxColorValue = 255)
}
off_rating_color <-
make_color_pal(c("#67a9cf", "#f8fcf8", "#ef8a62"), bias = 1.3)
def_rating_color <-
make_color_pal(c("#67a9cf", "#f8fcf8", "#ef8a62"), bias = 0.8)
team_rating_column <- function(maxWidth = 55, ...) {
colDef(
maxWidth = maxWidth,
align = "right",
class = "cell number",
headerStyle = list(fontWeight = "500"),
...
)
}
table <-
reactable(
NFL_table_clean,
pagination = FALSE,
showSortIcon = FALSE,
highlight = TRUE,
compact = TRUE,
defaultSorted = "SRS",
defaultSortOrder = "desc",
defaultColDef = colDef(headerClass = "header colheader"),
columnGroups = list(
colGroup(
name = "Team Rating (SRS)",
columns = c("SRS", "OSRS", "DSRS"),
headerClass = "groupheader"
),
colGroup(
name = "Team Scoring & Margin of Victory",
columns = c("PF", "PA", "MoV"),
headerClass = "groupheader"
)
),
# Add border between Divisions when sorting by Division
rowClass = JS("
function(rowInfo, state) {
const firstSorted = state.sorted[0]
if (firstSorted && firstSorted.id === 'Division') {
const nextRow = state.pageRows[rowInfo.viewIndex + 1]
if (nextRow && rowInfo.row.Division !== nextRow.Division) {
return 'Division-last'
}
}
}"
),
columns = list(
Division = colDef(
class = "division-name cell",
maxWidth = 90,
### Group teams into divisions when sorting by division - if sorting by other column then ungroup
style = JS("function(rowInfo, colInfo, state) {
var firstSorted = state.sorted[0]
if (!firstSorted || firstSorted.id === 'Division') {
var prevRow = state.pageRows[rowInfo.viewIndex - 1]
}
}")),
Team = colDef(
minWidth = 145,
class = "cell",
cell = function(value, index) {
### Team logos from images folder
img_src <- knitr::image_uri(sprintf("images/%s.png", value))
image <- img(class = "logo",
src = img_src,
alt = value)
div(class = "team", image,
### Team name
div(class = "team-name", value),
### Team record
div(class = "record", sprintf("(%s)", NFL_table_clean[index, "Record"])))
}
),
### Hide separate record column
Record = colDef(show = FALSE),
SRS = team_rating_column(
name = "Total",
cell = function(value) {
### Normalize team rating in order to assign color from color palette
normalized <-
(value - min(NFL_table_clean$SRS)) / (max(NFL_table_clean$SRS) - min(NFL_table_clean$SRS))
color <- off_rating_color(normalized)
value <- format(value, nsmall = 1, digits = 1)
### Round corners of cell
div(class = "roundcorners",
style = list(background = color),
value)
}
),
OSRS = team_rating_column(
name = "Off.",
cell = function(value) {
### Normalize team rating in order to assign color from color palette
normalized <-
(value - min(NFL_table_clean$OSRS)) / (max(NFL_table_clean$OSRS) - min(NFL_table_clean$OSRS))
color <- off_rating_color(normalized)
value <- format(value, nsmall = 1, digits = 1)
### Round corners of cell
div(class = "roundcorners",
style = list(background = color),
value)
}
),
DSRS = team_rating_column(
name = "Def.",
cell = function(value) {
### Normalize team rating in order to assign color from color palette
normalized <-
(value - min(NFL_table_clean$DSRS)) / (max(NFL_table_clean$DSRS) - min(NFL_table_clean$DSRS))
color <- off_rating_color(normalized)
value <- format(value, nsmall = 1, digits = 1)
### Round corners of cell
div(class = "roundcorners",
style = list(background = color),
value)
}
),
PF = colDef(
name = "Points Scored",
align = "left",
### Add column border to left side of column
class = "border-left cell number",
headerStyle = list(fontWeight = "500"),
cell = function(value) {
### Calculate width of bar color to display
width <- paste0(value / max(NFL_table_clean$PF) * 100, "%")
bar_chart(value,
width = width,
fill = "#ef8a62",
background = "#e1e1e1")
}
),
PA = colDef(
name = "Points Against",
align = "left",
class = "cell number",
headerStyle = list(fontWeight = "500"),
cell = function(value) {
### Calculate width of bar color to display
width <- paste0(value / max(NFL_table_clean$PA) * 100, "%")
bar_chart(value,
width = width,
fill = "#ef8a62",
background = "#e1e1e1")
}
),
MoV = colDef(
maxWidth = 55,
### Add column border to right side of column
class = "cell number border-right ",
headerStyle = list(fontWeight = "500"),
### For any positive number, add "+" sign. For any negative number leave as is
cell = function(value) {
if (value > 0)
paste0("+", value)
else
value
},
### For any positive number, assign green color. For any negative number assign red color
style = function(value) {
if (value > 0) {
color <- "#008000"
} else if (value < 0) {
color <- "#e00000"
} else {
color <- "#777"
}
list(color = color)
}
),
SoS_rating = colDef(
name = "SoS",
align = "center",
maxWidth = 65,
class = "cell number border-left",
cell = function(value) {
### For teams that were assigned a SoS_rating of 4 (highest rating), show a double-black diamond (note: there was no diamond icon available in the Font Awesome Free library, so the solution was to use a square icon and rotate it at a 45 degree angle)
if (value == 4) {
### In order to display two icons in the same column, they need to be placed in a list
list(tagAppendAttributes(shiny::icon("square", class = "rotate")),
tagAppendAttributes(shiny::icon("square", class = "rotate")))
### For teams that were assigned a SoS_rating of 3, show a single black diamond
} else if (value == 3) {
tagAppendAttributes(shiny::icon("square", class = "rotate"))
### For teams that were assigned a SoS_rating of 2, show a blue square
} else if (value == 2) {
tagAppendAttributes(shiny::icon("square"))
} else {
### For teams that were assigned a SoS_rating of 1, show a green circle
tagAppendAttributes(shiny::icon("circle"))
}
},
style = function(value) {
### Assign colors to icons
if (value %in% c(3, 4)) {
color <- "black"
} else if (value == 2) {
color <- "blue"
} else {
color <- "green"
}
list(color = color)
}
),
Playoffs = colDef(
maxWidth = 75,
align = "center",
class = "cell number border-left",
### If team made the playoffs in 2019, assign a green check mark. If they did not, assign a red X symbol
cell = function(value) {
if (value == "Yes")
tagAppendAttributes(shiny::icon("check"))
else
tagAppendAttributes(shiny::icon("times"))
},
### Assign colors to icons
style = function(value) {
if (value == "Yes") {
color <- "green"
} else {
color <- "red"
}
list(color = color)
}
)
)
)
### Add title and subtitle to top of page above table
div(class = "team-ratings",
div(class = "title",
h2("2019 NFL Team Rating & Stats"),
"Ratings and results from every NFL team from the 2019 season"),
table,
### Add legend and source below the table
tags$span(
style = "color:#777",
"Note: Strength of Schedule (SoS) aligns with the ski trail difficulty rating system:",
### In order to display icons with color, needed to create a new css class for the symbols that contained a color option
tagAppendAttributes(shiny::icon("circle", class = "green-circle-legend")), "= Easy",
tagAppendAttributes(shiny::icon("square", class = "blue-square-legend")), "= Moderate",
tagAppendAttributes(shiny::icon("square", class = "black-diamond-legend1")), "= Difficult",
list(tagAppendAttributes(shiny::icon("square", class = "black-diamond-legend1")),
tagAppendAttributes(shiny::icon("square", class = "black-diamond-legend2"))), "= Most Difficult",
div(
"Table created by: Kyle Cuilla @kc_analytics • Data: Pro-Football-Reference.com")
)
)
```
```{r}
### Load font from Google Fonts
tags$link(href = "https://fonts.googleapis.com/css?family=Karla:400,700|Fira+Mono&display=fallback", rel = "stylesheet")
```
```{css}
/* Row and column borders */
.cell {
box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.07);
}
.border-left {
border-left: 1px solid #777;
}
.border-right {
border-right: 1px solid #777;
}
.Division-last .cell {
box-shadow: inset 0 -1px 0 #777;
}
/* Column header hover formatting */
.header:hover,
.header[aria-sort="ascending"],
.header[aria-sort="descending"] {
background-color: #b3b3b3;
color: #fff;
}
.header:active,
.header[aria-sort="ascending"],
.header[aria-sort="descending"] {
background-color: #555;
color: #fff;
}
/* Column header formatting */
.colheader {
font-family: "Open Sans", sans-serif;
font-size: 15px;
border-bottom: 2px solid #555;
}
.groupheader {
font-family: "Open Sans", sans-serif;
font-size: 15px;
}
/* Number formatting */
.number {
font-family: "Fira Mono", Consolas, Monaco, monospace;
font-size: 13px;
line-height: 30px;
white-space: pre;
}
/* Text formatting */
.team-ratings {
font-family: Karla, "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 12px;
}
/* Division column formatting */
.division-name {
font-family: Karla, "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 30px;
}
/* Team column formatting */
.team {
display: flex;
align-items: baseline;
}
.logo {
margin-right: 10px;
height: 24px;
}
.team-name {
font-size: 14px;
font-weight: 700;
}
.record {
margin-left: 4px;
color: #999;
font-size: 13px;
}
/* Rotate SoS square into a diamond */
.rotate{
transform: rotate(45deg);
}
/* Round corners under Team Rating columns */
.roundcorners {
border-radius: 10px;
color: #000;
padding-bottom: 2px;
padding-right: 2px;
width: 46px;
height: 28px;
}
/* Formatting for title above table */
.title {
font-family: "Open Sans", sans-serif;
font-size: 16px;
margin: 16px 0;
}
/* SoS legend symbols underneath chart */
.black-diamond-legend1{
transform: rotate(45deg);
color: #000;
margin-left: 7px;
}
.black-diamond-legend2{
transform: rotate(45deg);
color: #000;
}
.green-circle-legend{
color: #008000;
margin-left: 7px;
}
.blue-square-legend{
color: #0000FF;
margin-left: 7px;
}
```