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