forked from eliottvincent/bay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jaa.Rmd
290 lines (249 loc) · 7.58 KB
/
jaa.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
---
title: "Women's World Cup Predictions"
output: html_document
resource_files:
layout: external.html
permalink: /ja
{% include head.html %}
{% include header.html %}
- '.'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r table, echo=FALSE}
library(reactable)
library(htmltools)
library(tidyverse)
forecasts <- read.csv("wwc_forecasts.csv", stringsAsFactors = FALSE)
rating_cols <- c("spi", "global_o", "global_d")
group_cols <- c("group_1", "group_2", "group_3")
knockout_cols <- c("make_round_of_16", "make_quarters", "make_semis", "make_final", "win_league")
forecasts <- forecasts[, c("team", "points", "group", rating_cols, group_cols, knockout_cols)]
rating_column <- function(maxWidth = 55, ...) {
colDef(maxWidth = maxWidth, align = "center", class = "cell number", ...)
}
group_column <- function(class = NULL, ...) {
colDef(cell = format_pct, maxWidth = 70, align = "center", class = paste("cell number", class), ...)
}
knockout_column <- function(maxWidth = 70, class = NULL, ...) {
colDef(
cell = format_pct,
maxWidth = maxWidth,
class = paste("cell number", class),
style = function(value) {
# Lighter color for <1%
if (value < 0.01) {
list(color = "#aaa")
} else {
list(color = "#111", background = knockout_pct_color(value))
}
},
...
)
}
format_pct <- function(value) {
if (value == 0) " \u2013 " # en dash for 0%
else if (value == 1) "\u2713" # checkmark for 100%
else if (value < 0.01) " <1%"
else if (value > 0.99) ">99%"
else formatC(paste0(round(value * 100), "%"), width = 4)
}
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("#ff2700", "#f8fcf8", "#44ab43"), bias = 1.3)
def_rating_color <- make_color_pal(c("#ff2700", "#f8fcf8", "#44ab43"), bias = 0.6)
knockout_pct_color <- make_color_pal(c("#ffffff", "#f2fbd2", "#c9ecb4", "#93d3ab", "#35b0ab"), bias = 2)
tbl <- reactable(
forecasts,
pagination = FALSE,
defaultSorted = "win_league",
defaultSortOrder = "desc",
defaultColGroup = colGroup(headerClass = "group-header"),
columnGroups = list(
colGroup(name = "Team Rating", columns = rating_cols),
colGroup(name = "Chance of Finishing Group Stage In ...", columns = group_cols),
colGroup(name = "Knockout Stage Chances", columns = knockout_cols)
),
defaultColDef = colDef(class = "cell", headerClass = "header"),
columns = list(
team = colDef(
defaultSortOrder = "asc",
minWidth = 200,
headerStyle = list(fontWeight = 700),
cell = function(value, index) {
div(
class = "team",
img(class = "flag", alt = paste(value, "flag"), src = sprintf("images/%s.png", value)),
div(class = "team-name", value),
div(class = "record", sprintf("%s pts.", forecasts[index, "points"]))
)
}
),
points = colDef(show = FALSE),
group = colDef(defaultSortOrder = "asc", align = "center", maxWidth = 75,
class = "cell group", headerStyle = list(fontWeight = 700)),
spi = rating_column(format = colFormat(digits = 1)),
global_o = rating_column(
name = "Off.",
cell = function(value) {
scaled <- (value - min(forecasts$global_o)) / (max(forecasts$global_o) - min(forecasts$global_o))
color <- off_rating_color(scaled)
value <- format(round(value, 1), nsmall = 1)
div(class = "spi-rating", style = list(background = color), value)
}
),
global_d = rating_column(
name = "Def.",
defaultSortOrder = "asc",
cell = function(value) {
scaled <- 1 - (value - min(forecasts$global_d)) / (max(forecasts$global_d) - min(forecasts$global_d))
color <- def_rating_color(scaled)
value <- format(round(value, 1), nsmall = 1)
div(class = "spi-rating", style = list(background = color), value)
}
),
group_1 = group_column(name = "1st Place", class = "border-left"),
group_2 = group_column(name = "2nd Place"),
group_3 = group_column(name = "3rd Place"),
make_round_of_16 = knockout_column(name = "Make Round of 16", class = "border-left"),
make_quarters = knockout_column(name = "Make Qtr-Finals"),
make_semis = knockout_column(name = "Make Semifinals", maxWidth = 90),
make_final = knockout_column(name = "Make Final"),
win_league = knockout_column(name = "Win World Cup")
),
# Emphasize borders between groups when sorting by group
rowClass = JS("
function(rowInfo, state) {
const firstSorted = state.sorted[0]
if (firstSorted && firstSorted.id === 'group') {
const nextRow = state.pageRows[rowInfo.viewIndex + 1]
if (nextRow && rowInfo.row.group !== nextRow.group) {
return 'group-last'
}
}
}"
),
showSortIcon = FALSE,
borderless = TRUE,
class = "standings-table"
)
div(class = "standings",
div(class = "title",
h2("2019 Women's World Cup Predictions"),
"Soccer Power Index (SPI) ratings and chances of advancing for every team"
),
tbl,
"Forecast from before 3rd group matches"
)
```
---
Source: [FiveThirtyEight](https://projects.fivethirtyeight.com/2019-womens-world-cup-predictions/)
Raw data: [`wwc_forecasts.csv`](wwc_forecasts.csv)
<h2>Source Code</h2>
Full source: [`vignettes/womens-world-cup/womens-world-cup.Rmd`](https://github.com/glin/reactable/blob/master/vignettes/womens-world-cup/womens-world-cup.Rmd)
```{r ref.label="table", eval=FALSE}
```
```{r}
tags$link(href = "https://fonts.googleapis.com/css?family=Karla:400,700|Fira+Mono&display=fallback", rel = "stylesheet")
```
```{css}
.standings {
font-family: Karla, "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
}
.title {
margin: 18px 0;
font-size: 16px;
}
.title h2 {
font-size: 20px;
font-weight: 600;
}
.standings-table {
margin-bottom: 20px;
}
/* Align header text to the bottom */
.header,
.group-header {
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.header {
border-bottom-color: #555;
font-size: 13px;
font-weight: 400;
text-transform: uppercase;
}
/* Highlight headers when sorting */
.header:hover,
.header[aria-sort="ascending"],
.header[aria-sort="descending"] {
background-color: #eee;
}
.border-left {
border-left: 2px solid #555;
}
/* Use box-shadow to create row borders that appear behind vertical borders */
.cell {
box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
}
.group-last .cell {
box-shadow: inset 0 -2px 0 #555;
}
.team {
display: flex;
align-items: baseline;
}
.record {
margin-left: 5px;
color: #999;
font-size: 13px;
}
.team-name {
font-size: 18px;
font-weight: 700;
}
.flag {
margin-right: 8px;
height: 21px;
border: 1px solid #f0f0f0;
}
.group {
font-size: 19px;
}
.number {
font-family: "Fira Mono", Consolas, Monaco, monospace;
font-size: 16px;
line-height: 30px;
white-space: pre;
}
.spi-rating {
width: 30px;
height: 30px;
border: 1px solid rgba(0, 0, 0, 0.03);
border-radius: 50%;
color: #000;
font-size: 13px;
letter-spacing: -2px;
}
```
```{css echo=FALSE}
/* rmarkdown html documents */
.main-container {
max-width: 1054px !important;
}
h1.title {
display: none;
}
/* pkgdown articles */
.contents {
width: 1054px;
}
.page-header {
display: none;
}
```