Create an application which predict the survival rate from the titatic. Create a split,
Drop files here
or click to upload
library(shiny)
library(ggplot2)
library(dplyr)
source("data_prep.R")
ui <- fluidPage(
titlePanel("Titanic Survival Prediction"),
splitLayout(
cellWidths = c("50%", "50%"),
# Left panel
wellPanel(
selectInput("pclass", "Passenger Class:",
choices = c("1st" = "1", "2nd" = "2", "3rd" = "3")),
selectInput("sex", "Sex:",
choices = c("Male", "Female")),
numericInput("age", "Age:", value = 30, min = 0, max = 100),
numericInput("sibsp", "Number of Siblings/Spouses:", value = 0, min = 0),
numericInput("parch", "Number of Parents/Children:", value = 0, min = 0),
actionButton("predict", "Predict Survival")
),
# Right panel
wellPanel(
plotOutput("survivalPlot"),
verbatimTextOutput("predictionText")
)
)
)
server <- function(input, output, session) {
# Reactive value for prediction
prediction <- eventReactive(input$predict, {
new_passenger <- data.frame(
Pclass = as.numeric(input$pclass),
Sex = tolower(input$sex),
Age = input$age,
SibSp = input$sibsp,
Parch = input$parch
)
predict(survival_model, newdata = new_passenger, type = "prob")[,2]
})
# Output prediction text
output$predictionText <- renderText({
prob <- prediction()
paste("Survival Probability:", round(prob * 100, 1), "%")
})
# Output survival plot
output$survivalPlot <- renderPlot({
# Create plot showing survival rates by class and sex
titanic_data %>%
group_by(Pclass, Sex) %>%
summarise(
SurvivalRate = mean(Survived),
.groups = 'drop'
) %>%
ggplot(aes(x = factor(Pclass), y = SurvivalRate, fill = Sex)) +
geom_bar(stat = "identity", position = "dodge") +
scale_y_continuous(labels = scales::percent) +
labs(x = "Passenger Class",
y = "Survival Rate",
title = "Survival Rates by Class and Sex") +
theme_minimal()
})
}
shinyApp(ui = ui, server = server)
Hi! I can help you with any questions about Shiny and R. What would you like to know?