Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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