Update app.R
Browse files
app.R
CHANGED
@@ -1,58 +1,145 @@
|
|
|
|
|
|
1 |
library(shiny)
|
2 |
-
library(bslib)
|
3 |
library(dplyr)
|
4 |
library(ggplot2)
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
),
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
)
|
26 |
|
27 |
server <- function(input, output, session) {
|
28 |
-
subsetted <- reactive({
|
29 |
-
req(input$species)
|
30 |
-
df |> filter(Species %in% input$species)
|
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 |
-
shinyApp(ui, server)
|
|
|
1 |
+
# install.packages(c("shiny", "dplyr", "ggplot2", "DT"))
|
2 |
+
|
3 |
library(shiny)
|
|
|
4 |
library(dplyr)
|
5 |
library(ggplot2)
|
6 |
+
library(DT)
|
7 |
+
|
8 |
+
ui <- fluidPage(
|
9 |
+
titlePanel("Country Representation Rankings Explorer"),
|
10 |
|
11 |
+
sidebarLayout(
|
12 |
+
sidebarPanel(
|
13 |
+
h4("Controls"),
|
14 |
+
selectInput(
|
15 |
+
inputId = "metric",
|
16 |
+
label = "Select a Representation Metric:",
|
17 |
+
choices = c(
|
18 |
+
"Overall",
|
19 |
+
"Representation Gap",
|
20 |
+
"Ethnicity",
|
21 |
+
"Gender",
|
22 |
+
"Religion",
|
23 |
+
"Language"
|
24 |
+
),
|
25 |
+
selected = "Overall"
|
26 |
+
),
|
27 |
+
checkboxInput(
|
28 |
+
inputId = "sort_desc",
|
29 |
+
label = "Sort in descending order",
|
30 |
+
value = TRUE
|
31 |
+
),
|
32 |
+
hr(),
|
33 |
+
sliderInput(
|
34 |
+
inputId = "top_n",
|
35 |
+
label = "Number of Countries to Display in Plot:",
|
36 |
+
min = 5,
|
37 |
+
max = 50,
|
38 |
+
value = 10,
|
39 |
+
step = 1
|
40 |
+
)
|
41 |
),
|
42 |
+
|
43 |
+
mainPanel(
|
44 |
+
tabsetPanel(
|
45 |
+
tabPanel(
|
46 |
+
title = "Plot",
|
47 |
+
plotOutput("bar_plot", height = "600px")
|
48 |
+
),
|
49 |
+
tabPanel(
|
50 |
+
title = "Data Table",
|
51 |
+
DTOutput("data_table")
|
52 |
+
),
|
53 |
+
tabPanel(
|
54 |
+
title = "Summary",
|
55 |
+
verbatimTextOutput("summary_text")
|
56 |
+
)
|
57 |
+
)
|
58 |
+
)
|
59 |
+
)
|
60 |
)
|
61 |
|
62 |
server <- function(input, output, session) {
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
# Load the data (adjust the path as necessary)
|
65 |
+
data_raw <- reactive({
|
66 |
+
read.csv("CountryRepresentationRankings.csv", stringsAsFactors = FALSE)
|
67 |
+
})
|
68 |
+
|
69 |
+
# Reactively prepare the data for plotting and table display
|
70 |
+
data_prepared <- reactive({
|
71 |
+
req(data_raw())
|
72 |
+
# Convert to tibble for convenience
|
73 |
+
df <- tibble::as_tibble(data_raw())
|
74 |
+
|
75 |
+
# Ensure the selected metric is numeric for plotting
|
76 |
+
# (This is typically already numeric, but we can do a safe check.)
|
77 |
+
# Also handle potential issues with missing values.
|
78 |
+
df <- df %>%
|
79 |
+
mutate(
|
80 |
+
across(all_of(input$metric), as.numeric)
|
81 |
+
) %>%
|
82 |
+
filter(!is.na(.data[[input$metric]]))
|
83 |
+
|
84 |
+
# Sort ascending or descending
|
85 |
+
if (input$sort_desc) {
|
86 |
+
df <- df %>% arrange(desc(.data[[input$metric]]))
|
87 |
+
} else {
|
88 |
+
df <- df %>% arrange(.data[[input$metric]])
|
89 |
+
}
|
90 |
+
|
91 |
+
# Keep only the top N rows for plotting
|
92 |
+
df <- df %>% slice_head(n = input$top_n)
|
93 |
+
|
94 |
+
df
|
95 |
+
})
|
96 |
|
97 |
+
# Render bar plot of the selected metric for top N countries
|
98 |
+
output$bar_plot <- renderPlot({
|
99 |
+
df <- data_prepared()
|
100 |
+
|
101 |
+
# Convert Country to factor for a nice ordered plot
|
102 |
+
df <- df %>%
|
103 |
+
mutate(Country = factor(Country, levels = Country))
|
104 |
+
|
105 |
+
ggplot(df, aes(x = Country, y = .data[[input$metric]])) +
|
106 |
+
geom_col(fill = "steelblue") +
|
107 |
+
coord_flip() +
|
108 |
+
labs(
|
109 |
+
x = "Country",
|
110 |
+
y = paste0(input$metric, " Index"),
|
111 |
+
title = paste("Top", input$top_n, "Countries by", input$metric)
|
112 |
+
) +
|
113 |
+
theme_minimal(base_size = 14)
|
114 |
+
})
|
115 |
+
|
116 |
+
# Render data table of the entire dataset
|
117 |
+
output$data_table <- renderDT({
|
118 |
+
req(data_raw())
|
119 |
+
datatable(
|
120 |
+
data_raw(),
|
121 |
+
options = list(pageLength = 10, autoWidth = TRUE),
|
122 |
+
filter = "top",
|
123 |
+
rownames = FALSE
|
124 |
+
)
|
125 |
+
})
|
126 |
|
127 |
+
# Render summary statistics text for the selected metric
|
128 |
+
output$summary_text <- renderPrint({
|
129 |
+
df <- data_raw()
|
130 |
+
req(df)
|
131 |
+
# Convert the chosen metric to numeric if needed
|
132 |
+
df[[input$metric]] <- suppressWarnings(as.numeric(df[[input$metric]]))
|
133 |
+
|
134 |
+
valid_metric <- df[[input$metric]][!is.na(df[[input$metric]])]
|
135 |
+
if (length(valid_metric) == 0) {
|
136 |
+
cat("No valid numeric data for the selected metric.")
|
137 |
+
} else {
|
138 |
+
summary_stats <- summary(valid_metric)
|
139 |
+
cat("Summary of", input$metric, "across all countries:\n")
|
140 |
+
print(summary_stats)
|
141 |
+
}
|
142 |
+
})
|
143 |
}
|
144 |
|
145 |
+
shinyApp(ui = ui, server = server)
|