You will see that I have done a couple of things. Make use of browser() to see specifically what is happening where.
You will see that I have created this risk_classification column which classifies the risk. The first one is selective where it uses breaks_calc based on which crime columns are selected and adjusting the ranges. This is one way you can do it.
You will see in USArrests3 I created a set of steps for you to classify the specific state and then pass that info to a render text.
All these changes above should help you with getting to what you exactly want.
library(shiny)
library(tidyverse)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Rate of Crime in United States"),
p("Use the variable selector to refine your search!"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
checkboxGroupInput("display_var",
"Which Crime/s to Display?",
choices = c("Murder" = "Murder",
"Assault" = "Assault",
"Rape" = "Rape"),
selected = "Murder"
),
sliderInput("bins",
"Number of bins (valid for Histogram chart only):",
min = 5,
max = 10,
value = 7
),
selectInput(
"search", "How safe is this state?", choices = (attributes(USArrests)$row.names), selected = NULL)
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(
tabPanel("Bar Plot", plotOutput("barplot")),
tabPanel("Histogram", plotOutput("distPlot")),
tabPanel("How Safe is the State?", textOutput("howsafe"))
)
)
))
# Define server logic required to draw a histogram
server <- function(input, output) {
output$barplot <- renderPlot({
marchoice <- req(input$display_var)
sd <- setdiff(names(USArrests),marchoice)
temp_df <- USArrests
temp_df[,sd] <- 0
counts <- temp_df$Murder + temp_df$Assault + temp_df$Rape
names(counts) <- rownames(temp_df)
barplot(counts,
main="Aggregate Sum of Crime in the United States",
xlab="State",
ylab="Frequency",las=2,col=rgb(0.2,0.4,0.6,0.6))
})
output$distPlot <- renderPlot({
#calculate the specific breaks based on which crime categories are selected
if(length(input$display_var == 3)){
breaks_calc <- c(0,150,300,450)
} else if(length(input$display_var == 2)){
breaks_calc <- c(0,150,300,450)*2/3
} else {
breaks_calc <- c(0,150,300,450)*1/3
}
#create new data based on the selection
USArrests2 <-
USArrests %>%
#magic happens here with Unquoting the input variable with the bang-bang
select(!!input$display_var) %>%
#we create the new cumaltive column based on row sums where they are numeric
mutate(cumulative_frequency = rowSums(across(where(is.numeric)))) %>%
#creating the risk classification column for the data based on the cumulative freq and the specified breaks
mutate(risk_classification = cut(cumulative_frequency, breaks_calc, labels = c("Low", "Medium", "High")))
# create plot - we show the cum_freq
ggplot(USArrests2, aes(cumulative_frequency)) + ggtitle("Histogram of Variable Frequency") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_histogram(bins = input$bins,
fill = rgb(0.2,0.4,0.6,0.6),
colour = "grey30") +
#we create a new label based on what has been selected
xlab(str_c(input$display_var, collapse = " & ")) +
theme_minimal()
})
output$searchstate <- renderDataTable(USArrests, options = list(pageLength = 5))
output$howsafe <- renderText({
USArrests2 <-
USArrests %>%
#magic happens here with Unquoting the input variable with the bang-bang
select(!!input$display_var) %>%
#we create the new cumaltive column based on row sums where they are numeric
mutate(cumulative_frequency = rowSums(across(where(is.numeric)))) %>%
#creating the risk classification column for the data based on the cumulative freq and the specified breaks
mutate(risk_classification = cut(cumulative_frequency, breaks_calc, labels = c("Low", "Medium", "High")))
USArrests3 <-
USArrests %>%
mutate(states_as_row_names = rownames(USArrests)) %>%
filter(states_as_row_names == !!input$search) %>%
select(Murder, Assault, Rape) %>%
mutate(cumulative_frequency = rowSums(across(where(is.numeric)))) %>%
mutate(risk_classification = cut(cumulative_frequency, c(0,150,300,450), labels = c("low", "medium", "high")))
#show the rate of crime for the select country
print(paste0(input$search, " has a " , USArrests3$risk_classification, " rate of crime"))
})
}
# Run the application
shinyApp(ui = ui, server = server)
Created on 2021-10-21 by the reprex package (v2.0.1)