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

fg_pitcher_leaders #343

Open
OpeningDayBestDay opened this issue Jun 25, 2024 · 5 comments
Open

fg_pitcher_leaders #343

OpeningDayBestDay opened this issue Jun 25, 2024 · 5 comments

Comments

@OpeningDayBestDay
Copy link

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

@OpeningDayBestDay
Copy link
Author

fg_pitcher_leaders ERROR in example on page

@kyledpark
Copy link

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

@OpeningDayBestDay
Copy link
Author

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?

@jacobakaye
Copy link

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

@supernole1
Copy link

supernole1 commented Jul 10, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants