Skip to content

Instantly share code, notes, and snippets.

@psychemedia
Last active August 29, 2015 14:01
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 psychemedia/6cead2633fdc8d158542 to your computer and use it in GitHub Desktop.
Save psychemedia/6cead2633fdc8d158542 to your computer and use it in GitHub Desktop.
Shiny - F1 2012 Championship showdown
library(shiny)
library(ggplot2)
library(reshape)
shinyServer(function(input, output) {
points=data.frame(pos=1:11,val=c(25,18,15,12,10,8,6,4,2,1,0))
points[[1,2]]
a=245
v=255
pospoints=function(a,v,pdiff,points){
pp=matrix(ncol = nrow(points), nrow = nrow(points))
for (i in 1:nrow(points)){
for (j in 1:nrow(points))
pp[[i,j]]=v-a+pdiff[[i,j]]
}
pp
}
pdiff=matrix(ncol = nrow(points), nrow = nrow(points))
for (i in 1:nrow(points)){
for (j in 1:nrow(points))
pdiff[[i,j]]=points[[i,2]]-points[[j,2]]
}
ppx=pospoints(a,v,pdiff,points)
winmdiff=function(vadiff,pdiff,points){
win=matrix(ncol = nrow(points), nrow = nrow(points))
for (i in 1:nrow(points)){
for (j in 1:nrow(points))
if (i==j) win[[i,j]]=''
else if ((vadiff+pdiff[[i,j]])>=0) win[[i,j]]='VET'
else win[[i,j]]='ALO'
}
win
}
output$distPlot <- reactivePlot(function() {
wmd=winmdiff(ppx[[input$vet,input$alo]],pdiff,points)
wmdm=melt(wmd)
g=ggplot(wmdm)+geom_text(aes(X1,X2,label=value,col=value))
g=g+xlab('VET position in Brazil')+ ylab('ALO position in Brazil')
g=g+labs(title="Championship outcomes in Brazil")
g=g+ theme(legend.position="none")
g=g+scale_x_continuous(breaks=seq(1, 11, 1))+scale_y_continuous(breaks=seq(1, 11, 1))
print(g)
})
})
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("F1 Driver Championship Scenarios"),
sidebarPanel(
sliderInput("alo",
"ALO race pos in United States Grand Prix:",
min = 1,
max = 11,
value = 1),
sliderInput("vet",
"VET race pos in United States Grand Prix:",
min = 1,
max = 11,
value = 2)
),
mainPanel(
plotOutput("distPlot")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment