Thanks Nir,
I've decided for simplicity just to recreate the USArrests dataframe manually. Im starting with 5 states first to make sure the program runs smoothly. I want to use ggplot as i intend on using additional widgets which require the graph to be modified. I cant for some reason get the else if statement to work so that when i press on murder and assault on the website, the graph responds by including the additional variable in the sum.
library(shiny)
# Create data
data <- data.frame(
state=c("Alabama","Alaska","Arizona","Arkansas","California") ,
murder=c(13.2,10,8.1,8.8,9),
assault=c(236, 263, 294, 190, 276),
rape=c(21.2, 44.5, 31, 19.5, 40.6)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Murder, Assaults and Rapes in the United States"),
p("Use the variable selector to refine your search!"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
checkboxGroupInput("murder_assault_rape",
"Which variable to display",
choices = c(
"Murder" = "Murder",
"Assault" = "Assault",
"Rape" = "Rape"
),
selected = "Murder"
)
),
mainPanel(
plotOutput("barplot")
)
)
)
server <- function(input, output, session) {
output$barplot <- renderPlot({
if (input$murder_assault_rape == c("Murder"))
{
ggplot(data, aes(x=state, y=murder)) +
geom_bar(stat="identity", fill="steelblue")+
geom_text(aes(label=murder), vjust=1.6, color="white", size=3.5)+
theme_minimal()}
else if (input$murder_assault_rape == c("Murder", "Assault"))
{
ggplot(data, aes(x=state, y=murder + assault)) +
geom_bar(stat="identity", fill="steelblue")+
geom_text(aes(label=murder), vjust=1.6, color="white", size=3.5)+
theme_minimal()}
}
)
}
shinyApp(ui, server)
This is what i've gotten so far. Any help will be appreciated