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

add fb_team_match_stats and understat_available_teams functions #320

merged 36 commits into from
Sep 9, 2023

Conversation

shufinskiy
Copy link
Contributor

@JaseZiv hello.

I create two functions for package:

  1. fb_team_match_stats - return main team stats in match.
    Снимок экрана от 2023-08-23 14-30-36

  2. understat_available_teams - return all available teams names for selected league. Helper function, the results of which can be passed to function understat_team_meta

@@ -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

@shufinskiy shufinskiy requested a review from JaseZiv August 29, 2023 16:03
Copy link
Collaborator

@tonyelhabr tonyelhabr left a comment

Choose a reason for hiding this comment

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

looks pretty good overall. in the future, i would love to see this for this to be split across 2 PRs (1 for each new function). no need to change this PR.

two requests:

  1. can you add usage to the vignettes?
  2. can you add tests?

R/fb_team_match_stats.R Show resolved Hide resolved
Copy link
Collaborator

@tonyelhabr tonyelhabr left a comment

Choose a reason for hiding this comment

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

bumped the package version and added an entry to the news file to get this past the finish line 👍

Copy link
Owner

@JaseZiv JaseZiv left a comment

Choose a reason for hiding this comment

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

lgtm

@JaseZiv JaseZiv merged commit cbd6366 into JaseZiv:main Sep 9, 2023
@shufinskiy shufinskiy deleted the fbteamstats branch September 10, 2023 06:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants