/
fg_team_fielder.R
182 lines (172 loc) · 6.34 KB
/
fg_team_fielder.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
#' @rdname fg_team_fielder
#' @title **Scrape Team Fielder Leaderboards from FanGraphs**
#' @param age (integer) Age of players
#' @param pos (character) Position of players, defaults to "all". To exclude pitchers, use "np".
#' @param stats (character) Statistic to return. Defaults to "bat".
#' @param lg (character) League to return. Defaults to "all". Options are "al", "nl", or "all".
#' @param qual (character) Whether you want only batters/pitchers that qualified in a given season, or the minimum number of plate appearances for inclusion. If you only want qualified hitters, use qual. If a minimum number of plate appearaces/innings pitched, use the number desired. Defaults to "y".
#' @param startseason (character) Season for which you want to scrape the data.
#' @param endseason (character) Last season for which you want data.
#' @param startdate (character) Start date for which you want data.
#' @param enddate (character) End date for which you want data.
#' @param month (character) Month for which you want data.
#' @param hand (character) Handedness of batter. Options are "L", "R", or "B". Empty string returns all.
#' @param team (character) Teams for which you want data, comma separated.
#' @param pageitems (character) Number of items per page.
#' @param pagenum (character) Page number.
#' @param ind (character) Whether or not to break the seasons out individual, or roll them up together. 1 = split seasons, 0 = aggregate seasons.
#' @param rost (character) Whether or not to include players on the roster. 1 = include, 0 = exclude.
#' @param players (character) Whether or not to include players on the roster. 1 = include only active roster players, 0 = exclude.
#' @param type (character) Defaults to 8, which is the standard leaderboard. The values for the leaderboards appear to go to from type = 0 to 48+, which correspond to links on the leaderboard page.
#' @param postseason (logical) Whether or not to include postseason data. TRUE = include postseason, FALSE = exclude postseason.
#' @param sortdir (character) Sort direction. Options are "asc" or "desc" or "default".
#' @param sortstat (character) Sort by stat. Default is "Defense".
#' @return A data frame of fielder data.
#'
#' |col_name |types |
#' |:-------------|:---------|
#' |Season |integer |
#' |team_name |character |
#' |SeasonMin |integer |
#' |SeasonMax |integer |
#' |Pos |character |
#' |G |integer |
#' |GS |integer |
#' |Inn |integer |
#' |PO |integer |
#' |A |integer |
#' |E |integer |
#' |FE |integer |
#' |TE |integer |
#' |DP |integer |
#' |DPS |integer |
#' |DPT |integer |
#' |DPF |integer |
#' |Scp |integer |
#' |SB |integer |
#' |CS |integer |
#' |PB |integer |
#' |WP |integer |
#' |FP |numeric |
#' |rSB |integer |
#' |rGDP |integer |
#' |rARM |integer |
#' |rGFP |integer |
#' |rPM |integer |
#' |rSZ |numeric |
#' |rTS |integer |
#' |rCERA |integer |
#' |DRS |integer |
#' |BIZ |integer |
#' |Plays |integer |
#' |RZR |numeric |
#' |OOZ |integer |
#' |ARM |numeric |
#' |DPR |numeric |
#' |RngR |numeric |
#' |ErrR |numeric |
#' |UZR |numeric |
#' |UZR_150 |numeric |
#' |Defense |numeric |
#' |CStrikes |numeric |
#' |CFraming |numeric |
#' |OAA |integer |
#' |rFRP |integer |
#' |Q |numeric |
#' |TInn |numeric |
#' |teamid |integer |
#' |team_name_abb |character |
#'
#' @import rvest
#' @export
#' @examples \donttest{
#' try(fg_team_fielder(startseason = 2023, endseason = 2023, qual = 150))
#' }
fg_team_fielder <- function(
age = "",
pos = "all",
stats = "fld",
lg = "all",
qual = "0",
startseason = "2023",
endseason = "2023",
startdate = "",
enddate = "",
month = "0",
hand = "",
team = "0,ts",
pageitems = "1000",
pagenum = "1",
ind = "0",
rost = "0",
players = "",
type = "1",
postseason = "",
sortdir = "default",
sortstat = "Defense") {
params <- list(
age = age,
pos = pos,
stats = stats,
lg = lg,
qual = qual,
season = startseason,
season1 = endseason,
startdate = startdate,
enddate = enddate,
month = month,
hand = hand,
team = team,
pageitems = pageitems,
pagenum = pagenum,
ind = ind,
rost = rost,
players = players,
type = type,
postseason = postseason,
sortdir = sortdir,
sortstat = sortstat
)
url <- "https://www.fangraphs.com/api/leaders/major-league/data"
fg_endpoint <- httr::modify_url(url, query = params)
tryCatch(
expr = {
resp <- fg_endpoint %>%
mlb_api_call()
fg_df <- resp$data %>%
jsonlite::toJSON() %>%
jsonlite::fromJSON(flatten=TRUE)
c <- colnames(fg_df)
c <- gsub("%", "_pct", c, fixed = TRUE)
c <- gsub("/", "_", c, fixed = TRUE)
c <- ifelse(substr(c, nchar(c) - 1 + 1, nchar(c)) == ".", gsub("\\.", "_pct", c), c)
c <- gsub(" ", "_", c, fixed = TRUE)
colnames(fg_df) <- c
leaders <- fg_df %>%
dplyr::rename_with(~ gsub("pi", "pi_", .x), starts_with("pi")) %>%
dplyr::rename_with(~ gsub("pfx", "pfx_", .x), starts_with("pfx")) %>%
dplyr::rename(
"team_name" = "TeamName",
"team_name_abb" = "TeamNameAbb") %>%
dplyr::select(-dplyr::any_of(c(
"Throws",
"xMLBAMID",
"Name",
"Team",
"PlayerNameRoute",
"PlayerName",
"playerid",
"Age",
"AgeRng"
))) %>%
dplyr::select("Season","team_name", tidyr::everything()) %>%
make_baseballr_data("MLB Team Fielding data from FanGraphs.com",Sys.time())
},
error = function(e) {
message(glue::glue("{Sys.time()}: Invalid arguments or no team fielding data available!"))
},
finally = {
}
)
return(leaders)
}