Hello,
I am stuck on how to write my server logic for my checkboxgroup widget. I currently have the server logic for a histogram to display the frequency of a particular crime (murder, assault or rape). However, I am unable to make the model show cumulative frequency based on variables selected so that if they select Murder AND rape or assault AND rape it produces an aggregate frequency of the variables selected. Any help would be appreciated!
library(shiny)
library(ggplot2)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Rate of Crime in United States"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
checkboxGroupInput("display_var",
"Which variable to display",
choices = c("Murder" = "Murder",
"Assault" = "Assault",
"Rape" = "Rape"),
selected = "Murder"
),
sliderInput("bins",
"Number of bins:",
min = 5,
max = 10,
value = 7)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# set x-axis label depending on the value of display_var
if (input$display_var == "Murder") {
xlabel <- "Frequency of Murder)"
} else if (input$display_var == "Assault") {
xlabel <- "Frequency of Assault"
} else if (input$display_var == "Rape") {
xlabel <- "Frequency of Rape"
}
# create plot
ggplot(USArrests, aes(.data[[input$display_var]])) +
geom_histogram(bins = input$bins,
fill = "steelblue3",
colour = "grey30") +
xlab(xlabel) +
theme_minimal()
})
}
# Run the application
shinyApp(ui = ui, server = server)