BillPetti/baseballr

fg_pitcher_leaders

Opened this issue · 5 comments

fg_pitcher_leaders is not working. Even the example shown has the same error I am getting. Here is the error:

test<-try(fg_pitcher_leaders(startseason = 2023, endseason = 2023))
2024-06-25 06:31:56.08341: Invalid arguments or no player pitching leaders data available!
Error in fg_pitcher_leaders(startseason = 2023, endseason = 2023) :
object 'leaders' not found

I'm getting the same error, I determined the issue was season needs to be capitalized on line 537

I'm getting the same error, I determined the issue was season needs to be capitalized on line 537

I tried capitalizing "Season", but still was not able to get data. Were you able to get it to run with your fix? If so, would you mind sharing?

Im getting the same issue

> pitching_leaders <- (fg_pitcher_leaders(qual = "y", startseason = 2024, endseason = 2024))
2024-07-07 16:33:18.348801: Invalid arguments or no player pitching leaders data available!
Error in fg_pitcher_leaders(qual = "y", startseason = 2024, endseason = 2024) : 
  object 'leaders' not found

fg_pitcher_leaders_dam - function.txt
I was having the same problem so i edited the function and found that something weird is going on in the mlb_api_call() function. I wasnt able to access that function so I modified the function fg_pitcher_leaders and wrote my own to get around the issue. This worked for me maybe ya'll can use it too...
fg_pitcher_leaders_dam <- function (age = "", pos = "all", stats = "pit", lg = "all",
qual = "0", startseason = "2024", endseason = "2024", startdate = "",
enddate = "", month = "0", hand = "", team = "0", pageitems = "10000",
pagenum = "1", ind = "0", rost = "0", players = "", type = "8",
postseason = "", sortdir = "default", sortstat = "WAR")
{
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 %>% httr::GET()
fg_df <- resp$data %>% jsonlite::toJSON() %>% jsonlite::fromJSON(flatten = TRUE)
fg_df <- resp %>%
httr::content(as = "text") %>%
jsonlite::fromJSON(flatten = TRUE) %>%
as.data.frame()
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
colnames(fg_df) <- gsub("^data\.", "", colnames(fg_df))
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(Start_IP = "Start.IP",Relief_IP = "Relief.IP", WPA_minus = ".WPA", WPA_plus = ".WPA.1",
AgeRng = "AgeR", team_name = "TeamName", team_name_abb = "TeamNameAbb") %>%
dplyr::select(-dplyr::any_of(c("Name", "Team"))) %>%
dplyr::select("Season", "team_name", "Throws", "xMLBAMID",
"PlayerNameRoute", "PlayerName", "playerid",
"Age", "AgeRng", tidyr::everything())
}, error = function(e) {
message(glue::glue("{Sys.time()}: Invalid arguments or no player pitching leaders data available!"))
}, finally = {
})
return(leaders)
}

this is how I'm using it:
fg_pitcher_leaders_dam(startseason = "2024", endseason = "2024") %>%
select(PlayerName,xMLBAMID,playerid,TBF,contains("Barrel"),HR.9,HR.FB,team_name_abb,teamid)