library(tidyverse)
library(httr)
library(jsonlite)
library(RCurl)
It is important to code defensively by capturing and processing error codes. To test if the code will fail if the status code is anything other than 200, uncomment the get_status code
api_call_return <- GET(url)
(get_status <- api_call_return$status_code)
[1] 200
# Uncomment to test stop_for_status for API call error
#get_status<- 404
Test the return status and extract the api result set for processing
if (get_status != 200) {
stop_for_status(get_status)
}
api_call_header <- headers(api_call_return)
api_call_parsed <- content(api_call_return,"parse")
results_list <- api_call_parsed[["results"]]
head(results_list,1)
[[1]]
[[1]]$display_title
[1] "Shiva Baby"
[[1]]$mpaa_rating
[1] ""
[[1]]$critics_pick
[1] 1
[[1]]$byline
[1] "Jason Bailey"
[[1]]$headline
[1] "‘Shiva Baby’ Review: It’s Complicated"
[[1]]$summary_short
[1] "The potential land mines of a young woman’s life are set to explode simultaneously in this tense comedy from Emma Seligman."
[[1]]$publication_date
[1] "2021-04-08"
[[1]]$opening_date
[1] "2021-04-02"
[[1]]$date_updated
[1] "2021-04-08 15:29:03"
[[1]]$link
[[1]]$link$type
[1] "article"
[[1]]$link$url
[1] "https://www.nytimes.com/2021/04/08/movies/shiva-baby-review.html"
[[1]]$link$suggested_link_text
[1] "Read the New York Times Review of Shiva Baby"
[[1]]$multimedia
[[1]]$multimedia$type
[1] "mediumThreeByTwo210"
[[1]]$multimedia$src
[1] "https://static01.nyt.com/images/2021/04/10/arts/shiva1/shiva1-mediumThreeByTwo440.jpg"
[[1]]$multimedia$height
[1] 140
[[1]]$multimedia$width
[1] 210
For this vignette I am using a subset of the baseball.csv from https://www.kaggle.com/danielmontilla/baseball-databank. The subset file contains 62 players from 2019 MLB season and I will calculate batting average by position for 2019 as well as visualize the top player batting averages for 2019 by position type (outfield, infield and catcher). The catcher position is not considered an infield nor outfield position. The catcher is the only postion positioned in foul territory. Plus the catcher is the only player that can see the entire field. How many of you new this?
# Source the subset file from the Kaggle Website from my github repository
filename <- getURL("https://raw.githubusercontent.com/audiorunner13/Masters-Coursework/main/DATA607%20Spring%202021/TidyVerseVignette/Data/BattingByPosition.csv")
batting_by_posit <- read.csv(text = filename,na.strings = "")
batting_by_posit
The next four sections calculate the batting average for each position type
# create a batting_by_posit_totals data.frame and aggregate the avg by position type
batting_by_posit_totals <- aggregate.data.frame(x = batting_by_posit$avg, # Sum by group
by = list(batting_by_posit$posit),
FUN = sum)
# rename fields in df
(batting_by_posit_totals <- batting_by_posit_totals %>%
dplyr::rename("position" = Group.1, "total_bavg" = x))
# calc a prercentage field and append to repsective rows
catcher_total_bavg_rec <- batting_by_posit_totals %>% filter(batting_by_posit_totals$position == "catcher")
catcher_bavg <- catcher_total_bavg_rec$total_bavg/14
bat_avg <- round(catcher_bavg,3)
(catcher_total_bavg_rec <- cbind(catcher_total_bavg_rec,bat_avg))
# calc a prercentage field and append to repsective rows
outfield_total_bavg_rec <- batting_by_posit_totals %>% filter(batting_by_posit_totals$position == "outfield")
outfield_bavg <- outfield_total_bavg_rec$total_bavg/24
bat_avg <- round(outfield_bavg,3)
(outfield_total_bavg_rec <- cbind(outfield_total_bavg_rec,bat_avg))
# calc a prercentage field and append to repsective rows
infield_total_bavg_rec <- batting_by_posit_totals %>% filter(batting_by_posit_totals$position == "infield")
infield_bavg <- infield_total_bavg_rec$total_bavg/24
bat_avg <- round(infield_bavg,3)
(infield_total_bavg_rec <- cbind(infield_total_bavg_rec,bat_avg))
final_batting_by_posit <- data.frame(c())
final_batting_by_posit <- rbind(final_batting_by_posit,catcher_total_bavg_rec)
final_batting_by_posit <- rbind(final_batting_by_posit,outfield_total_bavg_rec)
final_batting_by_posit <- rbind(final_batting_by_posit,infield_total_bavg_rec)
final_batting_by_posit
final_batting_by_posit %>%
ggplot(aes(y=reorder(position,bat_avg),x=bat_avg,fill=position)) +
geom_bar(stat = 'identity',position=position_dodge()) +
geom_text(aes(label=bat_avg), vjust=1.0, color="black",
position = position_dodge(0.9), size=3.0) +
labs(y = ("Position"),x = ("Position Batting Average"),
title = ("2019 Batting Average by Positiion")) +
theme_minimal()
(outfield_players <- batting_by_posit %>% filter(batting_by_posit$posit == "outfield"))
outfield_players %>%
top_n(15) %>%
ggplot(aes(y=reorder(playerID,avg),x=avg,fill=playerID)) +
geom_bar(stat = 'identity',position=position_dodge()) +
geom_text(aes(label=avg), vjust=1.0, color="black",
position = position_dodge(0.9), size=3.0) +
labs(y = ("Player"),x = ("Outfielder Batting Average"),
title = ("2019 Top 15 Outfielder Batting Averages")) +
theme_minimal()
Selecting by GIDP
(infield_players <- batting_by_posit %>% filter(batting_by_posit$posit == "infield"))
infield_players %>%
top_n(15) %>%
ggplot(aes(y=reorder(playerID,avg),x=avg,fill=playerID)) +
geom_bar(stat = 'identity',position=position_dodge()) +
geom_text(aes(label=avg), vjust=1.0, color="black",
position = position_dodge(0.9), size=3.0) +
labs(y = ("Player"),x = ("Infielder Batting Average"),
title = ("2019 Top 15 Infielder Batting Averages")) +
theme_minimal()
Selecting by GIDP
(catchers <- batting_by_posit %>% filter(batting_by_posit$posit == "catcher"))
catchers %>%
top_n(10) %>%
ggplot(aes(y=reorder(playerID,avg),x=avg,fill=playerID)) +
geom_bar(stat = 'identity',position=position_dodge()) +
geom_text(aes(label=avg), vjust=1.0, color="black",
position = position_dodge(0.9), size=3.0) +
labs(y = ("Player"),x = ("Catcher Batting Average"),
title = ("2019 Top 10 Catcher Batting Averages")) +
theme_minimal()
Selecting by GIDP