Skip to content

Instantly share code, notes, and snippets.

@garrettgman
Last active December 22, 2015 11:19

Revisions

  1. garrettgman revised this gist Sep 6, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions server.R
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    library(shiny)
    require(maps)
    require(mapproj)
    if(!require(maps)) stop("This app requires the maps package.\nPlease install it and then try again.")
    if(!require(mapproj)) stop("This app requires the maps package.\nPlease install it and then try again.")

    counties <- readRDS("counties.RDS")

  2. garrettgman revised this gist Sep 6, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion server.R
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    library(shiny)
    library(maps)
    require(maps)
    require(mapproj)

    counties <- readRDS("counties.RDS")

  3. garrettgman revised this gist Sep 6, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion server.R
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    library(shiny)
    library(maps)

    counties <- readRDS("data/counties.RDS")
    counties <- readRDS("counties.RDS")

    mp <- map("county", plot=FALSE, namesonly=TRUE)
    c.order <- match(mp,
  4. garrettgman revised this gist Sep 6, 2013. 1 changed file with 0 additions and 0 deletions.
    Binary file added counties.RDS
    Binary file not shown.
  5. garrettgman created this gist Sep 6, 2013.
    57 changes: 57 additions & 0 deletions server.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    library(shiny)
    library(maps)

    counties <- readRDS("data/counties.RDS")

    mp <- map("county", plot=FALSE, namesonly=TRUE)
    c.order <- match(mp,
    paste(counties$region, counties$subregion, sep = ","))

    shinyServer(function(input, output) {

    indexInput <- reactive({
    var <- switch(input$var,
    "Total Population (logged)" = log(counties$pop),
    "Percent White" = counties$white,
    "Percent Black" = counties$black,
    "Percent Hispanic" = counties$hispanic,
    "Percent Asian" = counties$asian)

    var <- pmax(var, input$range[1])
    var <- pmin(var, input$range[2])
    as.integer(cut(var, 100, include.lowest = TRUE,
    ordered = TRUE))[c.order]
    })

    shadesInput <- reactive({
    switch(input$var,
    "Percent White" = colorRampPalette(c("white", "darkgreen"))(100),
    "Percent Black" = colorRampPalette(c("white", "black"))(100),
    "Percent Hispanic" = colorRampPalette(c("white", "darkorange3"))(100),
    "Percent Asian" = colorRampPalette(c("white", "darkviolet"))(100))
    })

    legendText <- reactive({
    inc <- diff(range(input$range)) / 4
    c(paste0(input$range[1], " % or less"),
    paste0(input$range[1] + inc, " %"),
    paste0(input$range[1] + 2 * inc, " %"),
    paste0(input$range[1] + 3 * inc, " %"),
    paste0(input$range[2], " % or more"))
    })


    output$mapPlot <- renderPlot({
    fills <- shadesInput()[indexInput()]

    map("county", fill = TRUE, col = fills,
    resolution = 0, lty = 0, projection="polyconic",
    myborder = 0, mar = c(0,0,0,0))
    map("state",col = "white", fill=FALSE, add=TRUE, lty=1,
    lwd=1,projection="polyconic", myborder = 0,
    mar = c(0,0,0,0))
    legend("bottomleft", legend = legendText(),
    fill = shadesInput()[c(1, 25, 50, 75, 100)],
    title = input$var)
    })
    })
    24 changes: 24 additions & 0 deletions ui.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    library(shiny)

    shinyUI(pageWithSidebar(

    headerPanel("censusVis"),

    sidebarPanel(
    helpText("Create demographic maps with information from the 2010 US Census."),
    selectInput("var", "Choose a variable to display",
    choices = c(
    "Percent White",
    "Percent Black",
    "Percent Hispanic",
    "Percent Asian"
    ),
    selected = "Percent White"
    ),
    sliderInput("range", "Range of interest:",
    min = 0, max = 100, value = c(0, 100))
    ),
    mainPanel(
    plotOutput("mapPlot", height = "600px")
    )
    ))