FootballData.Rmd
library(FootballData)
library(tidyverse)
library(glue)
#devtools::install_github("hadley/emo")
library(emo)
if(Sys.getenv("API_FOOTBALL_DATA") == "") stop("API_FOOTBALL_DATA missing")
# table helper
table_it <- function(x){
x %>%
kableExtra::kbl(escape = F) %>%
kableExtra::kable_classic("striped", full_width = TRUE)
}
First I will pull all the leagues that are in the free tier, and show them on a table.
# Get competitions
competitions <- football_get_competition() %>%
filter(
# filter to what's in the free plan
plan == "TIER_ONE"
&
# The data structures returned for country level competitions is
# different, so let's remove it
!country %in% c("World","Europe"))
competitions %>% select(flag,country,league) %>% table_it()
flag | country | league |
---|---|---|
NA | Brazil | Série A |
|
England | Championship |
|
England | Premier League |
|
France | Ligue 1 |
|
Germany | Bundesliga |
|
Italy | Serie A |
|
Netherlands | Eredivisie |
|
Portugal | Primeira Liga |
|
Spain | Primera Division |
# Get league info
league <- competitions %>%
filter(country == "Germany" & league == "Bundesliga") %>%
pull(competition_id) %>%
football_get_standing()
league %>%
mutate(team = paste(crest,team)) %>%
select(team,playedGames,form,points) %>%
table_it()
team | playedGames | form | points |
---|---|---|---|
|
23 | W,L,D,W,W | 52 |
|
23 | W,W,W,W,W | 50 |
|
23 | W,W,D,W,W | 45 |
|
23 | L,W,W,W,W | 42 |
|
23 | W,W,D,L,W | 39 |
|
23 | L,D,D,W,L | 37 |
|
23 | D,W,D,L,D | 34 |
|
23 | W,L,D,W,L | 34 |
|
23 | L,L,D,L,D | 33 |
|
23 | W,W,D,L,W | 32 |
|
23 | D,W,D,L,L | 27 |
|
22 | W,L,D,D,W | 26 |
|
23 | W,D,L,L,L | 26 |
|
23 | L,L,L,W,W | 21 |
|
23 | L,L,D,L,L | 18 |
|
22 | L,L,D,L,L | 18 |
|
23 | L,W,D,W,L | 17 |
|
23 | L,L,D,L,D | 9 |
team_of_interest <- "SC Freiburg"
league %>%
filter(team == team_of_interest) %>%
pull(team_id) %>%
football_get_upcoming() %>%
arrange(utcDate) %>%
slice(1) %>%
mutate(
text = glue(
"Today is {Sys.Date()}\n",
"{team_of_interest}'s next game is on {as.Date(utcDate)}\n",
"Home: {homeTeam$name}; Away {awayTeam$name}"
)
) %>%
pull(text)
#> Today is 2021-03-02
#> SC Freiburg's next game is on 2021-03-06
#> Home: SC Freiburg; Away RB Leipzig
emoji_bad <- emo::ji("poop")
emoji_good <- emo::ji("beer")
add_bad <- function(x){
gsub("-",emoji_bad,x = x)
}
add_good <- function(x){
gsub("\\+",emoji_good,x = x)
}
league %>%
football_calculate_competition_metrics() %>%
mutate(
team = paste(crest,team),
metric_mood = as.character(metric_mood),
metric_mood = case_when(
str_detect(metric_mood,"Jubilant") ~ kableExtra::cell_spec(
metric_mood, color = "black", background = "#98FB98"
),
str_detect(metric_mood,"Optimistic") ~ kableExtra::cell_spec(
metric_mood, color = "black", background = "#e5f5f9"
),
str_detect(metric_mood,"Ambivalent") ~ kableExtra::cell_spec(
metric_mood, color = "black", background = "#f7fcb9"
),
str_detect(metric_mood,"Doldrums") ~ kableExtra::cell_spec(
metric_mood, color = "black", background = "#ffc4c4"
),
TRUE ~ metric_mood
)
) %>%
rename(mood = metric_mood,numeric_mood = metric_mood_numeric) %>%
mutate_at(vars(matches("metric_")),add_bad) %>%
mutate_all(add_good) %>%
select(team,points,starts_with("metric_"),numeric_mood,mood) %>%
table_it()
team | points | metric_season_position | metric_recent_wins | metric_back_of_net | numeric_mood | mood |
---|---|---|---|---|---|---|
|
52 | 🍺🍺🍺🍺 top of the league | 🍺🍺 recent wins | 🍺🍺🍺 strikers are on form | 9 | Jubilant: FC Bayern München are on a roll |
|
50 | 🍺🍺 strong league position | 🍺🍺🍺🍺 5 win streak | 🍺🍺🍺 strikers are on form | 9 | Jubilant: RB Leipzig are on a roll |
|
45 | 🍺🍺 strong league position | 🍺🍺 recent wins | 🍺 scored more than conceded | 5 | Optimistic: VfL Wolfsburg are doing well |
|
42 | 🍺 safe in middle of league | 🍺🍺 recent wins | 🍺 scored more than conceded | 4 | Optimistic: Eintracht Frankfurt are doing well |
|
39 | 🍺 safe in middle of league | 🍺🍺 recent wins | 🍺 scored more than conceded | 4 | Optimistic: Borussia Dortmund are doing well |
|
37 | 🍺 safe in middle of league | 💩 lost last game | 🍺 scored more than conceded | 1 | Optimistic: Bayer 04 Leverkusen are doing well |
|
34 | 🍺 safe in middle of league | drew last game | 🍺 scored more than conceded | 2 | Optimistic: 1. FC Union Berlin are doing well |
|
34 | 🍺 safe in middle of league | 💩 lost last game | 🍺 scored more than conceded | 1 | Optimistic: SC Freiburg are doing well |
|
33 | 💩 so so season | 💩💩 no recent wins | 🍺 scored more than conceded | -2 | Ambivalent: Borussia Mönchengladbach aren’t doing well, but hey - at least they aren’t FC Schalke 04 |
|
32 | 💩 so so season | 🍺🍺 recent wins | 🍺 scored more than conceded | 2 | Optimistic: VfB Stuttgart are doing well |
|
27 | 💩 so so season | 💩 lost last game | 💩 conceded more than scored | -3 | Ambivalent: TSG 1899 Hoffenheim aren’t doing well, but hey - at least they aren’t FC Schalke 04 |
|
26 | 💩 so so season | 🍺 won last game | 💩 conceded more than scored | -1 | Ambivalent: SV Werder Bremen aren’t doing well, but hey - at least they aren’t FC Schalke 04 |
|
26 | 💩 so so season | 💩💩 more recent losses than wins | 💩 conceded more than scored | -4 | Ambivalent: FC Augsburg aren’t doing well, but hey - at least they aren’t FC Schalke 04 |
|
21 | 💩 so so season | 🍺 won last game | 💩 conceded more than scored | -1 | Ambivalent: 1. FC Köln aren’t doing well, but hey - at least they aren’t FC Schalke 04 |
|
18 | 💩💩 poor season | 💩💩 no recent wins | 💩 conceded more than scored | -5 | Doldrums: Hertha BSC are doing poor. Now’s a good time to bad mouth the manager. |
|
18 | 💩💩 poor season | 💩💩 no recent wins | 💩💩💩 defense is a seive | -7 | Doldrums: Arminia Bielefeld are doing poor. Now’s a good time to bad mouth the manager. |
|
17 | 💩💩💩💩 relagation? | 💩 lost last game | 💩💩💩 defense is a seive | -8 | Doldrums: 1. FSV Mainz 05 are doing poor. Now’s a good time to bad mouth the manager. |
|
9 | 💩💩💩💩 relagation? | 💩💩 no recent wins | 💩💩💩 defense is a seive | -9 | Doldrums: FC Schalke 04 are doing poorly. Avoid supporters at all costs. |