Skip to content

Instantly share code, notes, and snippets.

@tcash21
Last active December 21, 2015 13:49
Show Gist options
  • Save tcash21/6315013 to your computer and use it in GitHub Desktop.
Save tcash21/6315013 to your computer and use it in GitHub Desktop.
.chart_container {
position: relative;
display: inline-block;
font-family: Arial, Helvetica, sans-serif;
}
.rChart {
display: inline-block;
margin-left: 40px;
}
.yAxis {
position: absolute;
top: 0;
bottom: 0;
width: 40px;
}
.legend {
position: absolute;
top: 0;
right: -160px;
vertical-align: top;
}
.slider {
margin-left: 40px;
margin-top: 12px;
width: 500px;
}
#table {
position: absolute;
top: 525px;
bottom: 0;
left: 300px;
}
#plot {
position: absolute;
left: 250px;
}
.select {
max-width: 350px;
}
.textarea {
max-width: 205px;
}
.well {
max-width: 255px;
}
.xchart {
height: 450px;
}
#text {
position: absolute;
width: 275px;
left: 45px;
top: 525px;
}
load(url('http://s3.amazonaws.com/rshiny/picktable.Rdat'))
load(url('http://s3.amazonaws.com/rshiny/players.Rdat'))
load(url('http://s3.amazonaws.com/rshiny/conf.Rdat'))
library(plyr)
powerRatings <- ddply(adf, .(pick1), summarize, power = sum(betterpick))
powerRatings<-powerRatings[order(powerRatings$power, decreasing=TRUE),]
powerRatings$position <- playerdf[match(powerRatings$pick1, playerdf$name),]$pos
powerRatings$power <- (powerRatings$power / max(powerRatings$power)) * 100
powerRatings <- powerRatings[order(powerRatings$power, decreasing=TRUE),]
require(shiny)
require(data.table)
require(rCharts)
library(RCurl)
library(plyr)
shinyServer(function(input, output, session){
newData <- reactive({
# Don't do anything until after the first button push.
if (input$action == 0)
return(powerRatings)
if (input$reset != 0){
return(restart())
input$reset <- 0
}
return(isolate({
powerRatings <<- powerRatings[-which(powerRatings$pick1 == input$draftedPlayer),]
}))
})
restart <- reactive({
powerRatings <- ddply(adf, .(pick1), summarize, power = sum(betterpick))
powerRatings<-powerRatings[order(powerRatings$power, decreasing=TRUE),]
powerRatings$position <- playerdf[match(powerRatings$pick1, playerdf$name),]$pos
powerRatings$power <- (powerRatings$power / max(powerRatings$power)) * 100
powerRatings <- powerRatings[order(powerRatings$power, decreasing=TRUE),]
return(powerRatings)
})
inputPower <- reactive({
x <- newData()
if(!("All" %in% input$position2)) {
a <- x[x[,3] %in% input$position2,]$pick1
return(a)
} else{
return(x$pick1)
}
})
input1Choices <- reactive({
if(!("All" %in% input$position)) {
a <- playerdf[playerdf$pos %in% input$position,]$name
return(a)
} else{
return(playerdf$name)
}
})
input2Choices <- reactive({
if(!("All" %in% input$position)) {
p <- playerdf[-grep(input$player1, playerdf$name),]
p <- p[p$pos %in% input$position,]
return(p$name)
} else {
return (playerdf[-grep(input$player1,
playerdf$name),]$name)
}
})
output$draftedPlayer <- renderUI({
selectInput("draftedPlayer", "", choices=inputPower())
})
output$player1 <- renderUI({
selectInput("player1", "", choices=input1Choices())
})
output$player2 <- renderUI({
if(is.null(input$player1))
return()
selectInput("player2", "OR", choices=input2Choices())
})
output$table = renderChart2({
if(is.null(input$player2) | is.null(input$player1)){
return(invisible())
}
if(input$allPlayers == FALSE){
p1 <- playerdf[playerdf$name == input$player1,]
p2 <- playerdf[playerdf$name == input$player2,]
d <- as.data.frame(rbind(p1, p2))
dTable(d)
} else {
if(!("All" %in% input$position)) {
p <- playerdf[playerdf$pos %in% input$position,]
dTable(p)
} else{
dTable(playerdf)
}
}
})
output$plot = renderChart2({
if(is.null(input$player1) | is.null(input$player2)) {
return(hPlot$new())
}
d1 <- confByDay[which(confByDay$name == input$player1),]
d2 <- confByDay[which(confByDay$name == input$player2),]
d1 <- d1[order(d1$thedate),]
d2 <- d2[order(d2$thedate),]
d <- rbind(d1, d2)
colnames(d)[3] <- "Uncertainty"
h1 <- hPlot(x="thedate", y="Uncertainty", data=d,
type=c("line"), group="name")
h1$xAxis(labels = list(rotation=-75, align="right"),
categories=d$thedate)
h1
})
output$text <- renderPrint({
if(is.null(input$player1) | is.null(input$player2)){
return(invisible())
}
x1 <- adf[which(adf$pick1 == input$player1 & adf$pick2 ==
input$player2),]$betterpick
x2 <- adf[which(adf$pick1 == input$player2 & adf$pick2 ==
input$player1),]$betterpick
tp1 <- playerdf[which(playerdf$name == input$player1),]$timesPicked
tp2 <- playerdf[which(playerdf$name == input$player2),]$timesPicked
if(tp1 >= tp2){
tpmin <- tp2
tpmax <- tp1
} else if (tp2 >= tp1){
tpmin <- tp1
tpmax <- tp2
}
if(x1 >= x2){
y <- paste(input$player1, " was drafted <br>",
signif((x1 / tpmin) * 100, 3), "% of the time before ", input$player2,
sep="")
o <- HTML(paste("<p><strong>", y, "</p>", sep=""))
cat(as.character(o))
} else {
y <- paste(input$player1, " was drafted <br>",
signif((x1 / tpmax) * 100, 3), "% of the time before ", input$player2,
sep="")
o <- HTML(paste("<p><strong>", y, "</p>"))
cat(as.character(o))
}
})
output$powerPosition <- renderChart2({
x <- newData()
x1 <- ddply(x, .(position), summarize, meanPower=mean(power))
n1 <- nPlot(meanPower ~ position, group = "position", data = x1, type = "multiBarChart")
#n1$print("chart3")
n1
})
output$power <- renderChart2({
if(is.null(newData())){
return()
}
x <- newData()
if(!("All" %in% input$position2)) {
p <- x[x[,3] %in% input$position2,]
#p <- p[order(p$power, decreasing=TRUE),]
dTable(p, aaSorting=list(c(1, "desc")))
} else{
p <- x[order(x[,2], decreasing=TRUE),]
dTable(p, aaSorting=list(c(1, "desc")))
}
})
output$about <- renderPrint({
info <- "Blah Blah <strong> Blah"
cat(as.character(HTML(info)))
})
})
library(shiny)
shinyUI(pageWithSidebar(
headerPanel(title="Fantasy Football - Who Should I Draft?"),
sidebarPanel(
includeCSS('app.css'),
conditionalPanel(
condition="input.tabs1=='Compare Two Players'",
uiOutput("player1"),
uiOutput("player2"),
checkboxInput("allPlayers", "Show All Players in Table", FALSE),
checkboxGroupInput("position", "Position", c("All"="All",
"QB"= "QB", "WR"="WR", "TE"="TE", "RB"="RB", "PK"="PK"),
selected="All"),
helpText(paste("Data is based on ", sum(playerdf$timesPicked),
" picks from ", max(playerdf$timesPicked), "
mock drafts from
http://www.fantasyfootballcalculator.com", sep=""))
),
conditionalPanel(
condition="input.tabs1=='Power By Position'",
checkboxGroupInput("position2", "Position", c("All"="All",
"QB"= "QB", "WR"="WR", "TE"="TE", "RB"="RB", "PK"="PK"),
selected="All"),
uiOutput('draftedPlayer'),
actionButton("action", "Player Was Drafted"),
actionButton("reset", "Reset"),
helpText(paste("The lower chart shows remaining average power by position. Choose players from the dropdown that have been drafted
and the chart and table will update. Data is based on ", sum(playerdf$timesPicked),
" picks from ", max(playerdf$timesPicked), "
mock drafts from
http://www.fantasyfootballcalculator.com", sep=""))
)
),
mainPanel(
tabsetPanel(id = "tabs1",
#plotOutput("plot"),
#showOutput("plot", "xcharts"),
tabPanel("Power By Position", showOutput('power', 'datatables'), showOutput('powerPosition', 'NVD3')),
tabPanel("Compare Two Players", showOutput('plot', 'highcharts'),
showOutput('table', 'datatables'), htmlOutput("text")),
tabPanel("About", htmlOutput("about"))
)
#chartOutput('table', 'datatables'),
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment