Skip to content

Instantly share code, notes, and snippets.

@atbradley
Created February 8, 2013 03:01
Show Gist options
  • Select an option

  • Save atbradley/4736289 to your computer and use it in GitHub Desktop.

Select an option

Save atbradley/4736289 to your computer and use it in GitHub Desktop.
A shiny app using Social Security baby names data aggregated by Hadley Wickham (https://github.com/hadley/data-baby-names).
library(shiny)
library(ggplot2)
#This is at http://raw.github.com/hadley/data-baby-names/master/baby-names.csv
names <- read.csv('~/R/baby-names.csv')
# Define server logic required to plot various variables against mpg
shinyServer(function(input, output) {
# Return the formula text for printing as a caption
output$caption <- reactiveText(function() {
"Plot"
})
output$subCaption <- reactiveText(function() {
"Plot"
})
output$plot <- reactivePlot(function() {
nms <- names[names$year==input$baseYear,]
nms <- nms[nms$sex==input$sex,]
nms <- nms$name[order(nms$percent, decreasing=T)]
nms <- nms[1:input$nameCount]
wrk <- names[names$name %in% nms,]
wrk <- wrk[wrk$sex==input$sex,]
wrk <- wrk[wrk$year %in%
seq(from=input$yearRange[[1]],
to=input$yearRange[[2]]),]
plt <- ggplot(wrk) +
geom_line(aes(year, percent, colour=name))
print(plt)
})
})
library(shiny)
# Define UI for miles per gallon application
shinyUI(pageWithSidebar(
# Application title
headerPanel("Baby Names"),
sidebarPanel(
selectInput('sex', 'Sex:',
list('Male' = 'boy',
'Female' = 'girl')),
sliderInput('nameCount', 'Number of popular names to plot:',
min=1, max=10, value=5),
sliderInput('baseYear', 'Year to choose popular names from:',
min=1880, max=2008, value=1880),
sliderInput("yearRange", "Years to plot:",
min = 1880, max = 2008, value = c(1880,2008))
),
mainPanel(
h3(textOutput("caption")),
h4(textOutput("subCaption")),
plotOutput("plot")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment