|
|
|
|
|
|
|
library(shiny) |
|
library(readr) |
|
library(dplyr) |
|
library(ggplot2) |
|
library(DT) |
|
|
|
|
|
ui <- fluidPage( |
|
|
|
|
|
titlePanel("Country Representation Explorer"), |
|
|
|
|
|
sidebarLayout( |
|
sidebarPanel( |
|
|
|
selectInput( |
|
inputId = "metric", |
|
label = "Choose representation metric:", |
|
choices = c("Overall", "Representation Gap", "Ethnicity", |
|
"Gender", "Religion", "Language"), |
|
selected = "Overall" |
|
), |
|
|
|
|
|
sliderInput( |
|
inputId = "metricRange", |
|
label = "Filter countries by metric range:", |
|
min = -10, |
|
max = 10, |
|
value = c(-10, 10), |
|
step = 0.1 |
|
), |
|
|
|
|
|
checkboxInput( |
|
inputId = "showDataTable", |
|
label = "Show data table of filtered results", |
|
value = TRUE |
|
) |
|
), |
|
|
|
|
|
mainPanel( |
|
|
|
plotOutput("repPlot", height = "600px"), |
|
|
|
|
|
conditionalPanel( |
|
condition = "input.showDataTable == true", |
|
DTOutput("tableOut") |
|
) |
|
) |
|
) |
|
) |
|
|
|
|
|
server <- function(input, output, session) { |
|
|
|
|
|
|
|
full_data <- reactive({ |
|
read_csv("CountryRepresentationRankings.csv", show_col_types = FALSE) |
|
}) |
|
|
|
|
|
filtered_data <- reactive({ |
|
req(input$metricRange) |
|
req(input$metric) |
|
|
|
|
|
data <- full_data() |
|
|
|
|
|
data %>% |
|
filter( |
|
|
|
get(input$metric) >= input$metricRange[1] & |
|
get(input$metric) <= input$metricRange[2] |
|
) |
|
}) |
|
|
|
|
|
output$repPlot <- renderPlot({ |
|
data_for_plot <- filtered_data() |
|
|
|
|
|
validate( |
|
need(nrow(data_for_plot) > 0, "No data available within this range.") |
|
) |
|
|
|
|
|
ggplot(data_for_plot, aes( |
|
x = reorder(Country, get(input$metric)), |
|
y = get(input$metric) |
|
)) + |
|
geom_col(fill = "steelblue") + |
|
coord_flip() + |
|
labs( |
|
x = "Country", |
|
y = paste(input$metric, "Index"), |
|
title = paste("Countries by", input$metric, "Representation") |
|
) + |
|
theme_minimal(base_size = 14) |
|
}) |
|
|
|
|
|
output$tableOut <- renderDT({ |
|
data_for_table <- filtered_data() |
|
datatable( |
|
data_for_table, |
|
options = list(pageLength = 10, autoWidth = TRUE), |
|
rownames = FALSE |
|
) |
|
}) |
|
} |
|
|
|
|
|
shinyApp(ui, server) |
|
|