Update app.R
Browse files
app.R
CHANGED
@@ -10,207 +10,6 @@ library(rnaturalearth)
|
|
10 |
library(rnaturalearthdata)
|
11 |
library(countrycode)
|
12 |
|
13 |
-
# =============================
|
14 |
-
# UI
|
15 |
-
# =============================
|
16 |
-
ui <- dashboardPage(
|
17 |
-
|
18 |
-
# Use black skin for the dashboard
|
19 |
-
skin = "black",
|
20 |
-
|
21 |
-
dashboardHeader(
|
22 |
-
title = span(
|
23 |
-
style = "font-weight: 600; font-size: 18px;",
|
24 |
-
"Country Representation"
|
25 |
-
)
|
26 |
-
),
|
27 |
-
|
28 |
-
dashboardSidebar(
|
29 |
-
sidebarMenu(
|
30 |
-
menuItem("Map Type", tabName = "cartogramTab", icon = icon("globe"))
|
31 |
-
),
|
32 |
-
div(
|
33 |
-
style = "margin: 15px;",
|
34 |
-
selectInput(
|
35 |
-
inputId = "indexChoice",
|
36 |
-
label = "Select Representation Index:",
|
37 |
-
choices = c("Overall", "RepresentationGap", "Ethnicity",
|
38 |
-
"Gender", "Religion", "Language"),
|
39 |
-
selected = "Overall"
|
40 |
-
)
|
41 |
-
)
|
42 |
-
),
|
43 |
-
|
44 |
-
dashboardBody(
|
45 |
-
# Bring in the OCR A Extended font from Google Fonts
|
46 |
-
tags$head(
|
47 |
-
tags$link(
|
48 |
-
href = "https://fonts.googleapis.com/css2?family=OCR+A+Extended&display=swap",
|
49 |
-
rel = "stylesheet"
|
50 |
-
),
|
51 |
-
tags$style(HTML("
|
52 |
-
/* Force OCR A Extended font across the entire UI and all HTML elements */
|
53 |
-
html, body, h1, h2, h3, h4, h5, h6, p, div, span, label, input, button, select,
|
54 |
-
.box, .content-wrapper, .main-sidebar, .main-header .navbar, .main-header .logo,
|
55 |
-
.sidebar-menu, .sidebar-menu li a, .sidebar-menu .fa {
|
56 |
-
font-family: 'OCR A Extended', monospace !important;
|
57 |
-
}
|
58 |
-
|
59 |
-
/* Header gradient background */
|
60 |
-
.main-header .navbar {
|
61 |
-
background: linear-gradient(to right, #3b6978, #204051) !important;
|
62 |
-
}
|
63 |
-
|
64 |
-
/* Logo area (left corner of the header) */
|
65 |
-
.main-header .logo {
|
66 |
-
background: #1b2a2f !important;
|
67 |
-
color: #ffffff !important;
|
68 |
-
border-bottom: none;
|
69 |
-
font-size: 18px;
|
70 |
-
font-weight: 600;
|
71 |
-
}
|
72 |
-
|
73 |
-
/* Sidebar background */
|
74 |
-
.main-sidebar {
|
75 |
-
background-color: #1b2a2f !important;
|
76 |
-
}
|
77 |
-
|
78 |
-
/* Active or hovered tab in the sidebar */
|
79 |
-
.sidebar-menu > li.active > a,
|
80 |
-
.sidebar-menu > li:hover > a {
|
81 |
-
background-color: #344e5c !important;
|
82 |
-
border-left-color: #78cdd7 !important;
|
83 |
-
color: #ffffff !important;
|
84 |
-
}
|
85 |
-
|
86 |
-
/* Sidebar menu item icons */
|
87 |
-
.sidebar-menu .fa {
|
88 |
-
color: #78cdd7 !important;
|
89 |
-
}
|
90 |
-
|
91 |
-
/* Sidebar menu item text */
|
92 |
-
.sidebar-menu > li > a {
|
93 |
-
color: #b8c7ce !important;
|
94 |
-
font-size: 15px;
|
95 |
-
font-weight: 500;
|
96 |
-
}
|
97 |
-
|
98 |
-
/* Customize the boxes */
|
99 |
-
.box {
|
100 |
-
border-top: none !important;
|
101 |
-
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
102 |
-
border-radius: 6px;
|
103 |
-
}
|
104 |
-
.box.box-solid > .box-header {
|
105 |
-
background-color: #204051;
|
106 |
-
color: #fff;
|
107 |
-
border-radius: 6px 6px 0 0;
|
108 |
-
}
|
109 |
-
|
110 |
-
/* Plot box spacing */
|
111 |
-
.box .box-body {
|
112 |
-
padding: 0 !important;
|
113 |
-
}
|
114 |
-
|
115 |
-
/* Footer text styling (plot captions, etc.) */
|
116 |
-
.small, small {
|
117 |
-
font-size: 75%;
|
118 |
-
}
|
119 |
-
"))
|
120 |
-
),
|
121 |
-
|
122 |
-
tabItems(
|
123 |
-
tabItem(
|
124 |
-
tabName = "cartogramTab",
|
125 |
-
fluidRow(
|
126 |
-
box(
|
127 |
-
width = 12,
|
128 |
-
title = strong("Global Leadership Project (GLP)"),
|
129 |
-
solidHeader = TRUE,
|
130 |
-
div(style = "height: 80vh; padding: 10px;",
|
131 |
-
plotOutput("cartogramPlot", height = "100%")
|
132 |
-
)
|
133 |
-
)
|
134 |
-
)
|
135 |
-
)
|
136 |
-
)
|
137 |
-
)
|
138 |
-
)
|
139 |
-
|
140 |
-
# =============================
|
141 |
-
# SERVER
|
142 |
-
# =============================
|
143 |
-
server <- function(input, output, session) {
|
144 |
-
|
145 |
-
# ---- Read CSV data and create ISO3 codes ----
|
146 |
-
rankings_data <- reactive({
|
147 |
-
read_csv("CountryRepresentationRankings.csv") %>%
|
148 |
-
mutate(iso_a3 = countrycode(Country, origin = "country.name", destination = "iso3c"))
|
149 |
-
})
|
150 |
-
|
151 |
-
# ---- Read/prepare world map shapefile ----
|
152 |
-
world_sf <- reactive({
|
153 |
-
ne_countries(scale = "medium", returnclass = "sf") %>%
|
154 |
-
dplyr::select(name, iso_a3, pop_est, geometry) %>%
|
155 |
-
st_transform(crs = "ESRI:54009") # Mollweide projection
|
156 |
-
})
|
157 |
-
|
158 |
-
# ---- Create cartogram (currently a regular map) ----
|
159 |
-
cartogram_sf <- reactive({
|
160 |
-
merged_sf <- world_sf() %>%
|
161 |
-
left_join(rankings_data(), by = "iso_a3")
|
162 |
-
# Filter out NA values in 'Overall' to avoid missing countries
|
163 |
-
merged_sf <- merged_sf[!is.na(merged_sf$Overall),]
|
164 |
-
return(merged_sf)
|
165 |
-
})
|
166 |
-
|
167 |
-
# ---- Plot output ----
|
168 |
-
output$cartogramPlot <- renderPlot({
|
169 |
-
req(input$indexChoice)
|
170 |
-
|
171 |
-
index_col <- input$indexChoice
|
172 |
-
plot_data <- cartogram_sf()
|
173 |
-
|
174 |
-
ggplot(plot_data) +
|
175 |
-
geom_sf(aes_string(fill = index_col), color = "grey20", size = 0.1) +
|
176 |
-
scale_fill_viridis_c(option = "D", na.value = "white") +
|
177 |
-
coord_sf(expand = FALSE) +
|
178 |
-
# Force OCR A Extended font in the plot
|
179 |
-
#theme_void(base_size = 14, base_family = "OCR A Extended") +
|
180 |
-
theme_void(base_size = 14, base_family = "Courier New") +
|
181 |
-
labs(
|
182 |
-
fill = paste(index_col, "Index"),
|
183 |
-
title = "Country Representation Rankings",
|
184 |
-
subtitle = "Map Colored by Selected Representation Index",
|
185 |
-
caption = "Source: Global Leadership Project (GLP) & Natural Earth"
|
186 |
-
) +
|
187 |
-
theme(
|
188 |
-
plot.title = element_text(face = "bold", hjust = 0.5, size = 20),
|
189 |
-
plot.subtitle = element_text(hjust = 0.5, size = 14),
|
190 |
-
plot.caption = element_text(hjust = 1, size = 10),
|
191 |
-
legend.position = "bottom",
|
192 |
-
legend.direction = "horizontal",
|
193 |
-
legend.key.width = unit(2, "cm")
|
194 |
-
)
|
195 |
-
})
|
196 |
-
}
|
197 |
-
|
198 |
-
# =============================
|
199 |
-
# Launch the Shiny App
|
200 |
-
# =============================
|
201 |
-
shinyApp(ui = ui, server = server)
|
202 |
-
options(error = NULL)
|
203 |
-
library(shiny)
|
204 |
-
library(shinydashboard)
|
205 |
-
library(dplyr)
|
206 |
-
library(readr)
|
207 |
-
library(sf)
|
208 |
-
library(cartogram) # optional if you decide to do cartograms later
|
209 |
-
library(ggplot2)
|
210 |
-
library(rnaturalearth)
|
211 |
-
library(rnaturalearthdata)
|
212 |
-
library(countrycode)
|
213 |
-
|
214 |
# =============================
|
215 |
# UI
|
216 |
# =============================
|
|
|
10 |
library(rnaturalearthdata)
|
11 |
library(countrycode)
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# =============================
|
14 |
# UI
|
15 |
# =============================
|