Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
## Load the stattleshipR package
sport <- 'baseball'
league <- 'mlb'
ep <- 'game_logs'
q_body <- list(team_id='mlb-bos', status='ended', interval_type='regularseason')
gls <- ss_get_result(sport=sport, league=league, ep=ep, query=q_body, walk=TRUE)
game_logs<'rbind', lapply(gls, function(x) x$game_logs))
sport <- 'baseball'
league <- 'mlb'
ep <- 'players'
q_body <- list(team_id='mlb-bos')
pls <- ss_get_result(sport=sport, league=league, ep=ep, query=q_body, walk=TRUE)
players<'rbind', lapply(pls, function(x) x$players))
colnames(players)[1] <- 'player_id'
game_logs <- merge(players, game_logs, by='player_id')
stats <-
game_logs %>%
filter(game_played == TRUE) %>%
group_by(name) %>%
summarise(totalRuns = sum(runs), meanBA = mean(batting_average), totalBases=sum(total_bases), salary=max(salary))
ggplot(stats, aes(x=totalRuns, y=meanBA, size=totalBases, label=name, color=salary)) + geom_text()

Thanks for the code! Everything goes well for me until the game_logs command before the stats command, where I get the following error:

Error in : object 'game_logs' not found

Please advise on how I can correct this or where I can go to troubleshoot.


tcash21 commented May 4, 2016

Sorry about that! Just fixed the code.

im getting a premature EOF error. Do you know why?

hquach commented Jan 21, 2017

I'm also getting the "Error: parse error: premature EOF" error after running the command:
gls <- ss_get_result(sport=sport, league=league, ep=ep, query=q_body, walk=TRUE)

For those seeing an "Error: parse error: premature EOF" it could be that your token has expired.

In November of last year, we at Stattleship decided to use Patreon to ask the data science and hobbyist community to support our efforts to maintain the API.

The R wrapper used doesn't handle the unauthorized response and fails when trying to parse the response as JSON.

Thanks to the community, we're adding more features and recently launched NBA and NHL play-by-play data with shot court/rink coordinates.

Check us out at Stattleship API or API docs or on Slack.

One note, as we're about to begin the 2017 MLB season, it's wise to include both a season_id and interval_type in:

q_body <- list(team_id='mlb-bos', season_id='mlb-2016', interval_type='regularseason')

Since we default to the current season, this was you'll be certain to fetch the data you expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment