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)
}

Intro

Get competitions

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

Dive into a league

# 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
FC Bayern München 23 W,L,D,W,W 52
RB Leipzig 23 W,W,W,W,W 50
VfL Wolfsburg 23 W,W,D,W,W 45
Eintracht Frankfurt 23 L,W,W,W,W 42
Borussia Dortmund 23 W,W,D,L,W 39
Bayer 04 Leverkusen 23 L,D,D,W,L 37
1. FC Union Berlin 23 D,W,D,L,D 34
SC Freiburg 23 W,L,D,W,L 34
Borussia Mönchengladbach 23 L,L,D,L,D 33
VfB Stuttgart 23 W,W,D,L,W 32
TSG 1899 Hoffenheim 23 D,W,D,L,L 27
SV Werder Bremen 22 W,L,D,D,W 26
FC Augsburg 23 W,D,L,L,L 26
1. FC Köln 23 L,L,L,W,W 21
Hertha BSC 23 L,L,D,L,L 18
Arminia Bielefeld 22 L,L,D,L,L 18
1. FSV Mainz 05 23 L,W,D,W,L 17
FC Schalke 04 23 L,L,D,L,D 9

Get upcoming games for a team

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

Calculate the moods

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
FC Bayern München 52 🍺🍺🍺🍺 top of the league 🍺🍺 recent wins 🍺🍺🍺 strikers are on form 9 Jubilant: FC Bayern München are on a roll
RB Leipzig 50 🍺🍺 strong league position 🍺🍺🍺🍺 5 win streak 🍺🍺🍺 strikers are on form 9 Jubilant: RB Leipzig are on a roll
VfL Wolfsburg 45 🍺🍺 strong league position 🍺🍺 recent wins 🍺 scored more than conceded 5 Optimistic: VfL Wolfsburg are doing well
Eintracht Frankfurt 42 🍺 safe in middle of league 🍺🍺 recent wins 🍺 scored more than conceded 4 Optimistic: Eintracht Frankfurt are doing well
Borussia Dortmund 39 🍺 safe in middle of league 🍺🍺 recent wins 🍺 scored more than conceded 4 Optimistic: Borussia Dortmund are doing well
Bayer 04 Leverkusen 37 🍺 safe in middle of league 💩 lost last game 🍺 scored more than conceded 1 Optimistic: Bayer 04 Leverkusen are doing well
1. FC Union Berlin 34 🍺 safe in middle of league drew last game 🍺 scored more than conceded 2 Optimistic: 1. FC Union Berlin are doing well
SC Freiburg 34 🍺 safe in middle of league 💩 lost last game 🍺 scored more than conceded 1 Optimistic: SC Freiburg are doing well
Borussia Mönchengladbach 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
VfB Stuttgart 32 💩 so so season 🍺🍺 recent wins 🍺 scored more than conceded 2 Optimistic: VfB Stuttgart are doing well
TSG 1899 Hoffenheim 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
SV Werder Bremen 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
FC Augsburg 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
1. FC Köln 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
Hertha BSC 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.
Arminia Bielefeld 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.
1. FSV Mainz 05 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.
FC Schalke 04 9 💩💩💩💩 relagation? 💩💩 no recent wins 💩💩💩 defense is a seive -9 Doldrums: FC Schalke 04 are doing poorly. Avoid supporters at all costs.