diff --git a/DESCRIPTION b/DESCRIPTION index 00c6297e..d0fb4445 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: worldfootballR Title: Extract and Clean World Football (Soccer) Data -Version: 0.5.6 +Version: 0.5.6.1000 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 5e9f9023..d121e49a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,12 @@ +# worldfootballR 0.5.6.1000 + +### Bugs + +* `player_transfer_history()` no longer throwing errors for players without a club [#137](https://github.com/JaseZiv/worldfootballR/issues/137) +* Increase sleep times in tests for FBref functions + +*** + # worldfootballR 0.5.6 * CRAN submission diff --git a/R/player_transfer_history.R b/R/player_transfer_history.R index 02713ba0..f0b45c53 100644 --- a/R/player_transfer_history.R +++ b/R/player_transfer_history.R @@ -50,7 +50,7 @@ player_transfer_history <- function(player_urls) { .tm_fix_dates() %>% lubridate::ymd(), error = function(e) NA_character_) # country_flags <- tryCatch(all_transfer_rows[each_row] %>% rvest::html_nodes(".flagge"), error = function(e) NA) - country_from <- tryCatch(all_transfer_rows[each_row] %>% rvest::html_nodes(".tm-player-transfer-history-grid__old-club .tm-player-transfer-history-grid__flag") %>% rvest::html_attr("alt") %>% stringr::str_squish(), error = function(e) NA_character_) + country_from <- tryCatch(all_transfer_rows[each_row] %>% rvest::html_nodes(".tm-player-transfer-history-grid__old-club .tm-player-transfer-history-grid__flag") %>% rvest::html_attr("alt") %>% stringr::str_squish() %>% .replace_empty_na(), error = function(e) NA_character_) country_to <- tryCatch(all_transfer_rows[each_row] %>% rvest::html_nodes(".tm-player-transfer-history-grid__new-club .tm-player-transfer-history-grid__flag") %>% rvest::html_attr("alt"), error = function(e) NA_character_) # to handle for players that are retired, like: "https://www.transfermarkt.com/massimiliano-allegri/profil/spieler/163501": if(rlang::is_empty(country_to)) { diff --git a/tests/testthat/test-fbref.R b/tests/testthat/test-fbref.R index a984f294..b9bde053 100644 --- a/tests/testthat/test-fbref.R +++ b/tests/testthat/test-fbref.R @@ -16,7 +16,7 @@ test_that("fb_league_urls() works", { expect_false(grepl("https://fbref.com/en/$", league_urls[1])) }) - +Sys.sleep(3) test_that("fb_team_urls() works", { testthat::skip_on_cran() team_urls <- fb_teams_urls("https://fbref.com/en/comps/9/Premier-League-Stats") @@ -25,7 +25,7 @@ test_that("fb_team_urls() works", { expect_false(grepl("https://fbref.com/en/$", team_urls[1])) }) - +Sys.sleep(3) test_that("fb_player_urls() works", { testthat::skip_on_cran() player_urls <- fb_player_urls("https://fbref.com/en/squads/fd962109/Fulham-Stats") @@ -34,32 +34,32 @@ test_that("fb_player_urls() works", { expect_false(grepl("https://fbref.com/en/$", player_urls[1])) }) - +Sys.sleep(3) test_that("get_advanced_match_stats() works", { testthat::skip_on_cran() testthat::skip_if_offline() # test the functions returns the data for each stat_type expect_type(get_advanced_match_stats(match_url = "https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1", - stat_type = "summary", team_or_player = "team"), "list") + stat_type = "summary", team_or_player = "team", time_pause = 4), "list") expect_type(get_advanced_match_stats(match_url = "https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1", - stat_type = "passing", team_or_player = "team"), "list") + stat_type = "passing", team_or_player = "team", time_pause = 4), "list") expect_type(get_advanced_match_stats(match_url = "https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1", - stat_type = "passing_types", team_or_player = "team"), "list") + stat_type = "passing_types", team_or_player = "team", time_pause = 4), "list") expect_type(get_advanced_match_stats(match_url = "https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1", - stat_type = "defense", team_or_player = "team"), "list") + stat_type = "defense", team_or_player = "team", time_pause = 4), "list") expect_type(get_advanced_match_stats(match_url = "https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1", - stat_type = "possession", team_or_player = "team"), "list") + stat_type = "possession", team_or_player = "team", time_pause = 4), "list") expect_type(get_advanced_match_stats(match_url = "https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1", - stat_type = "misc", team_or_player = "team"), "list") + stat_type = "misc", team_or_player = "team", time_pause = 4), "list") expect_type(get_advanced_match_stats(match_url = "https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1", - stat_type = "keeper", team_or_player = "team"), "list") + stat_type = "keeper", team_or_player = "team", time_pause = 4), "list") expect_type(get_advanced_match_stats(match_url = "https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1", - stat_type = "keeper", team_or_player = "player"), "list") + stat_type = "keeper", team_or_player = "player", time_pause = 4), "list") # test that multiple match_url can be passed to the function test_urls <- c("https://fbref.com/en/matches/e0a84e7e/Southampton-Manchester-United-November-29-2020-Premier-League", "https://fbref.com/en/matches/703db983/Paris-Saint-Germain-Toulouse-August-25-2019-Ligue-1") - test_df <- get_advanced_match_stats(match_url = test_urls, stat_type = "possession", team_or_player = "team") + test_df <- get_advanced_match_stats(match_url = test_urls, stat_type = "possession", team_or_player = "team", time_pause = 4) expect_type(test_df, "list") expect_equal(nrow(test_df), 4) @@ -68,14 +68,14 @@ test_that("get_advanced_match_stats() works", { # test that an invalid stat_type will error expect_error(get_advanced_match_stats(match_url = "https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1", - stat_type = "test", team_or_player = "team")) + stat_type = "test", team_or_player = "team", time_pause = 4)) expect_error(get_advanced_match_stats(match_url = "https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1", - stat_type = "test", team_or_player = "player")) + stat_type = "test", team_or_player = "player", time_pause = 4)) }) - +Sys.sleep(3) test_that("get_match_lineups() works", { testthat::skip_on_cran() testthat::skip_if_offline() @@ -91,7 +91,7 @@ test_that("get_match_lineups() works", { }) - +Sys.sleep(3) test_that("get_match_report() works", { testthat::skip_on_cran() testthat::skip_if_offline() @@ -99,7 +99,7 @@ test_that("get_match_report() works", { # test that multiple match_url can be passed to the function test_urls <- c("https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1", "https://fbref.com/en/matches/9cbccb37/Dijon-Angers-August-22-2020-Ligue-1") - test_df <- get_match_report(match_url = test_urls) + test_df <- get_match_report(match_url = test_urls, time_pause = 4) expect_type(test_df, "list") # test that the correct number of columns returned @@ -111,12 +111,12 @@ test_that("get_match_report() works", { }) - +Sys.sleep(3) test_that("get_match_results() works", { testthat::skip_on_cran() testthat::skip_if_offline() # test that multiple countries can be passed to the function - test_df <- get_match_results(country = c("ENG", "AUS"), gender = "F", season_end_year = 2021, tier="1st") + test_df <- get_match_results(country = c("ENG", "AUS"), gender = "F", season_end_year = 2021, tier="1st", time_pause = 4) expect_type(test_df, "list") # test that incorrect Country will error @@ -125,14 +125,14 @@ test_that("get_match_results() works", { }) - +Sys.sleep(3) test_that("get_match_summary() works", { testthat::skip_on_cran() testthat::skip_if_offline() # test that multiple match_url can be passed to the function test_urls <- c("https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1", "https://fbref.com/en/matches/9cbccb37/Dijon-Angers-August-22-2020-Ligue-1") - test_df <- get_match_summary(match_url = test_urls) + test_df <- get_match_summary(match_url = test_urls, time_pause = 4) expect_type(test_df, "list") # test that incorrect url will error @@ -142,18 +142,18 @@ test_that("get_match_summary() works", { }) - +Sys.sleep(3) test_that("get_match_url() works", { testthat::skip_on_cran() testthat::skip_if_offline() - test_urls <- get_match_urls(country = "AUS", gender = "F", season_end_year = 2020, tier="1st") + test_urls <- get_match_urls(country = "AUS", gender = "F", season_end_year = 2020, tier="1st", time_pause = 4) # test the functions returns the data expect_type(test_urls, "character") # also test if it's a correct url: expect_equal(grepl("https://", test_urls[1]), TRUE) # test that incorrect url will error - expect_equal(length(get_match_urls(country = "BBB", gender = "M", season_end_year = 2021, tier="1st")), 0) + expect_equal(length(get_match_urls(country = "BBB", gender = "M", season_end_year = 2021, tier="1st", time_pause = 4)), 0) # friendly_int_2021_urls <- get_match_urls(country = "", gender = "M", season_end_year = 2021, tier = "", non_dom_league_url = "https://fbref.com/en/comps/218/history/Friendlies-M-Seasons") # expect_type(friendly_int_2021_urls, "character") @@ -162,129 +162,137 @@ test_that("get_match_url() works", { - +Sys.sleep(3) test_that("get_season_team_stats() works", { testthat::skip_on_cran() testthat::skip_if_offline() # test the functions returns the data - expect_type(get_season_team_stats(country = "AUS", gender = "F", season_end_year = 2021, tier="1st", stat_type = "league_table_home_away"), "list") - expect_type(get_season_team_stats(country = "AUS", gender = "F", season_end_year = 2021, tier="1st", stat_type = "standard"), "list") - expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "keeper"), "list") - expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "keeper_adv"), "list") - expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "shooting"), "list") - expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "passing"), "list") - expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "passing_types"), "list") - expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "goal_shot_creation"), "list") - expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "defense"), "list") - expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "possession"), "list") - expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "playing_time"), "list") - expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "misc"), "list") + expect_type(get_season_team_stats(country = "AUS", gender = "F", season_end_year = 2021, tier="1st", stat_type = "league_table_home_away", time_pause = 4), "list") + expect_type(get_season_team_stats(country = "AUS", gender = "F", season_end_year = 2021, tier="1st", stat_type = "standard", time_pause = 4), "list") + expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "keeper", time_pause = 4), "list") + expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "keeper_adv", time_pause = 4), "list") + expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "shooting", time_pause = 4), "list") + expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "passing", time_pause = 4), "list") + expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "passing_types", time_pause = 4), "list") + expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "goal_shot_creation", time_pause = 4), "list") + expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "defense", time_pause = 4), "list") + expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "possession", time_pause = 4), "list") + expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "playing_time", time_pause = 4), "list") + expect_type(get_season_team_stats(country = "ENG", gender = "M", season_end_year = 2021, tier="1st", stat_type = "misc", time_pause = 4), "list") # test that multiple genders can be passed to the function - expect_type(get_season_team_stats(country = "AUS", gender = c("M", "F"), season_end_year = 2021, tier="1st", stat_type = "league_table"), "list") + expect_type(get_season_team_stats(country = "AUS", gender = c("M", "F"), season_end_year = 2021, tier="1st", stat_type = "league_table", time_pause = 4), "list") # test that incorrect URL will error - expect_equal(length(get_season_team_stats(country = "AUS", gender = "F", season_end_year = 2021, tier="1st", stat_type = "possession")), 0) + expect_equal(length(get_season_team_stats(country = "AUS", gender = "F", season_end_year = 2021, tier="1st", stat_type = "possession", time_pause = 4)), 0) # test that an invalid stat_type will error - expect_error(get_season_team_stats(country = "AUS", gender = "F", season_end_year = 2021, tier="1st", stat_type = "test")) + expect_error(get_season_team_stats(country = "AUS", gender = "F", season_end_year = 2021, tier="1st", stat_type = "test", time_pause = 4)) # test that MLS league table returns a new column for conference - mls_table <- get_season_team_stats(country = "USA", gender = "M", season_end_year = 2021, tier="1st", stat_type = "league_table") + mls_table <- get_season_team_stats(country = "USA", gender = "M", season_end_year = 2021, tier="1st", stat_type = "league_table", time_pause = 4) expect_type(mls_table, "list") expect_true(any(grepl("Conference", names(mls_table)))) }) +Sys.sleep(3) test_that("fb_big5_advanced_season_stats() works", { testthat::skip_on_cran() - big5_team_shooting_multiple <- fb_big5_advanced_season_stats(season_end_year= c(2021:2022), stat_type= "shooting", team_or_player= "team") + big5_team_shooting_multiple <- fb_big5_advanced_season_stats(season_end_year= c(2021:2022), stat_type= "shooting", team_or_player= "team", time_pause = 4) expect_type(big5_team_shooting_multiple, "list") expect_false(nrow(big5_team_shooting_multiple) == 0) - big5_player_shooting <- fb_big5_advanced_season_stats(season_end_year= c(2022), stat_type= "shooting", team_or_player= "player") + big5_player_shooting <- fb_big5_advanced_season_stats(season_end_year= c(2022), stat_type= "shooting", team_or_player= "player", time_pause = 4) expect_type(big5_player_shooting, "list") expect_false(nrow(big5_player_shooting) == 0) }) +Sys.sleep(3) test_that("get_match_shooting() works", { testthat::skip_on_cran() test_urls_multiple <- c("https://fbref.com/en/matches/c0996cac/Bordeaux-Nantes-August-21-2020-Ligue-1", "https://fbref.com/en/matches/9cbccb37/Dijon-Angers-August-22-2020-Ligue-1", "https://fbref.com/en/matches/f96cd5a0/Lorient-Strasbourg-August-23-2020-Ligue-1") - shot_multiple_matches <- get_match_shooting(test_urls_multiple) + shot_multiple_matches <- get_match_shooting(test_urls_multiple, time_pause = 4) expect_type(shot_multiple_matches, "list") expect_false(nrow(shot_multiple_matches) == 0) }) +Sys.sleep(3) test_that("get_team_match_results() works", { testthat::skip_on_cran() # get all team URLs for a league - epl_2021_team_urls <- fb_teams_urls("https://fbref.com/en/comps/9/Premier-League-Stats") - epl_2021_team_results <- get_team_match_results(team_url = epl_2021_team_urls[1:3]) + epl_2021_team_urls <- fb_teams_urls("https://fbref.com/en/comps/9/Premier-League-Stats", time_pause = 4) + epl_2021_team_results <- get_team_match_results(team_url = epl_2021_team_urls[1:3], time_pause = 4) expect_type(epl_2021_team_results, "list") expect_false(nrow(epl_2021_team_results) == 0) }) +Sys.sleep(3) test_that("fb_player_scouting_report() works", { testthat::skip_on_cran() scout <- fb_player_scouting_report(player_url = "https://fbref.com/en/players/d70ce98e/Lionel-Messi", - pos_versus = "primary") + pos_versus = "primary", time_pause = 4) expect_type(scout, "list") expect_false(nrow(scout) == 0) scout_secondary <- fb_player_scouting_report(player_url = "https://fbref.com/en/players/d70ce98e/Lionel-Messi", - pos_versus = "secondary") + pos_versus = "secondary", time_pause = 4) expect_type(scout, "list") expect_false(nrow(scout) == 0) }) +Sys.sleep(3) test_that("fb_player_season_stats() works", { testthat::skip_on_cran() multiple_playing_time <- fb_player_season_stats(player_url = c("https://fbref.com/en/players/d70ce98e/Lionel-Messi", "https://fbref.com/en/players/dea698d9/Cristiano-Ronaldo"), - stat_type = "playing_time") + stat_type = "playing_time", time_pause = 4) expect_type(multiple_playing_time, "list") expect_false(nrow(multiple_playing_time) == 0) }) +Sys.sleep(3) test_that("fb_player_match_logs() works", { testthat::skip_on_cran() - ederson_summary <- fb_player_match_logs("https://fbref.com/en/players/3bb7b8b4/Ederson", season_end_year = 2021, stat_type = 'summary') + 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) }) +Sys.sleep(3) test_that("fb_team_player_stats() works", { testthat::skip_on_cran() - fleetwood_standard_stats <- fb_team_player_stats(team_urls= "https://fbref.com/en/squads/d6a369a2/Fleetwood-Town-Stats", stat_type= 'standard') + fleetwood_standard_stats <- fb_team_player_stats(team_urls= "https://fbref.com/en/squads/d6a369a2/Fleetwood-Town-Stats", stat_type= 'standard', time_pause = 4) expect_type(fleetwood_standard_stats, "list") expect_false(nrow(fleetwood_standard_stats) == 0) # this should error because the stat type isn't available - expect_error(fb_team_player_stats(team_urls= "https://fbref.com/en/squads/d6a369a2/Fleetwood-Town-Stats", stat_type= 'keeper_adv')) + expect_error(fb_team_player_stats(team_urls= "https://fbref.com/en/squads/d6a369a2/Fleetwood-Town-Stats", stat_type= 'keeper_adv', time_pause = 4)) # this should error because the spelling of the stat type is incorrect - expect_error(fb_team_player_stats(team_urls= "https://fbref.com/en/squads/d6a369a2/Fleetwood-Town-Stats", stat_type= 'stangard')) + expect_error(fb_team_player_stats(team_urls= "https://fbref.com/en/squads/d6a369a2/Fleetwood-Town-Stats", stat_type= 'stangard', time_pause = 4)) }) +Sys.sleep(3) test_that("fb_team_match_log_stats() works", { testthat::skip_on_cran() - liv_passing_log <- fb_team_match_log_stats(team_url= "https://fbref.com/en/squads/822bd0ba/Liverpool-Stats", stat_type= 'passing') + liv_passing_log <- fb_team_match_log_stats(team_url= "https://fbref.com/en/squads/822bd0ba/Liverpool-Stats", stat_type= 'passing', time_pause = 4) expect_type(liv_passing_log, "list") expect_false(nrow(liv_passing_log) == 0) # this should error because the stat type isn't available - expect_error(fb_team_match_log_stats(team_url = "https://fbref.com/en/squads/d6a369a2/Fleetwood-Town-Stats", stat_type= 'keeper_adv')) + expect_error(fb_team_match_log_stats(team_url = "https://fbref.com/en/squads/d6a369a2/Fleetwood-Town-Stats", stat_type= 'keeper_adv', time_pause = 4)) # this should error because the spelling of the stat type is incorrect - expect_error(fb_team_player_stats(team_urls= "https://fbref.com/en/squads/d6a369a2/Fleetwood-Town-Stats", stat_type= 'shooooooting')) + expect_error(fb_team_player_stats(team_urls= "https://fbref.com/en/squads/d6a369a2/Fleetwood-Town-Stats", stat_type= 'shooooooting', time_pause = 4)) })