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

Fix fb_player_match_logs() bug with players with multiple teams in same season #329

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: worldfootballR
Title: Extract and Clean World Football (Soccer) Data
Version: 0.6.4.0005
Version: 0.6.4.0006
Authors@R: c(
person("Jason", "Zivkovic", , "[email protected]", role = c("aut", "cre", "cph")),
person("Tony", "ElHabr", , "[email protected]", role = "ctb"),
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
### Bugs

* `fb_league_stats()` failing for `playing_time`. (0.6.4.0001) [#314](https://github.com/JaseZiv/worldfootballR/issues/314)
* `fb_advanced_match_stats()` throwing errors when there were no stat tables available for matches [#315](https://github.com/JaseZiv/worldfootballR/issues/315) (0.6.4.0002)
* `fb_advanced_match_stats()` throwing errors when there were no stat tables available for matches (0.6.4.0002) [#315](https://github.com/JaseZiv/worldfootballR/issues/315)
* `fb_player_match_logs()` failing for players who have played on multiple teams/leagues in the same season (0.6.4.0006) [#327](https://github.com/JaseZiv/worldfootballR/issues/327)

### Improvements
* `fb_player_season_stats()` now includes the ability to get a player's national team stats [https://github.com/JaseZiv/worldfootballR/pull/310/files] (0.6.4.0002)
Expand Down
61 changes: 30 additions & 31 deletions R/fb_player_match_logs.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,49 +82,48 @@ fb_player_match_logs <- function(player_url, season_end_year, stat_type, time_pa
TRUE ~ NA_character_
))

log_url <- all_logs %>%
filt_logs <- all_logs %>%
dplyr::filter(.data[["stat"]] == stat_type,
.data[["season_end"]] == season_end_year) %>%
dplyr::pull(log_urls)
.data[["season_end"]] == season_end_year)

if(length(log_url) == 0) stop(glue::glue("check stat type: {stat_type} or season end: {season_end_year} exists"))

if(nrow(filt_logs) == 0) stop(glue::glue("check stat type: {stat_type} or season end: {season_end_year} exists"))

season <- all_logs %>%
dplyr::filter(.data[["stat"]] == stat_type,
.data[["season_end"]] == season_end_year) %>%
dplyr::pull(season)

# Get match logs for stat -------------------------------------------------

Sys.sleep(1)
stat_page <- .load_page(paste0(main_url, log_url))

tab <- stat_page %>% rvest::html_nodes(".table_container") %>% rvest::html_nodes("table") %>% rvest::html_table() %>% data.frame()
all_tabs <- purrr::map_dfr(
1:nrow(filt_logs),
\(i) {
log_url <- filt_logs[i, ][["log_urls"]]
season <- filt_logs[i, ][["season"]]

tab <- .clean_table_names(tab)
Sys.sleep(1)
stat_page <- .load_page(paste0(main_url, log_url))

tab <- tab %>%
dplyr::filter(.data[["Date"]] != "") %>%
dplyr::mutate(Squad = sub("^.*?([A-Z])", "\\1", .data[["Squad"]]),
Opponent = sub("^.*?([A-Z])", "\\1", .data[["Opponent"]]),
Player = player_name,
Season = season) %>%
dplyr::select(.data[["Player"]], .data[["Season"]], dplyr::everything(), -.data[["Match Report"]])
tab <- stat_page %>% rvest::html_nodes(".table_container") %>% rvest::html_nodes("table") %>% rvest::html_table() %>% data.frame()

tab <- .clean_table_names(tab)

tab <- tab %>%
dplyr::filter(.data[["Date"]] != "") %>%
dplyr::mutate(Squad = sub("^.*?([A-Z])", "\\1", .data[["Squad"]]),
Opponent = sub("^.*?([A-Z])", "\\1", .data[["Opponent"]]),
Player = player_name,
Season = season) %>%
dplyr::select(.data[["Player"]], .data[["Season"]], dplyr::everything(), -.data[["Match Report"]])

non_num_vars <- c("Player", "Season", "Date", "Day", "Comp", "Round", "Venue", "Result", "Squad", "Opponent", "Start", "Pos")
cols_to_transform <- names(tab)[!names(tab) %in% non_num_vars]
non_num_vars <- c("Player", "Season", "Date", "Day", "Comp", "Round", "Venue", "Result", "Squad", "Opponent", "Start", "Pos")
cols_to_transform <- names(tab)[!names(tab) %in% non_num_vars]

suppressWarnings(
tab <- tab %>%
dplyr::mutate_at(.vars = cols_to_transform, .funs = function(x) {gsub(",", "", x)}) %>%
dplyr::mutate_at(.vars = cols_to_transform, .funs = function(x) {gsub("+", "", x)}) %>%
dplyr::mutate_at(.vars = cols_to_transform, .funs = as.numeric)
suppressWarnings(
tab <- tab %>%
dplyr::mutate_at(.vars = cols_to_transform, .funs = function(x) {gsub(",", "", x)}) %>%
dplyr::mutate_at(.vars = cols_to_transform, .funs = function(x) {gsub("+", "", x)}) %>%
dplyr::mutate_at(.vars = cols_to_transform, .funs = as.numeric)
)
tab
}
)

return(tab)
return(all_tabs)

}

Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-fbref.R
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ test_that("fb_player_match_logs() works", {
ederson_summary <- fb_player_match_logs("https://fbref.com/en/players/3bb7b8b4/Ederson", season_end_year = 2021, stat_type = 'summary', time_pause = 4)
expect_type(ederson_summary, "list")
expect_false(nrow(ederson_summary) == 0)

## multiple teams in same season
messi_summary <- fb_player_match_logs("https://fbref.com/en/players/d70ce98e/Lionel-Messi", season_end_year = 2023, stat_type = 'summary', time_pause = 4)
expect_type(messi_summary, "list")
expect_false(nrow(messi_summary) == 0)
})


Expand Down
Loading