Created
November 6, 2013 01:08
-
-
Save andland/7329230 to your computer and use it in GitHub Desktop.
Shiny App plotting the average age of everyone in America with a given name in a particular year. To run, install shiny in R and type: library(shiny);
runGist(7329230)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(ggplot2) | |
exp.age.df=read.csv("https://dl.dropboxusercontent.com/u/17648661/ExpAgeByNameYear.csv") | |
age.range=range(exp.age.df$Age) | |
unique.names=sort(unique(exp.age.df$Name)) | |
unique.names=c("<NONE>",as.character(unique.names)) | |
start.names=c("Andrew","Dylan","Fred","Grace","Lillian","John") | |
a=0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define server logic required to summarize and view the selected dataset | |
shinyServer(function(input, output,session) { | |
# Generate a summary of the dataset | |
output$namePlot <- renderPlot({ | |
if (input$rand>a) { | |
name.list=sort(sample(as.character(unique(exp.age.df$Name)),6)) | |
} else { | |
name.list=c(input$name1,input$name2,input$name3,input$name4,input$name5,input$name6) | |
} | |
a=input$rand | |
# print(paste(input$rand,a)) | |
plot.lines=any(name.list!="<NONE>") | |
if (plot.lines) { | |
p<-ggplot(subset(exp.age.df,Name %in% name.list),aes(Year,Age,colour=Name,shape=Name,linetype=Name))+ | |
geom_line()+geom_point()+labs(y="Average Age")+scale_y_continuous(limits=age.range) | |
} else { | |
p<-ggplot(subset(exp.age.df,Name=="John"),aes(Year,Age))+geom_point(col=NA)+ | |
labs(y="Average Age")+scale_y_continuous(limits=age.range) | |
} | |
print(p) | |
},width=700,height=450) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define UI for dataset viewer application | |
shinyUI(pageWithSidebar( | |
# Application title | |
headerPanel("Average Age based on First Name and Year"), | |
# Sidebar with controls to select a dataset and specify the number | |
# of observations to view | |
sidebarPanel( | |
selectInput("name1", "First Name:", | |
choices = unique.names, | |
selected=start.names[1]), | |
selectInput("name2", "Second Name:", | |
choices = unique.names, | |
selected=start.names[2]), | |
selectInput("name3", "Third Name:", | |
choices = unique.names, | |
selected=start.names[3]), | |
selectInput("name4", "Fourth Name:", | |
choices = unique.names, | |
selected=start.names[4]), | |
selectInput("name5", "Fifth Name:", | |
choices = unique.names, | |
selected=start.names[5]), | |
selectInput("name6", "Sixth Name:", | |
choices = unique.names, | |
selected=start.names[6]), | |
actionButton("rand","Six Random Names"), | |
helpText( | |
"Refresh the page to select specific names after using the random name button" | |
), | |
p("My blog - ", | |
a("Statistically Significant", href="http://alandgraf.blogspot.com") | |
), | |
p("Follow me - ", | |
a("@andland", href="http://twitter.com/andland") | |
) | |
), | |
# Show a plot | |
mainPanel( | |
plotOutput("namePlot") | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run, install shiny, then:
library(shiny)
runGist(7329230)