library(shiny)
library(dplyr)
# Sample data frame (replace this with your actual data)
hospital_data <- data.frame(
State = c("CA", "NY", "TX", "CA", "NY", "TX"),
Illness = c("cancer", "cardiology", "neurology", "cardiology", "cancer", "neurology"),
Hospital = c("UCLA Medical Center", "New York-Presbyterian", "Houston Methodist", "Stanford Health Care", "Mount Sinai", "UT Southwestern")
)
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel("Best Hospital Finder"),
# Sidebar layout with input and output definitions
sidebarLayout(
# Sidebar panel for inputs
sidebarPanel(
textInput("state", "Enter State Initials (e.g., CA for California):", ""),
textInput("illness", "Enter Illness:", ""),
actionButton("submit", "Submit")
),
# Main panel for displaying outputs
mainPanel(
textOutput("result")
)
)
)
# Define server logic
server <- function(input, output) {
# Reactive expression to process the user input and find the best hospital
result <- eventReactive(input$submit, {
state <- toupper(input$state)
illness <- tolower(input$illness)
# Assuming 'hospital_data' is your data frame containing hospital info
hospital <- hospital_data %>%
filter(State == state, tolower(Illness) == illness) %>%
select(Hospital) %>%
slice(1) %>%
pull()
if (is.null(hospital)) {
return("No matching hospital found.")
} else {
return(paste("The best hospital in", state, "for", illness, "is:", hospital))
}
})
# Output the result
output$result <- renderText({
result()
})
}
# Run the application
shinyApp(ui = ui, server = server)