Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit135f179

Browse files
authored
Merge pull request#282 from DanielTomaro13/player-parameter
Player parameter
2 parentsc84cbcd +c5265ec commit135f179

File tree

5 files changed

+190
-51
lines changed

5 files changed

+190
-51
lines changed

‎R/fetch-player-details.R‎

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010
#' [fetch_player_details_afl()], [fetch_player_details_afltables()] and [fetch_player_details_footywire()]
1111
#' can be called directly and return data from the AFL website, AFL Tables and Footywire respectively.
1212
#'
13-
#' The function will typically be used to return the current team lists. For historical data, you can use the `current` argument set to FALSE. This will return all historical data for AFL.com and Footywire data. AFLTables data will always return historical data.
13+
#' The function will typically be used to return the current team lists. For historical data, you can use
14+
#' `current = FALSE`. This will return all historical data for AFL.com and Footywire data.
15+
#' AFLTables data will always return historical data.
1416
#'
1517
#' @param team team the player played for in the season for, defaults to NULL which returns all teams
18+
#' @param season Season in YYYY format
1619
#' @param current logical, return the current team list for the current calendar year or all historical data
1720
#' @param comp One of "AFLM" (default) or "AFLW"
1821
#' @param source One of "AFL" (default), "footywire", "afltables"
22+
#' @param player Character vector (optional). Filter by player name (exact/regex/fuzzy via `match`).
23+
#' @param player_id Character or numeric vector (optional). Filter by player ID (if an ID column is present).
24+
#' @param match One of "exact", "regex", or "fuzzy". Controls how `player` is matched. Default "exact".
1925
#' @param ... Optional parameters passed onto various functions depending on source.
2026
#'
2127
#' @return A Tibble with the details of the relevant players.
@@ -26,53 +32,65 @@
2632
#' # Return data for current Hawthorn players
2733
#' fetch_player_details("Hawthorn")
2834
#' fetch_player_details("Adelaide", current = FALSE, comp = "AFLW")
29-
#' fetch_player_details("GWS", current = TRUE,csource = "footywire")
35+
#' fetch_player_details("GWS", current = TRUE,source = "footywire")
3036
#' }
3137
#'
3238
#' @family fetch player details functions
3339
#' @seealso
3440
#' * [fetch_player_details_afl] for AFL.com data.
3541
#' * [fetch_player_details_footywire] for Footywire data.
36-
#' * [fetch_player_details_footywire] for AFL Tables data.
42+
#' * [fetch_player_details_afltables] for AFL Tables data.
3743
fetch_player_details<-function(team=NULL,
3844
season=NULL,
3945
current=TRUE,
4046
comp="AFLM",
4147
source="AFL",
48+
player=NULL,
49+
player_id=NULL,
50+
match= c("exact","regex","fuzzy"),
4251
...) {
43-
#Do some datachecks
52+
# checks
4453
check_comp_source(comp,source)
45-
46-
# Ignore certain parameters based on source
54+
match<- match.arg(match)
55+
56+
# messaging
4757
if (source=="afltables") {
4858
cli::cli_inform("For the afltables source, details are returned for all seasons. Ignoring `current` argument")
4959
}elseif (current) {
5060
cli::cli_inform("Returning player details for current season from source `{source}`")
51-
}elseif (!current){
61+
}else {
5262
cli::cli_inform("Returning historical player details from source `{source}`")
5363
}
54-
55-
if(source=="AFL"& is.null(season)) {
56-
dat<- fetch_player_details_afl(team=team,
57-
season=season,
58-
comp=comp,
59-
current=current)
64+
65+
# fetch
66+
if (source=="AFL"&& is.null(season)) {
67+
dat<- fetch_player_details_afl(team=team,season=season,comp=comp,current=current)
6068
}elseif (source=="AFL") {
61-
dat<-purrr::map_dfr(season,
62-
~ fetch_player_details_afl(team=team,
63-
season=.x,
64-
current=current,
65-
comp=comp))
69+
dat<-purrr::map_dfr(
70+
season,
71+
~ fetch_player_details_afl(team=team,season=.x,current=current,comp=comp)
72+
)
6673
}elseif (source=="afltables") {
67-
dat<- fetch_player_details_afltables(team=team)
74+
dat<- fetch_player_details_afltables(team=team)
6875
}elseif (source=="footywire") {
6976
dat<- fetch_player_details_footywire(team=team,current=current)
7077
}else {
7178
dat<-NULL
7279
}
73-
74-
if (is.null(dat))cli::cli_warn("The source\"{source}\" does not have Player Details data. Please use one of\"afltables\" and\"footywire\"")
75-
return(dat)
80+
81+
if (is.null(dat)) {
82+
cli::cli_warn('The source "{source}" does not have Player Details data. Please use one of "afltables" and "footywire"')
83+
return(dat)
84+
}
85+
86+
# optional filtering by player / player_id
87+
out<- .filter_players(dat,player=player,player_id=player_id,match=match)
88+
89+
if ((length(player)|| length(player_id))&& nrow(out)==0) {
90+
cli::cli_inform('No rows matched the supplied player/player_id filter for source "{source}".')
91+
}
92+
93+
out
7694
}
7795

7896
#' @param season Season in YYYY format

‎R/fetch-player-stats.R‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,40 @@
3939
#' * [fetch_player_stats_footywire] for Footywire data.
4040
#' * [fetch_player_stats_afltables] for AFL Tables data.
4141
#' * [fetch_player_stats_fryzigg] for Fryzigg data.
42+
43+
#' @param player Character vector. Filter by player name (exact/regex/fuzzy via `match`).
44+
#' @param player_id Character or numeric vector. Filter by player ID (if an ID column is present).
45+
#' @param match One of "exact", "regex", or "fuzzy". Controls how `player` is matched. Default "exact".
4246
fetch_player_stats<-function(season=NULL,
4347
round_number=NULL,
4448
comp="AFLM",
4549
source="AFL",
50+
player=NULL,
51+
player_id=NULL,
52+
match= c("exact","regex","fuzzy"),
4653
...) {
47-
# Do some data checks
4854
# season <- check_season(season)
4955
check_comp_source(comp,source)
50-
56+
match<- match.arg(match)
57+
5158
dat<-switch(source,
52-
"AFL"= fetch_player_stats_afl(season,round_number,comp),
53-
"footywire"= fetch_player_stats_footywire(season,round_number,...),
54-
"afltables"= fetch_player_stats_afltables(season,round_number),
55-
"fryzigg"= fetch_player_stats_fryzigg(season,round_number,comp),
56-
NULL
59+
"AFL"= fetch_player_stats_afl(season,round_number,comp),
60+
"footywire"= fetch_player_stats_footywire(season,round_number,...),
61+
"afltables"= fetch_player_stats_afltables(season,round_number),
62+
"fryzigg"= fetch_player_stats_fryzigg(season,round_number,comp),
63+
NULL
5764
)
58-
65+
5966
if (is.null(dat)) {
60-
cli::cli_warn("The source\"{source}\" does not have Player Stats.
61-
Please use one of\"AFL\"\"footywire\",\"afltables\" or\"fryzigg\"")
67+
cli::cli_warn('The source "{source}" does not have Player Stats.
68+
Please use one of "AFL" "footywire", "afltables" or "fryzigg"')
69+
return(dat)
6270
}
63-
return(dat)
71+
72+
.filter_players(dat,player=player,player_id=player_id,match=match)
6473
}
6574

75+
6676
#' @rdname fetch_player_stats
6777
#' @export
6878
fetch_player_stats_afl<-function(season=NULL,round_number=NULL,comp="AFLM") {

‎R/helpers-general.R‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,5 +287,113 @@ parse_team_abbr <- function(team_name) {
287287
)
288288
}
289289

290+
# ---- Player filtering helpers (internal)
291+
292+
# find a single name column if it exists; otherwise return NULL
293+
.find_player_name_col<-function(df) {
294+
nm<- tolower(names(df))
295+
prefs<- c("player","name","player_name","playername","display_name","displayname","full_name","fullname")
296+
idx<- which(nm%in%prefs)
297+
if (length(idx))return(names(df)[idx[1]])
298+
NULL# no fallback to first column
299+
}
300+
301+
302+
# return likely player-id columns across sources/schemas
303+
.find_player_id_cols<-function(df) {
304+
nm<- tolower(names(df))
305+
wanted<- c(
306+
"id","player_id","playerid","athleteid","athlete_id"
307+
)
308+
cols<- names(df)[nm%in%wanted]
309+
# also accept dotted/underscored variants like player.id, player.id., player_id
310+
cols<- union(cols, names(df)[grepl("(^|[._])player(_|\\.)?id$",nm)])
311+
unique(cols)
312+
}
313+
314+
# name matching modes: exact / regex / fuzzy (agrep)
315+
.match_names<-function(x,q,mode= c("exact","regex","fuzzy")) {
316+
mode<- match.arg(mode)
317+
x2<- trimws(tolower(as.character(x)))
318+
q<- trimws(tolower(as.character(q)))
319+
if (length(q)==0L)return(rep(TRUE, length(x2)))
320+
switch(
321+
mode,
322+
exact=x2%in%q,
323+
regex= Reduce(`|`, lapply(q,function(p) grepl(p,x2,ignore.case=TRUE))),
324+
fuzzy= Reduce(`|`, lapply(q,function(p) {
325+
idx<- agrep(p,x2,max.distance=0.1,ignore.case=TRUE)
326+
out<- rep(FALSE, length(x2));if (length(idx))out[idx]<-TRUE;out
327+
}))
328+
)
329+
}
330+
331+
# unified post-filter to apply on any fetched player-stats/details df
332+
.filter_players<-function(df,player=NULL,player_id=NULL,match= c("exact","regex","fuzzy")) {
333+
if (is.null(df)||!is.data.frame(df)||!nrow(df))return(df)
334+
match<- match.arg(match)
335+
336+
keep<- rep(TRUE, nrow(df))
337+
338+
# filter by ID if requested
339+
if (!is.null(player_id)) {
340+
id_cols<- .find_player_id_cols(df)
341+
if (length(id_cols)) {
342+
ids<- unique(as.character(player_id))
343+
id_hit<- Reduce(`|`, lapply(id_cols,function(col) as.character(df[[col]])%in%ids))
344+
keep<-keep&id_hit
345+
}else {
346+
keep<- rep(FALSE, nrow(df))
347+
}
348+
}
349+
350+
# filter by name if requested
351+
if (!is.null(player)) {
352+
name_col<- .find_player_name_col(df)
353+
354+
if (is.null(name_col)) {
355+
# compose FIRST + SURNAME when no single name column exists
356+
nm_raw<- names(df)
357+
nm<- tolower(nm_raw)
358+
nm_norm<- gsub("[^a-z]","",nm)
359+
360+
first_aliases<- c("firstname","givenname","givennames","forename","first")
361+
last_aliases<- c("surname","lastname","familyname","last")
362+
363+
first_idx<- match(first_aliases,nm_norm);first_idx<-first_idx[!is.na(first_idx)]
364+
last_idx<- match(last_aliases,nm_norm);last_idx<-last_idx[!is.na(last_idx)]
365+
366+
if (length(first_idx)&& length(last_idx)) {
367+
first_col<-nm_raw[first_idx[1]]
368+
last_col<-nm_raw[last_idx[1]]
369+
name_vec<- paste(df[[first_col]],df[[last_col]])
370+
}else {
371+
372+
bad_like<- c("no","games","age","height","weight","position","position1","position2",
373+
"dateofbirth","dob","origin","guernsey","jumper")
374+
cand<- which(!(nm%in%bad_like))
375+
if (length(cand)) {
376+
text_mask<- vapply(df[cand],function(x) is.character(x)|| is.factor(x),TRUE)
377+
if (any(text_mask)) {
378+
name_vec<- as.character(df[[nm_raw[cand[which(text_mask)[1]]]]])
379+
}else {
380+
name_vec<- rep(NA_character_, nrow(df))
381+
}
382+
}else {
383+
name_vec<- rep(NA_character_, nrow(df))
384+
}
385+
}
386+
}else {
387+
name_vec<-df[[name_col]]
388+
}
389+
390+
name_hit<- .match_names(name_vec,player,mode=match)
391+
keep<-keep&name_hit
392+
}
393+
394+
df[keep, ,drop=FALSE]
395+
}
396+
397+
290398
# silence global variable NOTES
291399
utils::globalVariables(names= c("x","y","round_mapping"))

‎man/fetch_player_details.Rd‎

Lines changed: 12 additions & 18 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎man/fetch_player_stats.Rd‎

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp