File size: 14,289 Bytes
a0659af 4021d3c 42b4218 0dbfc34 42b4218 f02e31c 6daf1d7 b9b9ee1 f20f49f 8aa102f 638f9f7 b74a0a8 638f9f7 44bda9c b74a0a8 44bda9c b74a0a8 44bda9c ca0cc05 b74a0a8 f02e31c 1551a7f 0dbfc34 b74a0a8 638f9f7 b74a0a8 638f9f7 089f30d b74a0a8 089f30d b74a0a8 089f30d b74a0a8 089f30d b74a0a8 089f30d b74a0a8 089f30d b74a0a8 f02e31c b74a0a8 f02e31c b74a0a8 f02e31c 638f9f7 b74a0a8 638f9f7 b74a0a8 638f9f7 b74a0a8 8061733 b74a0a8 f9c04ed 638f9f7 b74a0a8 943901c b74a0a8 943901c f02e31c b74a0a8 638f9f7 b74a0a8 f02e31c 20b5007 943901c 20b5007 b74a0a8 20b5007 943901c b74a0a8 20b5007 f02e31c 44bda9c 4a2e9da 44bda9c b74a0a8 44bda9c b74a0a8 44bda9c b74a0a8 44bda9c b74a0a8 c0d7f46 b74a0a8 44bda9c 8aa102f 0dbfc34 b74a0a8 f02e31c 638f9f7 f02e31c 0dbfc34 2acecda 8061733 f02e31c 4fa92b9 8aa102f 7952109 f02e31c 638f9f7 7952109 f02e31c 7952109 f02e31c 42b4218 7952109 8aa102f 2acecda 7952109 b9b9ee1 7952109 8aa102f f02e31c 7f0f334 8aa102f 7952109 f20f49f 7952109 7f0f334 7952109 b196acf 7952109 b196acf 7952109 7f0f334 7952109 b9b9ee1 7952109 f20f49f 7952109 b9b9ee1 7f0f334 7952109 20b5007 7952109 20b5007 7952109 20b5007 7952109 20b5007 0dbfc34 f02e31c 7952109 |
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# setwd("~/Downloads")
options(error = NULL)
library(shiny)
library(shinydashboard)
library(dplyr)
library(readr)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(countrycode)
library(leaflet) # <-- For OpenStreetMap-based map
library(viridisLite)
# =============================
# UI
# =============================
ui <- dashboardPage(
skin = "black",
# =======================
# HEADER
# =======================
dashboardHeader(
title = span(
style = "font-weight: 600; font-size: 13px;",
a(
href = "http://www.globalleadershipproject.net",
"GlobalLeadershipProject.net",
target = "_blank",
style = "color: white; text-decoration: underline;"
)
)
),
# =======================
# SIDEBAR
# =======================
dashboardSidebar(
sidebarMenu(
menuItem("Map Type", tabName = "cartogramTab", icon = icon("globe"))
),
div(
style = "margin: 15px;",
selectInput(
inputId = "indexChoice",
label = "Select Representation Index:",
choices = c("Overall", "RepresentationGap", "Ethnicity",
"Gender", "Religion", "Language"),
selected = "Overall"
)
),
# ---- “Share” button ----
tags$div(
style = "text-align: left; margin: 1em 0 1em 2em;",
HTML('
<button id="share-button"
style="
display:inline-flex;
align-items:center;
justify-content:center;
gap:8px;
padding:5px 10px;
font-size:16px;
font-weight:normal;
color:#000;
background-color:#fff;
border:1px solid #ddd;
border-radius:6px;
cursor:pointer;
box-shadow:0 1.5px 0 #000;">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2"
stroke-linecap="round" stroke-linejoin="round">
<circle cx="18" cy="5" r="3"></circle>
<circle cx="6" cy="12" r="3"></circle>
<circle cx="18" cy="19" r="3"></circle>
<line x1="8.59" y1="13.51" x2="15.42" y2="17.49"></line>
<line x1="15.41" y1="6.51" x2="8.59" y2="10.49"></line>
</svg>
<strong>Share</strong>
</button>
'),
tags$script(HTML("
(function () {
const shareBtn = document.getElementById('share-button');
function showCopyNotification () {
const n = document.createElement('div');
n.innerText = 'Copied to clipboard';
n.style.position = 'fixed';
n.style.bottom = '20px';
n.style.right = '20px';
n.style.background = 'rgba(0,0,0,0.8)';
n.style.color = '#fff';
n.style.padding = '8px 12px';
n.style.borderRadius = '4px';
n.style.zIndex = '9999';
document.body.appendChild(n);
setTimeout(() => n.remove(), 2000);
}
shareBtn.addEventListener('click', () => {
const url = window.location.href;
const title = document.title || 'Check this out!';
if (navigator.share) {
navigator.share({ title, text: '', url }).catch(console.log);
} else if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(url).then(showCopyNotification);
} else {
const ta = document.createElement('textarea');
ta.value = url;
document.body.appendChild(ta);
ta.select();
try { document.execCommand('copy'); showCopyNotification(); }
catch { alert('Please copy this link:\\n' + url); }
document.body.removeChild(ta);
}
});
})();
"))
)
# ---- /Share button ----
),
# =======================
# BODY
# =======================
dashboardBody(
tags$head(
tags$link(
href = "https://fonts.googleapis.com/css2?family=OCR+A+Extended&display=swap",
rel = "stylesheet"
),
tags$style(HTML("
/* -------- Base typography & theme -------- */
html, body, h1, h2, h3, h4, h5, h6, p, div, span, label, input, button,
select, .box, .content-wrapper, .main-sidebar, .main-header .navbar,
.main-header .logo, .sidebar-menu, .sidebar-menu li a, .sidebar-menu .fa {
font-family: 'OCR A Extended', monospace !important;
}
.main-header .navbar { background: linear-gradient(to right,#3b6978,#204051)!important; }
.main-header .logo { background:#1b2a2f!important; color:#fff!important;
border-bottom:none; font-size:18px; font-weight:600; }
.main-sidebar { background-color:#1b2a2f!important; }
.sidebar-menu>li.active>a,
.sidebar-menu>li:hover>a { background:#344e5c!important; border-left-color:#78cdd7!important;
color:#fff!important; }
.sidebar-menu .fa { color:#78cdd7!important; }
.sidebar-menu>li>a { color:#b8c7ce!important; font-size:15px; font-weight:500; }
.box { border-top:none!important; box-shadow:0 4px 8px rgba(0,0,0,.1);
border-radius:6px; }
.box.box-solid>.box-header{ background:#204051; color:#fff; border-radius:6px 6px 0 0; }
.box .box-body { padding:0!important; }
.small, small { font-size:75%; }
.map-container { height:70vh; width:100%; }
@media (max-width:768px) { .map-container{height:50vh;} }
/* -------- Mobile font bump -------- */
@media (max-width:768px){
html, body { font-size:18px!important; line-height:1.4!important; }
.sidebar-menu>li>a { font-size:18px!important; }
.sidebar-menu .fa { font-size:20px!important; }
.main-header .logo { font-size:22px!important; }
.box.box-solid>.box-header,
.box>.box-header>.box-title { font-size:20px!important; }
input, select, button { font-size:18px!important; }
.leaflet-popup-content,
.leaflet-tooltip { font-size:16px!important; }
}
"))
),
tabItem(
tabName = "cartogramTab",
fluidRow(
column(
width = 9,
box(
width = NULL,
title = strong("Country-level Representation"),
solidHeader = TRUE,
div(class = "map-container",
leafletOutput("cartogramPlot", width = "100%", height = "100%"))
)
),
column(
width = 3,
box(
width = NULL,
title = strong("Selected Country Data"),
solidHeader = TRUE,
uiOutput("selectedCountryData"),
style = "overflow-y: auto;"
)
)
),
fluidRow(
column(
width = 9,
box(
width = NULL,
title = strong("Citation"),
solidHeader = TRUE,
tags$p(
"John Gerring, Connor T. Jerzak, Erzen Öncel. ",
"The Composition of Descriptive Representation. ",
em("American Political Science Review,"), " 118(2): 784–801, 2024. ",
tags$a(
href = "https://www.cambridge.org/core/services/aop-cambridge-core/content/view/7EAEA1CA4C553AB9D76054D1FA9C0840/S0003055423000680a.pdf/the-composition-of-descriptive-representation.pdf",
"PDF", target = "_blank"
), " | ",
tags$a(
href = "https://connorjerzak.com/wp-content/uploads/2024/07/CompositionBib.txt",
"BibTeX", target = "_blank"
), " | ",
tags$a(
href = "https://www.youtube.com/watch?v=nnfDj1NdOMo",
"YouTube", target = "_blank"
)
)
)
)
)
)
)
)
# =============================
# SERVER
# =============================
server <- function(input, output, session) {
# 1. Custom matches for countries not recognized by default in 'countrycode'
custom_iso_matches <- c("Kosovo" = "XKX",
"Somaliland" = "SOM") # or any valid code you prefer
# 2. Read CSV data and create ISO3 codes with custom matches
rankings_data <- reactive({
read_csv("CountryRepresentationRankings.csv", show_col_types = FALSE) %>%
mutate(iso_a3 = countrycode(
sourcevar = Country,
origin = "country.name",
destination = "iso3c",
custom_match = custom_iso_matches
))
})
# 3. Read/prepare world map shapefile (still from Natural Earth),
# but transform to lat/lon (EPSG:4326) to align with Leaflet.
world_sf <- reactive({
ne_countries(scale = "medium", returnclass = "sf") %>%
dplyr::select(name, iso_a3, pop_est, geometry) %>%
st_transform(crs = 4326) # Leaflet requires lat/lon
})
# 4. Create the joined sf object
cartogram_sf <- reactive({
merged_sf <- world_sf() %>%
left_join(rankings_data(), by = "iso_a3")
# Filter out countries with no data in "Overall"
merged_sf[!is.na(merged_sf$Overall), ]
})
# 5. Create the Leaflet map with OSM tiles
# and dynamically add polygons based on index choice.
# Initialize the leaflet map (empty) so it renders once:
output$cartogramPlot <- renderLeaflet({
leaflet() %>%
addProviderTiles("OpenStreetMap.Mapnik") %>%
setView(lng = 0, lat = 20, zoom = 2) # A broad global view
})
# Observe changes in the chosen index and update polygons + legend
observeEvent(input$indexChoice, {
plot_data <- cartogram_sf()
index_col <- input$indexChoice
# Build a color palette based on the chosen index
pal <- colorNumeric(
palette = viridisLite::viridis(256, begin = 0, end = 0.80),
domain = plot_data[[index_col]],
na.color = "white"
)
# Construct a label/popup-like text
# Construct an indicator function for clarity
starIfSelected <- function(colName) {
if (index_col == colName) "*" else ""
}
# Build a label/popup to show all columns
labels <- sprintf(
paste0(
"<strong>Country:</strong> %s<br/>",
"<strong>%sOverall:</strong> %s<br/>",
"<strong>%sRepresentation Gap:</strong> %s<br/>",
"<strong>%sEthnicity:</strong> %s<br/>",
"<strong>%sGender:</strong> %s<br/>",
"<strong>%sReligion:</strong> %s<br/>",
"<strong>%sLanguage:</strong> %s"
),
# 1. Country
ifelse(is.na(plot_data$Country), "N/A", plot_data$Country),
# 2. Overall
starIfSelected("Overall"),
ifelse(is.na(plot_data$Overall), "N/A", plot_data$Overall),
# 3. RepresentationGap
starIfSelected("RepresentationGap"),
ifelse(is.na(plot_data$RepresentationGap), "N/A", plot_data$RepresentationGap),
# 4. Ethnicity
starIfSelected("Ethnicity"),
ifelse(is.na(plot_data$Ethnicity), "N/A", plot_data$Ethnicity),
# 5. Gender
starIfSelected("Gender"),
ifelse(is.na(plot_data$Gender), "N/A", plot_data$Gender),
# 6. Religion
starIfSelected("Religion"),
ifelse(is.na(plot_data$Religion), "N/A", plot_data$Religion),
# 7. Language
starIfSelected("Language"),
ifelse(is.na(plot_data$Language), "N/A", plot_data$Language)
) %>% lapply(htmltools::HTML)
leafletProxy("cartogramPlot", data = plot_data) %>%
clearShapes() %>% # clear existing polygons
clearControls() %>% # clear existing legends
addPolygons(
fillColor = ~pal(get(index_col)),
fillOpacity = 0.95,
color = "gray",
weight = 0.4,
layerId = ~iso_a3,
label = labels,
highlightOptions = highlightOptions(
color = "white",
weight = 2,
bringToFront = TRUE
)
) %>%
addLegend(
position = "bottomright",
pal = pal,
bins = 5,
values = plot_data[[index_col]],
title = paste(index_col, "Index"),
opacity = 1
)
}, ignoreNULL = FALSE) # Trigger once on startup too
# Track which country was clicked
selected_iso <- reactiveVal(NULL)
observeEvent(input$cartogramPlot_shape_click, {
click <- input$cartogramPlot_shape_click
if (!is.null(click$id)) {
selected_iso(click$id)
}
})
# Reactive to fetch the selected country's data
selected_data <- reactive({
iso <- selected_iso()
if (is.null(iso)) return(NULL)
rankings_data() %>%
filter(iso_a3 == iso)
})
# Render the selected country data in the box
output$selectedCountryData <- renderUI({
if (is.null(selected_data())) {
HTML("<p>Select a country by clicking on the map.</p>")
} else {
data <- selected_data()
html_content <- paste0(
"<b>Country:</b> ", data$Country, "<br/>",
"<b>Overall:</b> ", ifelse(is.na(data$Overall), "N/A", data$Overall), "<br/>",
"<b>Representation Gap:</b> ", ifelse(is.na(data$RepresentationGap), "N/A", data$RepresentationGap), "<br/>",
"<b>Ethnicity:</b> ", ifelse(is.na(data$Ethnicity), "N/A", data$Ethnicity), "<br/>",
"<b>Gender:</b> ", ifelse(is.na(data$Gender), "N/A", data$Gender), "<br/>",
"<b>Religion:</b> ", ifelse(is.na(data$Religion), "N/A", data$Religion), "<br/>",
"<b>Language:</b> ", ifelse(is.na(data$Language), "N/A", data$Language)
)
HTML(html_content)
}
})
}
# =============================
# Launch the Shiny App
# =============================
shinyApp(ui = ui, server = server)
|