Created
April 14, 2016 03:50
-
-
Save klahrich/89fde998f6887198572820fa110aeb98 to your computer and use it in GitHub Desktop.
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(shinydashboard) | |
library(shiny) | |
library(ggplot2) | |
ui <- fluidPage( | |
sidebarLayout( | |
sidebarPanel( | |
#selectInput("candidate", "Candidate name:", c("D. Trump", "T. Cruz", "H. Clinton", "B. Sanders"), | |
# selected="D. Trump"), | |
selectInput("state", "State:", c("all states", "alabama", "arizona", "arkansas", "colorado", | |
"district of columbia", "florida", "georgia", | |
"hawaii", "idaho", "illinois", "kentucky", | |
"louisiana", "maine", "massachusetts", "michigan", | |
"mississippi", "missouri", "nebraska", | |
"new hampshire", "north carolina", "ohio", | |
"oklahoma", "south carolina", "tennessee", "texas", | |
"utah", "vermont", "virginia", "washington")), | |
selectInput("candidate", "Candidate:", c("all candidates", "D. Trump", "T. Cruz", "H. Clinton", "B. Sanders")), | |
selectInput("variable1", "Variable 1:", c("percent_white", "percent_black", | |
"percent_hispanic", "per_capita_income", "median_rent", | |
"median_age", "votes.percentage")), | |
selectInput("variable2", "Variable 2:", c("percent_white", "percent_black", | |
"percent_hispanic", "per_capita_income", "median_rent", | |
"median_age", "votes.percentage"), selected="votes.percentage") | |
), | |
mainPanel(plotOutput("plot")) | |
) | |
) | |
#setwd("C:/Users/karim/Documents/R/usa_2016/shiny") | |
data = read.csv("all_results_long.csv", stringsAsFactors=FALSE) | |
server <- function(input, output) { | |
dataset <- reactive({ | |
if(input$state == "all states" && input$candidate == "all candidates"){ | |
data | |
} | |
else if(input$state == "all states" && input$candidate != "all candidates"){ | |
data[data$candidate==input$candidate, ] | |
} | |
else if(input$state != "all states" && input$candidate == "all candidates"){ | |
data[data$state.name==input$state, ] | |
} | |
else{ | |
data[data$state.name==input$state & data$candidate==input$candidate, ] | |
} | |
}) | |
output$plot <- renderPlot({ | |
ggplot(dataset(), aes_string(x=input$variable1, y=input$variable2, col="candidate")) + | |
geom_point() + | |
geom_smooth(method=lm, se=FALSE) | |
}) | |
} | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment