-
Notifications
You must be signed in to change notification settings - Fork 66
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
Load functions #130
Merged
Merged
Load functions #130
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e03189a
load functions for match results and advanced stats
JaseZiv bf96fdc
remove tier column from match results
JaseZiv f08c318
removed unnecessary test
JaseZiv 597e913
add error handling
JaseZiv c35ddbc
Apply suggestions from code review
JaseZiv 6fea819
fix param warning in fotmob_get_season_stats and update tests
JaseZiv 6fe808c
scrape timestamp now read from df attribute
JaseZiv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Type: Package | ||
Package: worldfootballR | ||
Title: Functions to Extract and Clean World Football (Soccer) Data | ||
Version: 0.5.2.3000 | ||
Version: 0.5.3 | ||
Authors@R: c( | ||
person("Jason", "Zivkovic", , "[email protected]", role = c("aut", "cre", "cph")), | ||
person("Tony", "ElHabr", , "[email protected]", role = "ctb") | ||
|
@@ -40,7 +40,8 @@ Imports: | |
utils, | ||
withr, | ||
xml2, | ||
tibble | ||
tibble, | ||
cli | ||
Suggests: | ||
knitr, | ||
rmarkdown, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#' Read in stored RDS data | ||
#' | ||
#' Reads in RDS stored data files typically stored on GitHub | ||
#' | ||
#' @param file_url URL to RDS file(s) for reading in | ||
#' | ||
#' @return data type dependent on RDS file being read in | ||
#' | ||
#' | ||
|
||
.file_reader <- function(file_url) { | ||
tryCatch(readRDS(url(file_url)), error = function(e) data.frame()) %>% | ||
suppressWarnings() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#' Load match results | ||
#' | ||
#' Loading version of \code{get_match_results} | ||
#' Returns the game results for a given league season(s) from FBref | ||
#' | ||
#' @param country the three character country code | ||
#' @param gender gender of competition, either "M" or "F" | ||
#' @param season_end_year the year(s) the season concludes | ||
#' @param tier the tier of the league, ie '1st' for the EPL or '2nd' for the Championship and so on | ||
#' | ||
#' @return returns a dataframe with the results of the competition, season and gender | ||
#' | ||
#' @importFrom magrittr %>% | ||
#' @importFrom rlang .data | ||
#' @importFrom utils read.csv | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \donttest{ | ||
#' df <- load_match_results( | ||
#' country = c("ITA"), gender = "M", season_end_year = 2021, tier = "1st" | ||
#' ) | ||
#' # for results from English 1st div for men and women: | ||
#' df <- load_match_results( | ||
#' country = "ENG", gender = c("M", "F"), season_end_year = 2021, tier = "1st" | ||
#' ) | ||
#' | ||
#' } | ||
|
||
load_match_results <- function(country, gender, season_end_year, tier) { | ||
dat_urls <- paste0("https://github.com/JaseZiv/worldfootballR_data/blob/master/data/match_results/", country, "_match_results.rds?raw=true") | ||
|
||
# collect_date <- .file_reader("https://github.com/JaseZiv/worldfootballR_data/blob/master/data/match_results/scrape_time_match_results.rds?raw=true") | ||
dat_df <- dat_urls %>% purrr::map_df(.file_reader) | ||
|
||
if(nrow(dat_df) == 0) { | ||
cli::cli_alert("Data not loaded. Please check parameters") | ||
} else { | ||
dat_df <- dat_df %>% | ||
dplyr::filter(.data$Country %in% country, | ||
.data$Gender %in% gender, | ||
.data$Season_End_Year %in% season_end_year, | ||
.data$Tier %in% tier) %>% | ||
dplyr::select(-.data$Tier) | ||
|
||
cli::cli_alert("Data last updated {attr(dat_df, 'scrape_timestamp')} UTC") | ||
} | ||
|
||
return(dat_df) | ||
|
||
} | ||
|
||
|
||
|
||
#' Load Big 5 Euro League Season Stats | ||
#' | ||
#' Loading version of \code{fb_big5_advanced_season_stats} | ||
#' Returns data frame of selected statistics for seasons of the big 5 Euro leagues, for either | ||
#' whole team or individual players. | ||
#' Multiple seasons can be passed to the function, but only one `stat_type` can be selected | ||
#' | ||
#' @param season_end_year the year(s) the season concludes | ||
#' @param stat_type the type of team statistics the user requires | ||
#' @param team_or_player result either summarised for each team, or individual players | ||
#' | ||
#' The statistic type options (stat_type) include: | ||
#' | ||
#' \emph{"standard"}, \emph{"shooting"}, \emph{"passing"}, \emph{"passing_types"}, | ||
#' \emph{"gca"}, \emph{"defense"}, \emph{"possession"}, \emph{"playing_time"}, | ||
#' \emph{"misc"}, \emph{"keepers"}, \emph{"keepers_adv"} | ||
#' | ||
#' @return returns a dataframe of a selected team or player statistic type for a selected season(s) | ||
#' | ||
#' @importFrom magrittr %>% | ||
#' @importFrom rlang .data | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' | ||
#' df <- load_fb_big5_advanced_season_stats( | ||
#' season_end_year = c(2018:2022), stat_type = "defense", team_or_player = "player" | ||
#' ) | ||
#' | ||
#' df <- load_fb_big5_advanced_season_stats( | ||
#' season_end_year = 2022, stat_type = "defense", team_or_player = "player" | ||
#' ) | ||
#' } | ||
|
||
load_fb_big5_advanced_season_stats <- function(season_end_year = NA, stat_type, team_or_player) { | ||
|
||
dat_url <- paste0("https://github.com/JaseZiv/worldfootballR_data/blob/master/data/fb_big5_advanced_season_stats/big5_", team_or_player, "_", stat_type, ".rds?raw=true") | ||
# collect_date <- .file_reader("https://github.com/JaseZiv/worldfootballR_data/blob/master/data/fb_big5_advanced_season_stats/scrape_time_big5_advanced_season_stats.rds?raw=true") | ||
|
||
dat_df <- tryCatch(.file_reader(dat_url), error = function(e) data.frame()) | ||
|
||
if(nrow(dat_df) == 0) { | ||
cli::cli_alert("Data not available. Check you have the correct stat_type or team_or_player") | ||
} else { | ||
dat_df <- dat_df %>% | ||
dplyr::filter(.data$Season_End_Year %in% season_end_year) | ||
|
||
cli::cli_alert("Data last updated {attr(dat_df, 'scrape_timestamp')} UTC") | ||
} | ||
|
||
return(dat_df) | ||
|
||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!