Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fb_team_match_stats and understat_available_teams functions #320

Merged
merged 36 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7d854ac
add fb_team_match_stats function
shufinskiy Aug 23, 2023
2683fdc
Update documents package
shufinskiy Aug 23, 2023
9bf1cd3
add test fb_match_tean_stats
shufinskiy Aug 23, 2023
33f77d8
fix ncol length
shufinskiy Aug 23, 2023
8f6b19a
fix name fb_team_match_stats
shufinskiy Aug 23, 2023
93e6853
add fb_team_match_stats to vignettes
shufinskiy Aug 23, 2023
24b08f4
add understat_available_teams function
shufinskiy Aug 23, 2023
c9521b9
add documentation understat_available_teams
shufinskiy Aug 23, 2023
f931160
add understat_available_teams function to vignettes
shufinskiy Aug 23, 2023
cc74904
address #315 and drive by global bindings
JaseZiv Aug 25, 2023
a179711
Update understat_available_teams function
shufinskiy Aug 25, 2023
0a06176
Merge branch 'JaseZiv:main' into fbteamstats
shufinskiy Aug 25, 2023
0ad7e65
rm suffix 2023 in url available teams
shufinskiy Aug 25, 2023
5f6d592
Merge branch 'fbteamstats' of github.com:shufinskiy/worldfootballR in…
shufinskiy Aug 25, 2023
4b1795e
Merge pull request #322 from JaseZiv/adv-match-bug
JaseZiv Aug 26, 2023
f0ac3bf
added check for the non-dom-league param
Sep 2, 2023
e5f422f
dplyr::arrange() not currently working on provided test multiple seas…
Sep 2, 2023
8a9e94e
add test and docs
tonyelhabr Sep 2, 2023
6effce2
use slightly cleaner logic for filtering
tonyelhabr Sep 2, 2023
01dc879
oops keep filters on country and tier
tonyelhabr Sep 2, 2023
922df25
updated description and news
francescozonaro Sep 6, 2023
f9407f8
Merge branch 'fix-no-dom-league-stats' of https://github.com/francesc…
francescozonaro Sep 6, 2023
c2552c2
Merge pull request #325 from francescozonaro/fix-no-dom-league-stats
JaseZiv Sep 7, 2023
ce40ada
add fb_team_match_stats function
shufinskiy Aug 23, 2023
74bb8bf
Update documents package
shufinskiy Aug 23, 2023
2d6c013
add test fb_match_tean_stats
shufinskiy Aug 23, 2023
970b5a3
fix ncol length
shufinskiy Aug 23, 2023
da77cfc
fix name fb_team_match_stats
shufinskiy Aug 23, 2023
0193fa6
add fb_team_match_stats to vignettes
shufinskiy Aug 23, 2023
836a659
add understat_available_teams function
shufinskiy Aug 23, 2023
dea46e7
add documentation understat_available_teams
shufinskiy Aug 23, 2023
81feb08
add understat_available_teams function to vignettes
shufinskiy Aug 23, 2023
541efdb
Update understat_available_teams function
shufinskiy Aug 25, 2023
3e81881
rm suffix 2023 in url available teams
shufinskiy Aug 25, 2023
e37d59c
bump description and news
tonyelhabr Sep 8, 2023
703e8f6
Merge branch 'fbteamstats' of https://github.com/shufinskiy/worldfoot…
tonyelhabr Sep 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export(fb_squad_wages)
export(fb_team_goal_logs)
export(fb_team_match_log_stats)
export(fb_team_match_results)
export(fb_team_match_stats)
export(fb_team_player_stats)
export(fb_teams_urls)
export(get_advanced_match_stats)
Expand Down Expand Up @@ -56,6 +57,7 @@ export(tm_team_staff_history)
export(tm_team_staff_urls)
export(tm_team_transfer_balances)
export(tm_team_transfers)
export(understat_available_teams)
export(understat_league_match_results)
export(understat_league_season_shots)
export(understat_match_shots)
Expand All @@ -65,6 +67,7 @@ export(understat_team_players_stats)
export(understat_team_season_shots)
export(understat_team_stats_breakdown)
importFrom(cli,cli_alert)
importFrom(dplyr,case_when)
importFrom(dplyr,filter)
importFrom(dplyr,mutate)
importFrom(dplyr,pull)
Expand All @@ -73,14 +76,15 @@ importFrom(httr,GET)
importFrom(httr,content)
importFrom(httr,set_cookies)
importFrom(jsonlite,fromJSON)
importFrom(lubridate,today)
importFrom(lubridate,ymd)
importFrom(magrittr,"%>%")
importFrom(progress,progress_bar)
importFrom(purrr,insistently)
importFrom(purrr,map_chr)
importFrom(purrr,map_dfr)
importFrom(purrr,possibly)
importFrom(purrr,rate_backoff)
importFrom(purrr,safely)
importFrom(readr,read_csv)
importFrom(readr,type_convert)
importFrom(rlang,.data)
Expand Down
166 changes: 166 additions & 0 deletions R/fb_team_match_stats.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#' Get match team stats for webpage
#'
#' Internal function to return match team stats for a selected match, with input being a webpage, not url
#'
#' @param match_page the fbref.com URL for the required match
#'
#' @return returns a dataframe with the match team stats for a selected match
#'
#' @importFrom magrittr %>%
#' @importFrom rlang .data
#' @noRd
#'
.get_team_match_stats <- function(match_page) {

Game_URL <- match_page %>% rvest::html_nodes(".langs") %>% rvest::html_node(".en") %>% rvest::html_attr("href")
each_game_page <- tryCatch(match_page, error = function(e) NA)

if(!is.na(each_game_page)) {
team_stats <- each_game_page %>% rvest::html_nodes("#team_stats") %>% rvest::html_nodes("tr") %>% rvest::html_text()
team_stats_extra <- each_game_page %>% rvest::html_nodes("#team_stats_extra") %>% rvest::html_text()

tryCatch( {League <- each_game_page %>% rvest::html_nodes("h1+ div a:nth-child(1)") %>% rvest::html_text()}, error = function(e) {League <- NA})
tryCatch( {Match_Date <- each_game_page %>% rvest::html_nodes(".venuetime") %>% rvest::html_attr("data-venue-date")}, error = function(e) {Match_Date <- NA})
tryCatch( {Matchweek <- each_game_page %>% rvest::html_nodes("h1+ div") %>% rvest::html_text()}, error = function(e) {Matchweek <- NA})

tryCatch( {Home_Team <- each_game_page %>% rvest::html_nodes("div+ strong a") %>% rvest::html_text() %>% .[1]}, error = function(e) {Home_Team <- NA})
tryCatch( {Home_Formation <- each_game_page %>% rvest::html_nodes(".lineup#a") %>% rvest::html_nodes("th") %>% rvest::html_text() %>% .[1] %>% gsub(".*\\(", "", .) %>% gsub("\\)", "", .)}, error = function(e) {Home_Formation <- NA})
tryCatch( {Home_Possession <- team_stats %>% .[3] %>% gsub("[\n\t]", "", .) %>% strsplit(., '%') %>% unlist(.) %>% .[1]}, error = function(e) {Home_Possession <- NA})
tryCatch( {Home_Success_Pass <- team_stats %>% .[5] %>% gsub("[\n\t]", "", .) %>% regmatches(., regexpr('.+(?=%\\d{1,3})', ., perl = TRUE)) %>% regmatches(., regexpr('.+(?= of)', ., perl = TRUE))}, error = function(e) {Home_Success_Pass <- NA})
tryCatch( {Home_Pass <- team_stats %>% .[5] %>% gsub("[\n\t]", "", .) %>% regmatches(., regexpr('.+(?=%\\d{1,3})', ., perl = TRUE)) %>% regmatches(., regexpr('(?<=of )\\d{1,4}', ., perl = TRUE))}, error = function(e) {Home_Pass <- NA})
tryCatch( {Home_Passing_Accuracy <- team_stats %>% .[5] %>% gsub("[\n\t]", "", .) %>% regmatches(., regexpr('.+(?=%\\d{1,3})', ., perl = TRUE)) %>% regmatches(., regexpr('\\d{1,3}$', ., perl = TRUE))}, error = function(e) {Home_Passing_Accuracy <- NA})
tryCatch( {Home_Fouls <- team_stats_extra %>% regmatches(., regexpr('\\d{1,4}(?=Fouls)', ., perl = TRUE))}, error = function(e) {Home_Fouls <- NA})
tryCatch( {Home_Corners <- team_stats_extra %>% regmatches(., regexpr('\\d{1,4}(?=Corners)', ., perl = TRUE))}, error = function(e) {Home_Corners <- NA})
tryCatch( {Home_Crosses <- team_stats_extra %>% regmatches(., regexpr('\\d{1,4}(?=Crosses)', ., perl = TRUE))}, error = function(e) {Home_Crosses <- NA})
tryCatch( {Home_Touches <- team_stats_extra %>% regmatches(., regexpr('\\d{1,4}(?=Touches)', ., perl = TRUE))}, error = function(e) {Home_Touches<- NA})
tryCatch( {Home_Tackles <- team_stats_extra %>% regmatches(., regexpr('\\d{1,4}(?=Tackles)', ., perl = TRUE))}, error = function(e) {Home_Tackles <- NA})
tryCatch( {Home_Interceptions <- team_stats_extra %>% regmatches(., regexpr('\\d{1,4}(?=Interceptions)', ., perl = TRUE))}, error = function(e) {Home_Interceptions <- NA})
tryCatch( {Home_Aerials_Won <- team_stats_extra %>% regmatches(., regexpr('\\d{1,4}(?=Aerials Won)', ., perl = TRUE))}, error = function(e) {Home_Aerials_Won <- NA})
tryCatch( {Home_Clearances <- team_stats_extra %>% regmatches(., regexpr('\\d{1,4}(?=Clearances)', ., perl = TRUE))}, error = function(e) {Home_Clearances <- NA})
tryCatch( {Home_Offsides <- team_stats_extra %>% regmatches(., regexpr('\\d{1,4}(?=Offsides)', ., perl = TRUE))}, error = function(e) {Home_Offsides <- NA})
tryCatch( {Home_Goal_Kicks <- team_stats_extra %>% regmatches(., regexpr('\\d{1,4}(?=Goal Kicks)', ., perl = TRUE))}, error = function(e) {Home_Goal_Kicks <- NA})
tryCatch( {Home_Throw_Ins <- team_stats_extra %>% regmatches(., regexpr('\\d{1,4}(?=Throw Ins)', ., perl = TRUE))}, error = function(e) {Home_Throw_Ins <- NA})
tryCatch( {Home_Long_Balls <- team_stats_extra %>% regmatches(., regexpr('\\d{1,4}(?=Long Balls)', ., perl = TRUE))}, error = function(e) {Home_Long_Balls <- NA})
tryCatch( {Home_Yellow_Cards <- each_game_page %>% rvest::html_nodes(".cards") %>% .[1] %>% rvest::html_nodes("span.yellow_card, span.yellow_red_card") %>% length()}, error = function(e) {Home_Yellow_Cards <- 0})
tryCatch( {Home_Red_Cards <- each_game_page %>% rvest::html_nodes(".cards") %>% .[1] %>% rvest::html_nodes("span.red_card, span.yellow_red_card") %>% length()}, error = function(e) {Home_Red_Cards <- 0})

tryCatch( {Away_Team <- each_game_page %>% rvest::html_nodes("div+ strong a") %>% rvest::html_text() %>% .[2]}, error = function(e) {Away_Team <- NA})
tryCatch( {Away_Formation <- each_game_page %>% rvest::html_nodes(".lineup#b") %>% rvest::html_nodes("th") %>% rvest::html_text() %>% .[1] %>% gsub(".*\\(", "", .) %>% gsub("\\)", "", .)}, error = function(e) {Away_Formation <- NA})
tryCatch( {Away_Possession <- team_stats %>% .[3] %>% gsub("[\n\t]", "", .) %>% strsplit(., '%') %>% unlist(.) %>% .[2]}, error = function(e) {Away_Possession <- NA})
tryCatch( {Away_Success_Pass <- team_stats %>% .[5] %>% gsub("[\n\t]", "", .) %>% regmatches(., regexpr('(?<=%).+', ., perl = TRUE)) %>% regmatches(., regexpr('\\d{1,4}(?= of)', ., perl = TRUE))}, error = function(e) {Away_Success_Pass <- NA})
tryCatch( {Away_Pass <- team_stats %>% .[5] %>% gsub("[\n\t]", "", .) %>% regmatches(., regexpr('(?<=%).+', ., perl = TRUE)) %>% regmatches(., regexpr('(?<=of )\\d{1,4}', ., perl = TRUE))}, error = function(e) {Away_Pass <- NA})
tryCatch( {Away_Passing_Accuracy <- team_stats %>% .[5] %>% gsub("[\n\t]", "", .) %>% regmatches(., regexpr('(?<=%).+', ., perl = TRUE)) %>% regmatches(., regexpr('\\d{1,3}(?=%)', ., perl = TRUE))}, error = function(e) {Away_Passing_Accuracy <- NA})
tryCatch( {Away_Fouls <- team_stats_extra %>% regmatches(., regexpr('(?<=Fouls)\\d{1,4}', ., perl = TRUE))}, error = function(e) {Away_Fouls <- NA})
tryCatch( {Away_Corners <- team_stats_extra %>% regmatches(., regexpr('(?<=Corners)\\d{1,4}', ., perl = TRUE))}, error = function(e) {Away_Corners <- NA})
tryCatch( {Away_Crosses <- team_stats_extra %>% regmatches(., regexpr('(?<=Crosses)\\d{1,4}', ., perl = TRUE))}, error = function(e) {Away_Crosses <- NA})
tryCatch( {Away_Touches <- team_stats_extra %>% regmatches(., regexpr('(?<=Touches)\\d{1,4}', ., perl = TRUE))}, error = function(e) {Away_Touches <- NA})
tryCatch( {Away_Tackles <- team_stats_extra %>% regmatches(., regexpr('(?<=Tackles)\\d{1,4}', ., perl = TRUE))}, error = function(e) {Away_Tackles <- NA})
tryCatch( {Away_Interceptions <- team_stats_extra %>% regmatches(., regexpr('(?<=Interceptions)\\d{1,4}', ., perl = TRUE))}, error = function(e) {Away_Interceptions <- NA})
tryCatch( {Away_Aerials_Won <- team_stats_extra %>% regmatches(., regexpr('(?<=Aerials Won)\\d{1,4}', ., perl = TRUE))}, error = function(e) {Away_Aerials_Won <- NA})
tryCatch( {Away_Clearances <- team_stats_extra %>% regmatches(., regexpr('(?<=Clearances)\\d{1,4}', ., perl = TRUE))}, error = function(e) {Away_Clearances <- NA})
tryCatch( {Away_Offsides <- team_stats_extra %>% regmatches(., regexpr('(?<=Offsides)\\d{1,4}', ., perl = TRUE))}, error = function(e) {Away_Offsides <- NA})
tryCatch( {Away_Goal_Kicks <- team_stats_extra %>% regmatches(., regexpr('(?<=Goal Kicks)\\d{1,4}', ., perl = TRUE))}, error = function(e) {Away_Goal_Kicks <- NA})
tryCatch( {Away_Throw_Ins <- team_stats_extra %>% regmatches(., regexpr('(?<=Throw Ins)\\d{1,4}', ., perl = TRUE))}, error = function(e) {Away_Throw_Ins <- NA})
tryCatch( {Away_Long_Balls <- team_stats_extra %>% regmatches(., regexpr('(?<=Long Balls)\\d{1,4}', ., perl = TRUE))}, error = function(e) {Away_Long_Balls <- NA})
tryCatch( {Away_Yellow_Cards <- each_game_page %>% rvest::html_nodes(".cards") %>% .[2] %>% rvest::html_nodes("span.yellow_card, span.yellow_red_card") %>% length()}, error = function(e) {Away_Yellow_Cards <- 0})
tryCatch( {Away_Red_Cards <- each_game_page %>% rvest::html_nodes(".cards") %>% .[2] %>% rvest::html_nodes("span.red_card, span.yellow_red_card") %>% length()}, error = function(e) {Away_Red_Cards <- 0})

suppressWarnings(each_game <- cbind(League, Match_Date, Matchweek, Home_Team, Home_Formation, Home_Possession, Home_Success_Pass, Home_Pass, Home_Passing_Accuracy, Home_Fouls, Home_Corners, Home_Crosses, Home_Touches, Home_Tackles,
Home_Interceptions, Home_Aerials_Won, Home_Clearances, Home_Offsides, Home_Goal_Kicks, Home_Throw_Ins, Home_Long_Balls, Home_Yellow_Cards, Home_Red_Cards,
Away_Team, Away_Formation, Away_Possession, Away_Success_Pass, Away_Pass, Away_Passing_Accuracy, Away_Fouls, Away_Corners, Away_Crosses, Away_Touches, Away_Tackles, Away_Interceptions, Away_Aerials_Won,
Away_Clearances, Away_Offsides, Away_Goal_Kicks, Away_Throw_Ins, Away_Long_Balls, Away_Yellow_Cards, Away_Red_Cards, Game_URL) %>%
dplyr::as_tibble() %>%
dplyr::mutate(Home_Possession = as.numeric(.data[["Home_Possession"]]),
Home_Success_Pass = as.numeric(.data[["Home_Success_Pass"]]),
Home_Pass = as.numeric(.data[["Home_Pass"]]),
Home_Passing_Accuracy = as.numeric(.data[["Home_Passing_Accuracy"]]),
Home_Fouls = as.numeric(.data[["Home_Fouls"]]),
Home_Corners = as.numeric(.data[["Home_Corners"]]),
Home_Crosses = as.numeric(.data[["Home_Crosses"]]),
Home_Touches = as.numeric(.data[["Home_Touches"]]),
Home_Tackles = as.numeric(.data[["Home_Tackles"]]),
Home_Interceptions = as.numeric(.data[["Home_Interceptions"]]),
Home_Aerials_Won = as.numeric(.data[["Home_Aerials_Won"]]),
Home_Clearances = as.numeric(.data[["Home_Clearances"]]),
Home_Offsides = as.numeric(.data[["Home_Offsides"]]),
Home_Goal_Kicks = as.numeric(.data[["Home_Goal_Kicks"]]),
Home_Throw_Ins = as.numeric(.data[["Home_Throw_Ins"]]),
Home_Long_Balls = as.numeric(.data[["Home_Long_Balls"]]),
Away_Possession = as.numeric(.data[["Away_Possession"]]),
Away_Success_Pass = as.numeric(.data[["Away_Success_Pass"]]),
Away_Pass = as.numeric(.data[["Away_Pass"]]),
Away_Passing_Accuracy = as.numeric(.data[["Away_Passing_Accuracy"]]),
Away_Fouls = as.numeric(.data[["Away_Fouls"]]),
Away_Corners = as.numeric(.data[["Away_Corners"]]),
Away_Crosses = as.numeric(.data[["Away_Crosses"]]),
Away_Touches = as.numeric(.data[["Away_Touches"]]),
Away_Tackles = as.numeric(.data[["Away_Tackles"]]),
Away_Interceptions = as.numeric(.data[["Away_Interceptions"]]),
Away_Aerials_Won = as.numeric(.data[["Away_Aerials_Won"]]),
Away_Clearances = as.numeric(.data[["Away_Clearances"]]),
Away_Offsides = as.numeric(.data[["Away_Offsides"]]),
Away_Goal_Kicks = as.numeric(.data[["Away_Goal_Kicks"]]),
Away_Throw_Ins = as.numeric(.data[["Away_Throw_Ins"]]),
Away_Long_Balls = as.numeric(.data[["Away_Long_Balls"]]))
)


} else {
print(glue::glue("{Game_URL} is not available"))
each_game <- data.frame()
}

return(each_game)
}

#' Get FBref match team stats
#'
#' Returns match team stats for selected matches.
#'
#' @param match_url the fbref.com URL for the required match
#' @param time_pause the wait time (in seconds) between page loads
#'
#' @return returns a dataframe with the match team stats for a selected match
#'
#' @importFrom magrittr %>%
#' @importFrom rlang .data
#'
#' @export
#'
#' @examples
#' \dontrun{
#' try({
#' match <- fb_match_urls(country = "AUS", gender = "F", season_end_year = 2021, tier = "1st")[1]
#' df <- fb_team_match_stats(match_url = match)
tonyelhabr marked this conversation as resolved.
Show resolved Hide resolved
#' })
#' }

fb_team_match_stats <- function(match_url, time_pause=3) {

time_wait <- time_pause

each_match_report <- function(match_url, time_pause=time_wait) {
pb$tick()

# put sleep in as per new user agreement on FBref
Sys.sleep(time_pause)

match_page <- tryCatch(.load_page(match_url), error = function(e) NA)

if(!is.na(match_page)) {
each_game <- .get_team_match_stats(match_page)
} else {
print(glue::glue("{match_url} is not available"))
each_game <- data.frame()
}
return(each_game)
}

# create the progress bar with a progress function.
pb <- progress::progress_bar$new(total = length(match_url))

all_games <- match_url %>%
purrr::map_df(each_match_report)

return(all_games)
}
48 changes: 48 additions & 0 deletions R/worldfootballr_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,54 @@ tm_team_staff_urls <- function(team_urls, staff_role) {
}


#' Get Understat available teams
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would this function scale across seasons? Hard coding team names means every season this would need to be re-done?

I don't love this function tbh

Copy link
Contributor Author

@shufinskiy shufinskiy Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand you. I found an option how to do this without hard coding.

understat_available_teams <- function(leagues){
  correct_leagues <- c("EPL", "La liga", "Bundesliga", "Serie A", "Ligue 1", "RFPL")

  teams_list <- list()

  for(lg in leagues){
    if(!lg %in% correct_leagues){warning(glue::glue("League {lg} not found. Please check understats.com for the correct league name")); next}
    match_url <- switch (lg,
                         'EPL' = 'https://understat.com/team/Arsenal',
                         'La liga' = 'https://understat.com/team/Barcelona',
                         'Bundesliga' = 'https://understat.com/team/Bayern_Munich',
                         'Serie A' = 'https://understat.com/team/AC_Milan',
                         'Ligue 1' = 'https://understat.com/team/Paris_Saint_Germain',
                         'RFPL' = 'https://understat.com/team/Spartak_Moscow'
    )
    team_page <- tryCatch(.load_page(match_url), error = function(e) NA)
    teams_list[[lg]] <- team_page %>% rvest::html_nodes(".header-wrapper") %>% html_text() %>% gsub("([\n\t])|(\\d{4}/\\d{4})", "", .) %>% gsub('(?<!\\s)([[:upper:]])', '(&&)\\1', ., perl = TRUE) %>%
      strsplit(., "\\(&&\\)", perl = TRUE) %>% unlist(.) %>% .[2:length(.)] %>%  unlist(.)
  }
  if(length(leagues) == 1){
    return(unlist(teams_list, use.names = FALSE))
  }

  return(teams_list)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

@JaseZiv JaseZiv Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been thinking about this for the last few days... I just think it feels a little out of place in worldfootballR, but happy to keep it in place for now

#'
#' Returns all available team names for the selected league
#'
#' @param league the available league in Understat as outlined below
#'#'
#' The leagues currently available for Understat are:
#' \emph{"EPL"}, \emph{"La liga}", \emph{"Bundesliga"},
#' \emph{"Serie A"}, \emph{"Ligue 1"}, \emph{"RFPL"}
#'
#' #' @return a character vector teams names
#'
#' @export
#' @examples
##' \dontrun{
#' try({
#' understat_available_teams(league = 'EPL')
#' })
#' }
understat_available_teams <- function(league){
leagues <- c("EPL", "La liga", "Bundesliga", "Serie A", "Ligue 1", "RFPL")
if(!league %in% leagues) stop("Check league name")

available_teams <- list(
'EPL' = c('Arsenal', 'Aston Villa', 'Bournemouth', 'Brentford', 'Brighton', 'Burnley', 'Cardiff', 'Chelsea', 'Crystal Palace', 'Everton', 'Fulham', 'Huddersfield',
'Hull', 'Leeds', 'Leicester', 'Liverpool', 'Luton', 'Manchester City', 'Manchester United', 'Middlesbrough', 'Newcastle United', 'Norwich',
'Nottingham Forest', 'Queens Park Rangers', 'Sheffield United', 'Southampton', 'Stoke', 'Sunderland', 'Swansea', 'Tottenham', 'Watford', 'West Bromwich Albion',
'West Ham', 'Wolverhampton Wanderers'),
'La liga' = c('Alaves', 'Almeria', 'Athletic Club', 'Atletico Madrid', 'Barcelona', 'Cadiz', 'Celta Vigo', 'Cordoba', 'Deportivo La Coruna', 'Eibar', 'Elche', 'Espanyol',
'Getafe', 'Girona', 'Granada', 'Las Palmas', 'Leganes', 'Levante', 'Malaga', 'Mallorca', 'Osasuna', 'Rayo Vallecano', 'Real Betis', 'Real Madrid',
'Real Sociedad', 'Real Valladolid', 'SD Huesca', 'Sevilla', 'Sporting Gijon', 'Valencia', 'Villarreal'),
'Bundesliga' = c('Arminia Bielefeld', 'Augsburg', 'Bayer Leverkusen', 'Bayern Munich', 'Bochum', 'Borussia Dortmund', 'Borussia M.Gladbach', 'Darmstadt', 'Eintracht Frankfurt',
'FC Cologne', 'FC Heidenheim', 'Fortuna Duesseldorf', 'Freiburg', 'Greuther Fuerth', 'Hamburger SV', 'Hannover 96', 'Hertha Berlin', 'Hoffenheim',
'Ingolstadt', 'Mainz 05', 'Nuernberg', 'Paderborn', 'RasenBallsport Leipzig', 'Schalke 04', 'Union Berlin', 'VfB Stuttgart', 'Werder Bremen', 'Wolfsburg'),
'Serie A' = c('AC Milan', 'Atalanta', 'Benevento', 'Bologna', 'Brescia', 'Cagliari', 'Carpi', 'Cesena', 'Chievo', 'Cremonese', 'Crotone', 'Empoli', 'Fiorentina', 'Frosinone',
'Genoa', 'Inter', 'Juventus', 'Lazio', 'Lecce', 'Monza', 'Napoli', 'Palermo', 'Parma', 'Parma Calcio 1913', 'Pescara', 'Roma', 'Salernitana', 'Sampdoria',
'Sassuolo', 'SPAL 2013', 'Spezia', 'Torino', 'Udinese', 'Venezia', 'Verona'),
'Ligue 1' = c('Ajaccio', 'Amiens', 'Angers', 'Auxerre', 'Bordeaux', 'Brest', 'Caen', 'Clermont Foot', 'Dijon', 'Evian Thonon Gaillard', 'GFC Ajaccio', 'Guingamp', 'Le Havre',
'Lens', 'Lille', 'Lorient', 'Lyon', 'Marseille', 'Metz', 'Monaco', 'Montpellier', 'Nancy', 'Nantes', 'Nice', 'Nimes', 'Paris Saint Germain', 'Reims', 'Rennes',
'Saint-Etienne', 'SC Bastia', 'Strasbourg', 'Toulouse', 'Troyes'),
'RFPL' = c('Amkar', 'Anzhi Makhachkala', 'Arsenal Tula', 'Baltika', 'CSKA Moscow', 'Dinamo Moscow', 'Fakel', 'FC Krasnodar', 'FC Orenburg', 'FC Rostov', 'FC Rotor Volgograd',
'FC Tambov', 'FC Ufa', 'FC Yenisey Krasnoyarsk', 'FK Akhmat', 'Khimki', 'Krylya Sovetov Samara', 'Kuban Krasnodar', 'Lokomotiv Moscow', 'Mordovya',
'Nizhny Novgorod', 'PFC Sochi', 'Rubin Kazan', 'SKA-Khabarovsk', 'Spartak Moscow', 'Tom Tomsk', 'Torpedo Moscow', 'Tosno', 'Ural', 'Zenit St. Petersburg')
)

return(available_teams[[league]])
}

#' Get Understat team info
#'
#' Retrieve Understat team metadata, including team URLs. Similar to `understatr::get_team_meta`.
Expand Down
2 changes: 1 addition & 1 deletion man/fb_league_stats.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions man/fb_team_match_stats.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading