From 7119e2dae895bd089a02768b0376603f214bf2bb Mon Sep 17 00:00:00 2001 From: Tony ElHabr Date: Sun, 17 Sep 2023 07:49:46 -0500 Subject: [PATCH 1/3] fix fb_player_match_logs for players with multiple teams in same season --- R/fb_player_match_logs.R | 61 ++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/R/fb_player_match_logs.R b/R/fb_player_match_logs.R index e21ca926..904bd181 100644 --- a/R/fb_player_match_logs.R +++ b/R/fb_player_match_logs.R @@ -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) } From 8ab80656071f7827e383ca0f05da8d72e0430c04 Mon Sep 17 00:00:00 2001 From: Tony ElHabr Date: Sun, 17 Sep 2023 07:52:05 -0500 Subject: [PATCH 2/3] add test for multiple teams in same season --- tests/testthat/test-fbref.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/testthat/test-fbref.R b/tests/testthat/test-fbref.R index 81b1d48e..a92ab19c 100644 --- a/tests/testthat/test-fbref.R +++ b/tests/testthat/test-fbref.R @@ -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) }) From c91e0a933906926f7c5897e6428d5fd0c8290633 Mon Sep 17 00:00:00 2001 From: Tony ElHabr Date: Mon, 18 Sep 2023 06:37:54 -0500 Subject: [PATCH 3/3] bump package version and add news --- DESCRIPTION | 2 +- NEWS.md | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d11ac064..167bf413 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", , "jaseziv83@gmail.com", role = c("aut", "cre", "cph")), person("Tony", "ElHabr", , "anthonyelhabr@gmail.com", role = "ctb"), diff --git a/NEWS.md b/NEWS.md index d1c17aa2..3e6ad1bd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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)