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

Load functions #130

Merged
merged 7 commits into from
Jun 3, 2022
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
5 changes: 3 additions & 2 deletions DESCRIPTION
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")
Expand Down Expand Up @@ -40,7 +40,8 @@ Imports:
utils,
withr,
xml2,
tibble
tibble,
cli
Suggests:
knitr,
rmarkdown,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export(get_match_urls)
export(get_player_market_values)
export(get_season_team_stats)
export(get_team_match_results)
export(load_fb_big5_advanced_season_stats)
export(load_match_results)
export(player_dictionary_mapping)
export(player_transfer_history)
export(tm_expiring_contracts)
Expand Down
11 changes: 11 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# worldfootballR 0.5.3

### Improvements

Now starting to have load functions:

* `load_match_results()` - function to load pre stored data similar to `get_match_results()`
* `load_fb_big5_advanced_season_stats()` - function to load pre stored data similar to `fb_big5_advanced_season_stats()`

***

# worldfootballR 0.5.2.3000

### Bugs
Expand Down
20 changes: 4 additions & 16 deletions R/fotmob_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@
#' @param team_or_player return statistics for either \code{"team"} or \code{"player"}. Can only be one or the other.
#' @param stat_name the type of statistic. Can be more than one.
#' `stat_name` must be one of the following for \code{"player"}:
#' \item{Accurate long balls per 90}
#'
#' \itemize{
#' #' \item{Accurate long balls per 90}
#' \item{Accurate passes per 90}
#' \item{Assists}
#' \item{Big chances created}
Expand Down Expand Up @@ -126,7 +128,7 @@
#' \item{xG + xA per 90}
#' \item{Yellow cards}
#' }
#'
#'
#' For \code{"team"}, `stat_name` must be one of the following:
#' \itemize{
#' \item{Accurate crosses per match}
Expand Down Expand Up @@ -175,20 +177,6 @@
#' team_or_player = "team"
#' )
#'
#' ## fotmob doesn't has data for 2016/2017 for some leagues, but not all.
#' ## you'll see a warning about this and receive an empty dataframe
#' get_epl_season_stats(
#' season = "2016/2017"
#' )
#'
#' ## Note that the `stat_type` name is slightly different.
#' epl_player_xg_2021 <- get_epl_season_stats(
#' country = "ENG",
#' league_name = "Premier League",
#' season = "2020/2021",
#' stat_type = "Expected goals (xG)",
#' team_or_player = "player"
#' )
#' }
fotmob_get_season_stats <- function(
country,
Expand Down
14 changes: 14 additions & 0 deletions R/internals_loading.R
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()
}
111 changes: 111 additions & 0 deletions R/load_fb.R
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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

}

return(dat_df)

}

17 changes: 17 additions & 0 deletions man/dot-file_reader.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 37 additions & 49 deletions man/fotmob_get_season_stats.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions man/load_fb_big5_advanced_season_stats.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading