Create a user interface with BSLIB to display graph & table. Also create some card at the top to display the key information Use this dataset: https://raw.githubusercontent.com/datasciencedojo/datasets/refs/heads/master/titanic.csv Which is about titanic The data corresponds to the following: PassengerId,Survived,Pclass,Name,Sex,Age,SibSp,Parch,Ticket,Fare,Cabin,Embarked 1,0,3,"Braund, Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S 2,1,1,"Cumings, Mrs. John Bradley (Florence Briggs Thayer)",female,38,1,0,PC 17599,71.2833,C85,C
Drop files here
or click to upload
library(shiny)
library(bslib)
library(dplyr)
library(ggplot2)
library(DT)
source("data_prep.R")
ui <- page_fillable(
title = "Titanic Dashboard",
theme = bs_theme(bootswatch = "simplex"),
# Key metrics in value boxes
layout_columns(
fill = FALSE,
value_box(
title = "Total Passengers",
value = textOutput("total_passengers"),
showcase = bsicons::bs_icon("people-fill")
),
value_box(
title = "Survival Rate",
value = textOutput("survival_rate"),
showcase = bsicons::bs_icon("heart-pulse-fill"),
theme_color = "success"
),
value_box(
title = "Average Fare",
value = textOutput("avg_fare"),
showcase = bsicons::bs_icon("cash-coin"),
theme_color = "info"
)
),
# Main content
layout_columns(
col_widths = c(8, 4),
card(
full_screen = TRUE,
card_header("Survival Analysis by Class and Gender"),
card_body(
plotOutput("survival_plot", height = "400px")
)
),
card(
full_screen = TRUE,
card_header("Age Distribution"),
card_body(
plotOutput("age_plot", height = "400px")
)
)
),
# Data table
card(
full_screen = TRUE,
card_header("Passenger Details"),
card_body(
DTOutput("passenger_table")
)
)
)
server <- function(input, output) {
bs_themer()
# Load and prepare data
titanic_data <- load_titanic_data()
# Key metrics
output$total_passengers <- renderText({
nrow(titanic_data)
})
output$survival_rate <- renderText({
paste0(round(mean(titanic_data$Survived) * 100, 1), "%")
})
output$avg_fare <- renderText({
paste0("$", round(mean(titanic_data$Fare, na.rm = TRUE), 2))
})
# Survival plot
output$survival_plot <- renderPlot({
ggplot(titanic_data, aes(x = factor(Pclass), fill = factor(Survived))) +
geom_bar(position = "fill") +
facet_wrap(~Sex) +
scale_fill_manual(values = c("#FF4136", "#2ECC40"),
labels = c("No", "Yes"),
name = "Survived") +
scale_x_discrete(name = "Passenger Class") +
scale_y_continuous(labels = scales::percent) +
theme_minimal() +
labs(y = "Survival Rate",
title = "Survival Rate by Passenger Class and Gender")
})
# Age distribution plot
output$age_plot <- renderPlot({
ggplot(titanic_data, aes(x = Age, fill = factor(Survived))) +
geom_density(alpha = 0.5) +
scale_fill_manual(values = c("#FF4136", "#2ECC40"),
labels = c("No", "Yes"),
name = "Survived") +
theme_minimal() +
labs(x = "Age",
y = "Density",
title = "Age Distribution by Survival Status")
})
# Passenger table
output$passenger_table <- renderDT({
titanic_data %>%
select(Name, Sex, Age, Pclass, Survived, Fare) %>%
mutate(
Survived = ifelse(Survived == 1, "Yes", "No"),
Pclass = paste0("Class ", Pclass)
) %>%
datatable(
options = list(
pageLength = 10,
scrollX = TRUE,
dom = 'Bfrtip'
),
rownames = FALSE
)
})
}
shinyApp(ui = ui, server = server)
Hi! I can help you with any questions about Shiny and R. What would you like to know?