Skip to content

Commit

Permalink
Merge pull request #17 from saiemgilani/standings
Browse files Browse the repository at this point in the history
standings
  • Loading branch information
saiemgilani committed May 22, 2021
2 parents 4ddbfaf + 27bfe8a commit c17ffd9
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
^\.github$
^inst*
^vignette*
nba_pbp_db
mbb_pbp_db
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ export(espn_mbb_pbp)
export(espn_mbb_player_box)
export(espn_mbb_rankings)
export(espn_mbb_scoreboard)
export(espn_mbb_standings)
export(espn_mbb_team_box)
export(espn_mbb_teams)
export(espn_nba_game_all)
export(espn_nba_pbp)
export(espn_nba_player_box)
export(espn_nba_scoreboard)
export(espn_nba_standings)
export(espn_nba_team_box)
export(espn_nba_teams)
export(has_kp_user_and_pw)
Expand Down Expand Up @@ -62,6 +64,7 @@ import(rvest)
import(stringr)
import(utils)
importFrom(assertthat,assert_that)
importFrom(data.table,rbindlist)
importFrom(data.table,setDT)
importFrom(dplyr,"%>%")
importFrom(dplyr,any_of)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# **hoopR 1.0.5**
### **Standings functions**
- [```hoopR::espn_nba_standings()```](https://saiemgilani.github.io/hoopR/reference/espn_nba_standings.html)
- [```hoopR::espn_mbb_standings()```](https://saiemgilani.github.io/hoopR/reference/espn_mbb_standings.html)

# **hoopR 1.0.4**
### **Add retry**
- Adding [```httr::retry()```](https://httr.r-lib.org/reference/RETRY.html) to all function calls to more naturally navigate rejected/failed requests from the API.
Expand Down
82 changes: 80 additions & 2 deletions R/espn_mbb_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ espn_mbb_scoreboard <- function(season){

},
error = function(e) {
message(glue::glue("{Sys.time()}: Invalid arguments or no scoreboard data for {game_id} available!"))
message(glue::glue("{Sys.time()}: Invalid arguments or no scoreboard data available!"))
},
warning = function(w) {
},
Expand Down Expand Up @@ -703,7 +703,7 @@ espn_mbb_rankings <- function(){
janitor::clean_names()
},
error = function(e) {
message(glue::glue("{Sys.time()}: Invalid arguments or no rankings data for {game_id} available!"))
message(glue::glue("{Sys.time()}: Invalid arguments or no rankings data available!"))
},
warning = function(w) {
},
Expand All @@ -714,3 +714,81 @@ espn_mbb_rankings <- function(){
return(ranks)
}


#' Get ESPN men's college basketball standings
#'
#' @param year Either numeric or character (YYYY)
#' @keywords MBB Standings
#' @importFrom rlang .data
#' @importFrom jsonlite fromJSON toJSON
#' @importFrom dplyr select rename
#' @importFrom tidyr pivot_wider
#' @export
#' @examples
#' espn_mbb_standings(2021)
espn_mbb_standings <- function(year){

standings_url <- "https://site.web.api.espn.com/apis/v2/sports/basketball/mens-college-basketball/standings?region=us&lang=en&contentorigin=espn&type=0&level=1&sort=winpercent%3Adesc%2Cwins%3Adesc%2Cgamesbehind%3Aasc&"

## Inputs
## year
full_url <- paste0(standings_url,
"season=", year)

res <- httr::RETRY("GET", full_url)

# Check the result
check_status(res)
tryCatch(
expr = {
resp <- res %>%
httr::content(as = "text", encoding = "UTF-8")

raw_standings <- jsonlite::fromJSON(resp)[["standings"]]

#Create a dataframe of all NBA teams by extracting from the raw_standings file

teams <- raw_standings[["entries"]][["team"]]

teams <- teams %>%
dplyr::select(.data$id, .data$displayName) %>%
dplyr::rename(team_id = .data$id,
team = .data$displayName)

#creating a dataframe of the WNBA raw standings table from ESPN

standings_df <- raw_standings[["entries"]][["stats"]]

standings_data <- data.table::rbindlist(standings_df, fill = TRUE, idcol = T)

#Use the following code to replace NA's in the dataframe with the correct corresponding values and removing all unnecessary columns

standings_data$value <- ifelse(is.na(standings_data$value) & !is.na(standings_data$summary), standings_data$summary, standings_data$value)

standings_data <- standings_data %>%
dplyr::select(.data$.id, .data$type, .data$value)

#Use pivot_wider to transpose the dataframe so that we now have a standings row for each team

standings_data <- standings_data %>%
tidyr::pivot_wider(names_from = .data$type, values_from = .data$value)


standings_data <- standings_data %>%
dplyr::select(-.data$.id)

#joining the 2 dataframes together to create a standings table

standings <- cbind(teams, standings_data)
},
error = function(e) {
message(glue::glue("{Sys.time()}: Invalid arguments or no standings data available!"))
},
warning = function(w) {
},
finally = {
}

)
return(standings)
}
86 changes: 85 additions & 1 deletion R/espn_nba_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ espn_nba_scoreboard <- function(season){
}
},
error = function(e) {
message(glue::glue("{Sys.time()}: Invalid arguments or no scoreboard data for {game_id} available!"))
message(glue::glue("{Sys.time()}: Invalid arguments or no scoreboard data available!"))
},
warning = function(w) {
},
Expand All @@ -579,5 +579,89 @@ espn_nba_scoreboard <- function(season){
)
}

#' Get ESPN NBA's Standings
#'
#' @param year Either numeric or character (YYYY)
#' @keywords NBA Standings
#' @importFrom rlang .data
#' @importFrom jsonlite fromJSON toJSON
#' @importFrom dplyr select rename
#' @importFrom tidyr pivot_wider
#' @importFrom data.table rbindlist
#' @export
#' @examples
#' \dontrun{
#' espn_nba_standings(year = 2021)
#' }
espn_nba_standings <- function(year){

standings_url <- "https://site.web.api.espn.com/apis/v2/sports/basketball/nba/standings?region=us&lang=en&contentorigin=espn&type=0&level=1&sort=winpercent%3Adesc%2Cwins%3Adesc%2Cgamesbehind%3Aasc&"

## Inputs
## year
full_url <- paste0(standings_url,
"season=", year)

res <- httr::RETRY("GET", full_url)

# Check the result
check_status(res)
tryCatch(
expr = {
resp <- res %>%
httr::content(as = "text", encoding = "UTF-8")

raw_standings <- jsonlite::fromJSON(resp)[["standings"]]

#Create a dataframe of all NBA teams by extracting from the raw_standings file

teams <- raw_standings[["entries"]][["team"]]

teams <- teams %>%
dplyr::select(.data$id, .data$displayName) %>%
dplyr::rename(team_id = .data$id,
team = .data$displayName)

#creating a dataframe of the WNBA raw standings table from ESPN

teams <- teams %>%
dplyr::select(.data$id, .data$displayName) %>%
dplyr::rename(team_id = .data$id,
team = .data$displayName)

#creating a dataframe of the WNBA raw standings table from ESPN

standings_df <- raw_standings[["entries"]][["stats"]]

standings_data <- data.table::rbindlist(standings_df, fill = TRUE, idcol = T)

#Use the following code to replace NA's in the dataframe with the correct corresponding values and removing all unnecessary columns

standings_data$value <- ifelse(is.na(standings_data$value) & !is.na(standings_data$summary), standings_data$summary, standings_data$value)

standings_data <- standings_data %>%
dplyr::select(.data$.id, .data$type, .data$value)

#Use pivot_wider to transpose the dataframe so that we now have a standings row for each team

standings_data <- standings_data %>%
tidyr::pivot_wider(names_from = .data$type, values_from = .data$value)

standings_data <- standings_data %>%
dplyr::select(-.data$.id)

#joining the 2 dataframes together to create a standings table

standings <- cbind(teams, standings_data)
},
error = function(e) {
message(glue::glue("{Sys.time()}: Invalid arguments or no standings data available!"))
},
warning = function(w) {
},
finally = {
}

)
return(standings)
}
9 changes: 9 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ For more information on the package and function reference, please see the [**`

[**Full News on Releases**](https://saiemgilani.github.io/hoopR/news/index.html)

# **hoopR 1.0.5**
### **Standings functions**
- [```hoopR::espn_nba_standings()```](https://saiemgilani.github.io/hoopR/reference/espn_nba_standings.html)
- [```hoopR::espn_mbb_standings()```](https://saiemgilani.github.io/hoopR/reference/espn_mbb_standings.html)

#
<details>
<summary>View more version news</summary>

# **hoopR 1.0.4**
### **Add retry**
- Adding [```httr::retry()```](https://httr.r-lib.org/reference/RETRY.html) to all function calls to more naturally navigate rejected/failed requests from the API.
Expand Down
19 changes: 19 additions & 0 deletions man/espn_mbb_standings.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions man/espn_nba_standings.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c17ffd9

Please sign in to comment.