<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>OUseful.Info, the blog... &#187; Interactive Scenarios With Shiny &#8211; The Race to the F1 2012 Drivers&#8217; Championship</title>
	<atom:link href="http://blog.ouseful.info/2012/11/18/interactive-scenarios-with-shiny-the-race-to-the-f1-2012-drivers-championship/feed/?withoutcomments=1" rel="self" type="application/rss+xml" />
	<link>http://blog.ouseful.info</link>
	<description>Trying to find useful things to do with emerging technologies in open education</description>
	<lastBuildDate>Tue, 21 May 2013 21:21:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.ouseful.info' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>OUseful.Info, the blog... &#187; Interactive Scenarios With Shiny &#8211; The Race to the F1 2012 Drivers&#8217; Championship</title>
		<link>http://blog.ouseful.info</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.ouseful.info/osd.xml" title="OUseful.Info, the blog..." />
	<atom:link rel='hub' href='http://blog.ouseful.info/?pushpress=hub'/>
		<item>
		<title>Interactive Scenarios With Shiny &#8211; The Race to the F1 2012 Drivers&#8217; Championship</title>
		<link>http://blog.ouseful.info/2012/11/18/interactive-scenarios-with-shiny-the-race-to-the-f1-2012-drivers-championship/</link>
		<comments>http://blog.ouseful.info/2012/11/18/interactive-scenarios-with-shiny-the-race-to-the-f1-2012-drivers-championship/#comments</comments>
		<pubDate>Sun, 18 Nov 2012 18:38:25 +0000</pubDate>
		<dc:creator>Tony Hirst</dc:creator>
				<category><![CDATA[Rstats]]></category>
		<category><![CDATA[Tinkering]]></category>
		<category><![CDATA[ddj]]></category>
		<category><![CDATA[f1datajunkie]]></category>

		<guid isPermaLink="false">http://blog.ouseful.info/?p=9019</guid>
		<description><![CDATA[In Paths to the F1 2012 Championship Based on How They Might Finish in the US Grand Prix I posted a quick hack to calculate the finishing positions that would determine the F1 2012 Drivers&#8217; Championship in today&#8217;s United States Grand Prix, leaving a tease dangling around the possibility of working out what combinations would [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ouseful.info&#038;blog=325417&#038;post=9019&#038;subd=ouseful&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In <a href="http://blog.ouseful.info/2012/11/18/paths-to-the-f1-2012-championship-based-on-how-they-might-finish-in-the-us-grand-prix/">Paths to the F1 2012 Championship Based on How They Might Finish in the US Grand Prix</a> I posted a quick hack to calculate the finishing positions that would determine the F1 2012 Drivers&#8217; Championship in today&#8217;s United States Grand Prix, leaving a tease dangling around the possibility of working out what combinations would lead to a VET or ALO victory if the championship isn&#8217;t decided today. So in the hour before the race started, I began to doodle a quick&#8217;n'dirty interactive app that would let me keep track of what the championship scenarios would be for the Brazil race given the lap by lap placement of VET and ALO during the US Grand Prix. Given the prep I&#8217;d done in the aforementioned post, this meant figuring out how to code up a similar algorithm in R, and then working out how to make it interactive&#8230;</p>
<p>But before I show you how I did it, here&#8217;s the scenario for Brazil given how the US race finished:</p>
<p><a href="http://ouseful.files.wordpress.com/2012/11/f1-drivers-championship-brazil-scenarios.png"><img src="http://ouseful.files.wordpress.com/2012/11/f1-drivers-championship-brazil-scenarios.png?w=700&#038;h=255" alt="" title="F1 Drivers&#039; championship - Brazil scenarios" width="700" height="255" class="alignnone size-full wp-image-9032" /></a></p>
<p>So how was this quick hack app done&#8230;?</p>
<p>Trying out the new <a href="http://www.rstudio.com/shiny/">Shiny interactive stats app builder</a> from the RStudio folk has been on my to do list for some time. It didn&#8217;t take long to realise that an interactive race scenario builder would provide an ideal context for trying it out. There are essentially two (with a minor middle third) steps to a Shiny model:</p>
<ol>
<li>work out the points difference between VET and ALO for all their possible points combinations in the US Grand Prix;</li>
<li>calculate the points difference going into the Brazilian Grand Prix;</li>
<li>calculate the possible outcomes depending on placements in the Brazilian Grand Prix (essentially, an application of the algorithm I did in the original post).</li>
</ol>
<p>The Shiny app requires two bits of code &#8211; a UI in file ui.R, in which I define two sliders that allow me to set the actual (or anticpated, or possible;-) race classifications in the US for Vettel and Alonso:</p>
<pre class="brush: r; title: ; notranslate">library(shiny)

shinyUI(pageWithSidebar(
  
  # Application title
  headerPanel(&quot;F1 Driver Championship Scenarios&quot;),
  
  # Sidebar with a slider input for number of observations
  sidebarPanel(
    sliderInput(&quot;alo&quot;, 
                &quot;ALO race pos in United States Grand Prix:&quot;, 
                min = 1, 
                max = 11, 
                value = 1),
    sliderInput(&quot;vet&quot;, 
                &quot;VET race pos in United States Grand Prix:&quot;, 
                min = 1, 
                max = 11, 
                value = 2)
  ),
  
  # Show a plot of the generated model
  mainPanel(
    plotOutput(&quot;distPlot&quot;)
  )
))</pre>
<p>And some logic, in file server.R (original had errors; hopefully now bugfixed&#8230;) &#8211; the original &#8220;Paths to the Championship&#8221; unpicks elements of  the algorithm in a little more detail, but basically I figure out the points difference between VET and ALO based on the points difference at the start of the race and the additional points difference arising from the posited finishing positions for the US race, and then generate a matrix that works out the difference in points awarded for each possible combination of finishes in Brazil:</p>
<pre class="brush: r; title: ; notranslate">library(shiny)
library(ggplot2)
library(reshape)

# Define server logic required to generate and plot a random distribution
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]])&gt;=0) win[[i,j]]='VET'
        else win[[i,j]]='ALO'
    }
    win
  }
  
  # Function that generates a plot of the distribution. The function
  # is wrapped in a call to reactivePlot to indicate that:
  #
  #  1) It is &quot;reactive&quot; and therefore should be automatically 
  #     re-executed when inputs change
  #  2) Its output type is a plot 
  #
  output$distPlot &lt;- 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=&quot;Championship outcomes in Brazil&quot;)
    g=g+ theme(legend.position=&quot;none&quot;)
    g=g+scale_x_continuous(breaks=seq(1, 11, 1))+scale_y_continuous(breaks=seq(1, 11, 1))
    print(g)
  })
})</pre>
<p>To run the app, if your server and ui files are in some directory <tt>shinychamp</tt>, then something like the following should et the Shiny app running:</p>
<pre class="brush: plain; title: ; notranslate">library(shiny)
runApp(&quot;~/path/to/my/shinychamp&quot;)</pre>
<p><a href="http://glimmer.rstudio.com/psychemedia/f1champ/">Here&#8217;s what it looks like</a>:</p>
<p><a href="http://glimmer.rstudio.com/psychemedia/f1champ/"><img src="http://ouseful.files.wordpress.com/2012/11/f1-2012-scenario-1.png?w=700&#038;h=254" alt="" title="f1 2012 scenario 1" width="700" height="254" class="alignnone size-full wp-image-9024" /></a></p>
<p><a href="http://glimmer.rstudio.com/psychemedia/f1champ/"><img src="http://ouseful.files.wordpress.com/2012/11/f1-2012-scenario-2.png?w=700&#038;h=250" alt="" title="f1 2012 scenario 2" width="700" height="250" class="alignnone size-full wp-image-9025" /></a></p>
<p><a href="http://glimmer.rstudio.com/psychemedia/f1champ/"><img src="http://ouseful.files.wordpress.com/2012/11/f1-2012-scenario-31.png?w=700&#038;h=264" alt="" title="f1 2012 scenario 3" width="700" height="264" class="alignnone size-full wp-image-9026" /></a></p>
<p><a href="http://glimmer.rstudio.com/psychemedia/f1champ/"><img src="http://ouseful.files.wordpress.com/2012/11/f1-2012-scenario-42.png?w=700&#038;h=258" alt="" title="f1 2012 scenario 4" width="700" height="258" class="alignnone size-full wp-image-9027" /></a></p>
<p>You can find the code on github here: <a href="https://github.com/psychemedia/f1DataJunkie/tree/master/f1djR/shinychamp">F1 Championship 2012 &#8211; scenarios if the race gets to Brazil&#8230;</a></p>
<p>Unfortunately, until a hosted service is available, you&#8217;ll have to run it yourself if you want to try it out&#8230;</p>
<p>Disclaimer: I&#8217;ve been rushing to get this posted before the start of the race&#8230; If you spot errors, please shout!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ouseful.wordpress.com/9019/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ouseful.wordpress.com/9019/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ouseful.info&#038;blog=325417&#038;post=9019&#038;subd=ouseful&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ouseful.info/2012/11/18/interactive-scenarios-with-shiny-the-race-to-the-f1-2012-drivers-championship/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/abbd9f90565ce9ae4d065d93a81d8c03?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">Tony Hirst</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/11/f1-drivers-championship-brazil-scenarios.png" medium="image">
			<media:title type="html">F1 Drivers&#039; championship - Brazil scenarios</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/11/f1-2012-scenario-1.png" medium="image">
			<media:title type="html">f1 2012 scenario 1</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/11/f1-2012-scenario-2.png" medium="image">
			<media:title type="html">f1 2012 scenario 2</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/11/f1-2012-scenario-31.png" medium="image">
			<media:title type="html">f1 2012 scenario 3</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/11/f1-2012-scenario-42.png" medium="image">
			<media:title type="html">f1 2012 scenario 4</media:title>
		</media:content>
	</item>
	</channel>
</rss>
