cjerzak's picture
Update app.R
a0659af verified
raw
history blame
7.62 kB
# setwd("~/Downloads")
options(error = NULL)
library(shiny)
library(shinydashboard)
library(dplyr)
library(readr)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(countrycode)
library(ggplot2)
library(ggiraph)
# =============================
# UI
# =============================
ui <- dashboardPage(
skin = "black",
dashboardHeader(
title = span(
style = "font-weight: 600; font-size: 18px;",
"Country Representation"
)
),
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"
)
)
),
dashboardBody(
tags$head(
tags$link(
href = "https://fonts.googleapis.com/css2?family=OCR+A+Extended&display=swap",
rel = "stylesheet"
),
tags$style(HTML("
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: #ffffff !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-color: #344e5c !important;
border-left-color: #78cdd7 !important;
color: #ffffff !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,0.1);
border-radius: 6px;
}
.box.box-solid > .box-header {
background-color: #204051;
color: #fff;
border-radius: 6px 6px 0 0;
}
.box .box-body {
padding: 0 !important;
}
.small, small {
font-size: 75%;
}
"))
),
tabItems(
tabItem(
tabName = "cartogramTab",
fluidRow(
box(
width = 12,
title = strong("Global Leadership Project (GLP)"),
solidHeader = TRUE,
div(
style = "height: 80vh; padding: 10px;",
girafeOutput("cartogramPlot", width = "100%", height = "100%")
)
),
box(
width = 12,
title = "Country Details",
verbatimTextOutput("countryDetails")
)
)
)
)
)
)
# =============================
# SERVER
# =============================
server <- function(input, output, session) {
custom_iso_matches <- c("Kosovo" = "XKX", "Somaliland" = "SOM")
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
))
})
world_sf <- reactive({
ne_countries(scale = "medium", returnclass = "sf") %>%
dplyr::select(name, iso_a3, pop_est, geometry) %>%
st_transform(crs = "ESRI:54009")
})
cartogram_sf <- reactive({
merged_sf <- world_sf() %>%
left_join(rankings_data(), by = "iso_a3")
merged_sf[!is.na(merged_sf$Overall),]
})
output$cartogramPlot <- renderGirafe({
req(input$indexChoice)
plot_data <- cartogram_sf()
index_col <- input$indexChoice
plot_data$tooltip_text <- paste0(
"<b>Country:</b> ", plot_data$Country, "<br/>",
"<b>Overall:</b> ", ifelse(!is.na(plot_data$Overall), plot_data$Overall, "N/A"), "<br/>",
"<b>Representation Gap:</b> ", ifelse(!is.na(plot_data$RepresentationGap), plot_data$RepresentationGap, "N/A"), "<br/>",
"<b>Ethnicity:</b> ", ifelse(!is.na(plot_data$Ethnicity), plot_data$Ethnicity, "N/A"), "<br/>",
"<b>Gender:</b> ", ifelse(!is.na(plot_data$Gender), plot_data$Gender, "N/A"), "<br/>",
"<b>Religion:</b> ", ifelse(!is.na(plot_data$Religion), plot_data$Religion, "N/A"), "<br/>",
"<b>Language:</b> ", ifelse(!is.na(plot_data$Language), plot_data$Language, "N/A")
)
p <- ggplot(plot_data) +
geom_sf_interactive(
aes(
fill = get(index_col),
tooltip = tooltip_text,
data_id = iso_a3
),
color = "grey20",
size = 0.1
) +
scale_fill_viridis_c(option = "D", na.value = "white") +
coord_sf(expand = FALSE) +
theme_void(base_size = 14, base_family = "sans") +
labs(
fill = paste(index_col, "Index"),
title = "Country-level Representation",
subtitle = "Map Colored by Selected Representation Index",
caption = "Source: Global Leadership Project (GLP) & Natural Earth"
) +
theme(
plot.title = element_text(face = "bold", hjust = 0.5, size = 20),
plot.subtitle = element_text(hjust = 0.5, size = 14),
plot.caption = element_text(hjust = 1, size = 10),
legend.position = "bottom",
legend.direction = "horizontal",
legend.key.width = unit(2, "cm")
)
girafe(
ggobj = p,
width_svg = 10,
height_svg = 6,
options = list(
opts_tooltip(
css = "background-color: white;
font-family: 'OCR A Extended', monospace;
color: black;"
),
opts_selection(
type = "single",
css = "stroke:yellow;stroke-width:2px;"
)
)
)
})
selected_country_data <- reactive({
selected_iso <- input$cartogramPlot_selected
if (length(selected_iso) == 0) {
return(NULL)
}
plot_data <- cartogram_sf()
country_data <- plot_data %>% filter(iso_a3 == selected_iso)
if (nrow(country_data) == 0) {
return(NULL)
}
country_data
})
output$countryDetails <- renderText({
country_data <- selected_country_data()
if (is.null(country_data)) {
return("Click on a country to see details.")
}
paste0(
"Country: ", country_data$Country, "\n",
"Overall: ", ifelse(is.na(country_data$Overall), "N/A", country_data$Overall), "\n",
"Representation Gap: ", ifelse(is.na(country_data$RepresentationGap), "N/A", country_data$RepresentationGap), "\n",
"Ethnicity: ", ifelse(is.na(country_data$Ethnicity), "N/A", country_data$Ethnicity), "\n",
"Gender: ", ifelse(is.na(country_data$Gender), "N/A", country_data$Gender), "\n",
"Religion: ", ifelse(is.na(country_data$Religion), "N/A", country_data$Religion), "\n",
"Language: ", ifelse(is.na(country_data$Language), "N/A", country_data$Language)
)
})
}
# =============================
# Launch the Shiny App
# =============================
shinyApp(ui = ui, server = server)