This repository has been archived by the owner on Apr 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
player_functions.R
218 lines (195 loc) · 8.81 KB
/
player_functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# imports
#' @import jsonlite
#' @import dplyr
#' @import getPass
#' @importFrom plyr rbind.fill
## quiets concerns of R CMD check re: the .'s that appear in pipelines
if(getRversion() >= "2.15.1") utils::globalVariables(c("."))
# get_player_id
#' get_player_id
#'
#' This function fetches the FPL ID of a player given the player's full name. If blank, the function fetches all current players.
#' @param name The player's full name, as listed on the official Fantasy Premier League site (for instance: "Richarlison de Andrade", not "Richarlison". You can list multiple, e.g. with c().
#' @keywords player
#' @export
#' @examples
#' get_player_id(name="Virgil van Dijk")
get_player_id <- function(name = NULL){
ifelse(
is.null(name),
{elements <- jsonlite::fromJSON("https://fantasy.premierleague.com/api/bootstrap-static/")$elements
elements$playername <- paste(elements$first_name,elements$second_name)
return(elements %>% dplyr::select(playername,id))}
,
{
elements <- jsonlite::fromJSON("https://fantasy.premierleague.com/api/bootstrap-static/")$elements
elements$playername <- paste(elements$first_name,elements$second_name)
return(elements %>% dplyr::filter(playername %in% name) %>% dplyr::select(playername,id))
}
)
}
# get_player_ids
#' get_player_ids
#'
#' This function fetches ids for all players.
#' @param season To retrieve prior season data, enter a 2-digit year corresponding to the start of the historical FPL season of interest (e.g. '17' for the 2017/2018 season).
#' @keywords player
#' @export
#' @examples
#' get_player_ids()
get_player_ids <- function(season = NULL){
ifelse(
is.null(season),
{
elements <- jsonlite::fromJSON("https://fantasy.premierleague.com/api/bootstrap-static/")$elements
elements$playername <- paste(elements$first_name,elements$second_name)
},
{
elements <- read.csv(paste0("https://raw.githubusercontent.com/wiscostret/histfpldata/master/getplayerinfo",season,".csv"),encoding="UTF-8")
})
return(elements %>% dplyr::select(id,playername))
}
# get_player_name
#' get_player_name
#'
#' This function fetches a player's full name given FPL ID. If blank, the function fetches all current players.
#' @param playerid The player's FPL ID. You can list multiple, e.g. with c().
#' @keywords player
#' @export
#' @examples
#' get_player_name(playerid=300)
#'
#' get_player_name(playerid=150:160)
get_player_name <- function(playerid = NULL){
ifelse(
is.null(playerid),
{
elements <- jsonlite::fromJSON("https://fantasy.premierleague.com/api/bootstrap-static/")$elements
elements$playername <- paste(elements$first_name,elements$second_name)
return(elements %>% dplyr::select(playername,id))
}
,
{
elements <- jsonlite::fromJSON("https://fantasy.premierleague.com/api/bootstrap-static/")$elements
elements$playername <- paste(elements$first_name,elements$second_name)
return(elements %>% dplyr::filter(id %in% playerid) %>% dplyr::select(playername,id))
}
)
}
# get_player_info
#' get_player_info
#'
#' This function fetches selected summary information on selected players given full player name(s).
#' @param name The player's full name, as listed on the official Fantasy Premier League site (for instance: "Richarlison de Andrade", not "Richarlison"). If blank, the function fetches all players.
#' @param season To retrieve prior season data, enter a 2-digit year corresponding to the start of the historical FPL season of interest (e.g. '17' for the 2017/2018 season).
#' @keywords player
#' @export
#' @examples
#' get_player_info(name=c("Jesse Lingard","Virgil van Dijk"))
get_player_info <- function(name = NULL, season = NULL){
ifelse(
is.null(season),
{
elements <- jsonlite::fromJSON("https://fantasy.premierleague.com/api/bootstrap-static/")$elements
elements$playername <- paste(elements$first_name,elements$second_name)
},
{
elements <- read.csv(paste0("https://raw.githubusercontent.com/wiscostret/histfpldata/master/getplayerinfo",season,".csv"),encoding="UTF-8")
})
ifelse(
is.null(name),
return(elements),
return(elements %>% dplyr::filter(playername %in% name))
)
}
# get_player_hist
#' get_player_hist
#'
#' This function fetches historical summary information on selected players given player ID(s).
#' @param playerid The player's ID. Can be found using get_player_id(). You can list multiple , e.g. with c(). If blank, the function fetches all current players - note that takes a while to load.
#' @keywords player
#' @export
#' @examples
#' get_player_hist(playerid=200)
#'
#' get_player_hist(playerid=get_player_id("Sead Kolasinac")$id)
get_player_hist <- function(playerid = NULL){
elements <- jsonlite::fromJSON("https://fantasy.premierleague.com/api/bootstrap-static/")$elements
elements$playername <- paste(elements$first_name,elements$second_name)
ifelse(
is.null(playerid),
{
histinfo <- data.frame()
for (i in 1:nrow(elements)){
fplboot <- jsonlite::fromJSON(url(paste("https://fantasy.premierleague.com/api/element-summary/",elements$id[i],"/",sep="")))$history_past
histinfo <- rbind(histinfo,data.frame(fplboot,playername=rep(elements$playername[which(elements$id==elements$id[i])],length(unique(fplboot$season_name)))))}
return(histinfo)
},
{
histinfo <- data.frame()
for (i in 1:length(playerid)){
fplboot <- jsonlite::fromJSON(url(paste("https://fantasy.premierleague.com/api/element-summary/",playerid[i],"/",sep="")))$history_past
histinfo <- rbind(histinfo,data.frame(fplboot,playername=rep(elements$playername[which(elements$id==playerid[i])],length(unique(fplboot$season_name)))))}
return(histinfo)})
}
# get_player_details
#' get_player_details
#'
#' This function fetches detailed, gameweek-by-gameweek information for the current season on selected players. If parameters left blank, the function fetches all current players - note that takes a while to load.
#' @param playerid The player's ID. Can be found using get_player_id(). You can list multiple, e.g. with c(). Won't work with prior season's data.
#' @param name Alternatively, the player's full name, as listed on the official Fantasy Premier League site (for instance: "Richarlison de Andrade", not "Richarlison"). If blank, the function fetches all players.
#' @param season To retrieve prior season data, enter a 2-digit year corresponding to the start of the historical FPL season of interest (e.g. '17' for the 2017/2018 season).
#' @keywords player
#' @export
#' @examples
#' get_player_details(300)
#'
#' get_player_details(name="Virgil van Dijk")
get_player_details <- function(playerid = NULL, name = NULL, season = NULL){
if(!is.null(playerid) & !is.null(name)) stop("Please only supply playerid OR name, not both.")
ifelse(
!is.null(season),
{
detinfo <- read.csv(paste0("https://raw.githubusercontent.com/wiscostret/histfpldata/master/getplayerdetails",season,".csv"),encoding="UTF-8")
ifelse(
is.null(playerid),
ifelse(
is.null(name),
return(detinfo),
return(detinfo %>% dplyr::filter(playername %in% name))),
return(detinfo %>% dplyr::filter(playerid %in% id))
)
},
{
elements <- jsonlite::fromJSON("https://fantasy.premierleague.com/api/bootstrap-static/")$elements
elements$playername <- paste(elements$first_name,elements$second_name)
ifelse(
is.null(playerid),
ifelse(
is.null(name),
{
detinfo <- data.frame()
for (i in 1:nrow(elements)){
fplboot <- jsonlite::fromJSON(url(paste("https://fantasy.premierleague.com/api/element-summary/",elements$id[i],"/",sep="")))$history
detinfo <- plyr::rbind.fill(detinfo,data.frame(cbind(fplboot,playername=elements$playername[which(elements$id==elements$id[i])])))}
return(detinfo)
},
{
detinfo <- data.frame()
selection <- elements %>% dplyr::filter(playername %in% name) %>% dplyr::select(id) %>% unlist() %>% as.numeric()
for (i in 1:length(selection)){
fplboot <- jsonlite::fromJSON(url(paste("https://fantasy.premierleague.com/api/element-summary/",selection[i],"/",sep="")))$history
detinfo <- plyr::rbind.fill(detinfo,data.frame(cbind(fplboot,playername=elements$playername[which(elements$id==selection[i])])))}
return(detinfo)
}),
{
detinfo <- data.frame()
for (i in 1:length(playerid)){
fplboot <- jsonlite::fromJSON(url(paste("https://fantasy.premierleague.com/api/element-summary/",playerid[i],"/",sep="")))$history
detinfo <- plyr::rbind.fill(detinfo,data.frame(cbind(fplboot,playername=elements$playername[which(elements$id==playerid[i])])))}
return(detinfo)
}
)
}
)
}