Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
library(tidyverse)
library(gt)
library(escnscrapeR)
library(rvest)
# styled after: Dom Luszczyszyn
# https://twitter.com/domluszczyszyn/status/1353003080439255043?s=20
qbr <- espnscrapeR::get_nfl_qbr(2020) %>%
select(starter = name, qbr = qbr_total)
fpi <- espnscrapeR::scrape_fpi(season = 2020, stat = "PROJ") %>%
select(team, win_prob = make_sb_pct) %>%
left_join(
espnscrapeR::scrape_fpi(season = 2020) %>%
select(team, fpi_score = fpi, rk, off:st)
)
daily_lines <- "https://www.espn.com/nfl/lines" %>%
read_html() %>%
html_table() %>%
map(set_names, nm = c("team", "rec", "line", "open", "ml", "fpi")) %>%
bind_rows() %>%
tibble() %>%
mutate(
fpi = str_remove(fpi, "%"),
fpi = as.double(fpi)
)
daily_lines
nfl_data <- raw_json$events %>%
tibble(data = .) %>%
unnest_wider(data) %>%
select(competitions) %>%
unchop(competitions) %>%
unnest_auto(competitions) %>%
select(competitors, odds)%>%
unchop(competitors) %>%
unchop(odds) %>%
unnest_wider(competitors) %>%
rowwise() %>%
mutate(leaders = pluck(leaders, 1, "leaders", 1, "athlete", "displayName")) %>%
unnest_wider(odds) %>%
select(details, overUnder, team, starter = leaders) %>%
unnest_wider(team) %>%
select(team = displayName, nickname = shortDisplayName, abb = abbreviation, starter, odds = details, ou = overUnder, contains("color"), logo)
table_data <- daily_lines %>%
left_join(nfl_data) %>%
left_join(fpi) %>%
left_join(qbr)
table_data %>%
select(logo, starter, qbr, fpi_rank = rk, win_prob, line, ml) %>%
gt() %>%
text_transform(
locations = cells_body(vars(logo)),
fn = function(x){
web_image(url = x,
height = px(30))
}
) %>%
tab_header(title = html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Game Probabilities: January 24</b>")) %>%
cols_label(
logo = "",
starter = "Starting QB",
qbr = "QBR",
fpi_rank = "FPI Rank",
win_prob = "Probability",
line = "Odds",
ml = html("Money<br>Line")
) %>%
cols_align(align = "center", columns = vars(fpi_rank, win_prob)) %>%
data_color(
vars(win_prob),
colors = scales::col_numeric(
palette = c("#ff2d21", "white", "#489bca"),
domain = NULL
),
autocolor_text = FALSE
) %>%
tab_style(
style = cell_text(
font = google_font("Roboto Mono")
),
locations = cells_body(
vars(qbr, fpi_rank, win_prob, line, ml)
)
) %>%
tab_style(
style = cell_text(
font = google_font("Roboto Slab")
),
locations = cells_title(groups = "title")
) %>%
tab_style(
style = cell_borders(sides = "bottom", color = "#f9fbfc", weight = px(8)),
locations = cells_body(
rows = 2
)
) %>%
tab_style(
style = cell_borders(sides = "bottom", color = "transparent", weight = px(3)),
locations = cells_body(
columns = 1
)
) %>%
tab_style(
style = cell_borders(sides = "bottom", color = "transparent", weight = px(3)),
locations = cells_body(
rows = 4
)
) %>%
tab_style(
style = cell_text(weight = "bold"),
locations = cells_body(columns = vars(fpi_rank))
) %>%
fmt_percent(vars(win_prob), scale_values = FALSE, decimals = 1) %>%
tab_source_note(
html("<span style='float:right';><b>Data</b>: ESPN | <b>Table</b>: @thomas_mock| <b>Inspiration</b>: @domluszczyszyn</span>")
) %>%
opt_table_font(font = "Lato") %>%
tab_options(
heading.align = "left",
heading.border.bottom.width = px(3),
heading.border.bottom.color = "black",
table.border.top.width = px(3),
table.border.top.color = "transparent",
table.border.bottom.color = "transparent",
table.border.bottom.width = px(3),
table.background.color = "#f9fbfc",
column_labels.font.size = 12,
table.width = px(600),
heading.title.font.weight = "bold",
data_row.padding = px(1),
source_notes.padding = px(8)
)
@jthomasmock
Copy link
Author

jthomasmock commented Jan 24, 2021

nfl-prob

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment