Skip to content

Instantly share code, notes, and snippets.

@psychemedia
Last active December 18, 2015 18:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save psychemedia/5824495 to your computer and use it in GitHub Desktop.
Save psychemedia/5824495 to your computer and use it in GitHub Desktop.
Quick example of a Shiny app for explorer a dump of Guardian 2014 university rankings data
#Here's a way of grabbing down the data - it probably makes sense to grab a local copy...
library(RCurl)
gsqAPI = function(key,query,gid=0){
#tmp=getURL( paste( sep="",'https://spreadsheets.google.com/tq?', 'tqx=out:csv','&tq=', curlEscape(query), '&key=', key, '&gid=', gid), ssl.verifypeer = FALSE )
tmp=getURL( paste( sep="",'https://docs.google.com/spreadsheets/d/',key,'/gviz/tq?tqx=out:csv','&tq=', curlEscape(query),'&gid=', gid), ssl.verifypeer = FALSE )
return( read.csv( textConnection( tmp ), stringsAsFactors=F ) )
}
handler=function(key,i){
tmp=gsqAPI(key,"select * where B!=''", i)
subject=sub(".Rank",'',colnames(tmp)[1])
colnames(tmp)[1]="Subject.Rank"
tmp$subject=subject
tmp
}
#key='0Aq73qj3QslDedHFNdUsxOVZQZ1dmbXNrYlZGUWgwdHc'
key='1KpjH0-W1kwlnu_INYtSntGzrrfqqiJ12rS2Lk7um-iQ'
gdata=handler(key,2)
for (i in 3:47){
gdata=rbind(gdata,handler(key,i))
}
#save(gdata,file="guardian2014unidata.Rda")
#Or just load the data in if you have saved it previously...
#load("guardian2014unidata.Rda")
gdata[, 4:11] <- sapply(gdata[, 4:11], as.numeric)
gdata$Name.of.Institution=as.factor(gdata$Name.of.Institution)
gdata$subject=as.factor(gdata$subject)
library(shiny)
library(ggplot2)
# Define server logic
shinyServer(function(input, output) {
#Simple test plot
output$testPlot = renderPlot( {
pdata=subset(gdata, Name.of.Institution==input$tbl)
#g=ggplot(pdata) + geom_text(aes(x=X..Satisfied.with.Teaching,y=X..Satisfied.with.Assessment,label=subject,size=Value.added.score.10))
g=ggplot(pdata) + geom_text(aes_string(x=input$tblx,y=input$tbly,size=input$tbls, label='subject'))
g=g+labs(title=paste("Guardian University Tables 2014:",input$tbl))
print(g)
})
})
library(shiny)
uList=levels(gdata$Name.of.Institution)
names(uList) = uList
cList=colnames(gdata[c(1,3:11)])
names(cList) = cList
# Define UI for application that plots random distributions
shinyUI(pageWithSidebar(
# Application title
headerPanel("Guardian 2014 University Tables Explorer"),
sidebarPanel(
#Just a single selector here - which table do you want to view?
selectInput("tbl", "Institution:",uList),
selectInput("tblx", "x axis:",cList,selected = 'X..Satisfied.with.Teaching'),
selectInput("tbly", "y axis:",cList,selected='X..Satisfied.with.Assessment'),
selectInput("tbls", "Label size:",cList,selected = 'Value.added.score.10'),
div("This demo provides a crude graphical view over data extracted from",
a(href='http://www.guardian.co.uk/news/datablog/2013/jun/04/university-guide-2014-table-rankings',
"Guardian Datablog: University guide 2014 data tables") )
),
#The main panel is where the "results" charts are plotted
mainPanel(
plotOutput("testPlot")#,
#tableOutput("view")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment