Skip to content

Instantly share code, notes, and snippets.

@tcash21
Created January 18, 2016 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcash21/1f87fd0c386237c36176 to your computer and use it in GitHub Desktop.
Save tcash21/1f87fd0c386237c36176 to your computer and use it in GitHub Desktop.
Reddit Live Game Thread Swears by Fanbase Dashboard
library(stattleshipR)
library(ggplot2)
library(dplyr)
library(shiny)
library(shinydashboard)
library(RCurl)
## get a free Stattleship API token from www.stattleship.com
set_token("stattleship-token")
league <- "nfl"
sport <- "football"
ep <- "game_logs"
shinyServer(function(session, input, output) {
getScore <- reactive({
invalidateLater(120000, session)
q_body <- list(status = 'in_progress', interval_type='divisionalplayoffs')
games <- ss_get_result(sport=sport, league=league, ep=ep, query=q_body, version=1, verbose=TRUE, walk=FALSE)
#gl <- lapply(games, function(x) x$games)
g <- games[[1]]$games
away_score <- g$away_team_score
home_score <- g$home_team_score
return(data.frame(away=away_score, home=home_score))
})
getData <- reactive({
invalidateLater(30000, session)
#q_body <- list(status = 'in_progress', interval_type='divisionalplayoffs')
#games <- ss_get_result(sport=sport, league=league, ep=ep, query=q_body, version=1, verbose=TRUE, walk=FALSE)
#gl <- lapply(games, function(x) x$games)
x<-getURL('https://s3.amazonaws.com/stattleship/comments.csv')
swears <- read.csv(text=x)
swears <- swears[match(unique(swears$ids), swears$ids),]
swears$date <- as.POSIXct(swears$date, origin = "1970-01-01")
## round to minute
swears$date <- strptime(format(round(swears$date, units="mins"), format="%Y-%m-%d %H:%M"), format='%Y-%m-%d %H:%M')
swears$date <- as.POSIXct(swears$date)
swears <- subset(swears, flairs == "Steelers" | flairs == "Broncos")
the_data <- summarise(group_by(swears, date, flairs), counts = length(flairs))
return(the_data)
})
getData2 <- reactive({
invalidateLater(30000, session)
x<-getURL('https://s3.amazonaws.com/stattleship/comments.csv')
swears <- read.csv(text=x)
swears <- swears[match(unique(swears$ids), swears$ids),]
swears$date <- as.POSIXct(swears$date, origin = "1970-01-01")
## round to minute
swears$date <- strptime(format(round(swears$date, units="mins"), format="%Y-%m-%d %H:%M"), format='%Y-%m-%d %H:%M')
swears$date <- as.POSIXct(swears$date)
return(swears)
})
output$plot1 <- renderPlot({
the_data <- getData()
if(is.null(the_data)){
return(plot(x=0,y=0, xlab='No anomalies detected, try another stat or player'))
} else{
print(ggplot(the_data, aes(x=date, y=counts, colour=flairs)) + geom_line())
}
})
output$table1 <- renderTable({
the_data <- getData2()
the_data <- data.frame(table(the_data$flairs))
colnames(the_data) <- c("Team_Flair", "Comments")
the_data <- the_data[order(the_data$Comments, decreasing=TRUE),]
return(the_data)
}, include.rownames=FALSE)
output$table2 <- renderTable({
the_data <- getScore()
return(the_data)
}, include.rownames=FALSE)
})
library(shiny)
library(shinydashboard)
dashboardPage(
dashboardHeader(title = "NFL Live Reddit Game Thread Swears by Fanbase", titleWidth = "580"),
dashboardSidebar(
sidebarMenu(id="menu1", a(img(src="logo.png", align="left", width="230px", class="nav_logo"),
href="http://www.stattleship.com", target="_blank")
))
,
dashboardBody(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "app.css")
),
fluidRow(
plotOutput("plot1", height = 350),
column(6, tableOutput('table1')),
column(6, tableOutput('table2'))
))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment