-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_script.R
82 lines (62 loc) · 2.6 KB
/
test_script.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
rm(list = ls())
# Load necessary libraries
library(tidyverse)
library(RColorBrewer)
library(shiny)
library(plotly)
life_expectancy <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-12-05/life_expectancy.csv')
life_expectancy_different_ages <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-12-05/life_expectancy_different_ages.csv')
life_expectancy_female_male <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-12-05/life_expectancy_female_male.csv')
# Assuming all datasets are loaded into R
combined_df <- life_expectancy %>%
inner_join(life_expectancy_different_ages, by = c("Entity", "Code", "Year")) %>%
inner_join(life_expectancy_female_male, by = c("Entity", "Code", "Year"))
## Building the Shiny App
ui <- fluidPage(
titlePanel("Life Expectancy Trends"),
sidebarLayout(
sidebarPanel(
selectInput("countryInput", "Select Country", choices = unique(combined_df$Entity)),
selectInput("ageGroupInput", "Select Age Group",
choices = c("At Birth" = "LifeExpectancy0",
"Age 10" = "LifeExpectancy10",
"Age 25" = "LifeExpectancy25",
"Age 45" = "LifeExpectancy45",
"Age 65" = "LifeExpectancy65",
"Age 80" = "LifeExpectancy80"))
),
mainPanel(
tabsetPanel(
tabPanel("Life Expectancy by Age Group", plotlyOutput("lifeExpPlot")),
tabPanel("Gender Disparity in Life Expectancy", plotlyOutput("lifeExpDiffPlot"))
)
)
)
)
server <- function(input, output) {
output$lifeExpPlot <- renderPlotly({
filtered_data <- combined_df %>%
filter(Entity == input$countryInput) %>%
select(Year, input$ageGroupInput)
p <- ggplot(filtered_data, aes(x = Year,
y = get(input$ageGroupInput))) +
geom_line(color = "blue") +
labs(x = "Year",
y = "Life Expectancy") +
theme_minimal()
ggplotly(p)
})
output$lifeExpDiffPlot <- renderPlotly({
filtered_data <- combined_df %>%
filter(Entity == input$countryInput) %>%
select(Year, LifeExpectancyDiffFM)
p <- ggplot(filtered_data, aes(x = Year,
y = LifeExpectancyDiffFM)) +
geom_line(color = "red") +
labs(x = "Year",
y = "Life Expectancy Difference") +
theme_minimal()
ggplotly(p)
})
}
shinyApp(ui = ui, server = server)