File size: 3,746 Bytes
8aa102f
 
0dbfc34
 
 
f50f92c
8aa102f
 
 
0dbfc34
8aa102f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0dbfc34
8aa102f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0dbfc34
 
 
 
8aa102f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0dbfc34
8aa102f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0dbfc34
8aa102f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0dbfc34
 
8aa102f
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
# install.packages(c("shiny", "dplyr", "ggplot2", "DT"))

library(shiny)
library(dplyr)
library(ggplot2)
library(data.table)

ui <- fluidPage(
  titlePanel("Country Representation Rankings Explorer"),

  sidebarLayout(
    sidebarPanel(
      h4("Controls"),
      selectInput(
        inputId = "metric",
        label = "Select a Representation Metric:",
        choices = c(
          "Overall", 
          "Representation Gap", 
          "Ethnicity", 
          "Gender", 
          "Religion", 
          "Language"
        ),
        selected = "Overall"
      ),
      checkboxInput(
        inputId = "sort_desc",
        label = "Sort in descending order",
        value = TRUE
      ),
      hr(),
      sliderInput(
        inputId = "top_n",
        label = "Number of Countries to Display in Plot:",
        min = 5,
        max = 50,
        value = 10,
        step = 1
      )
    ),

    mainPanel(
      tabsetPanel(
        tabPanel(
          title = "Plot",
          plotOutput("bar_plot", height = "600px")
        ),
        tabPanel(
          title = "Data Table",
          DTOutput("data_table")
        ),
        tabPanel(
          title = "Summary",
          verbatimTextOutput("summary_text")
        )
      )
    )
  )
)

server <- function(input, output, session) {

  # Load the data (adjust the path as necessary)
  data_raw <- reactive({
    read.csv("CountryRepresentationRankings.csv", stringsAsFactors = FALSE)
  })
  
  # Reactively prepare the data for plotting and table display
  data_prepared <- reactive({
    req(data_raw())
    # Convert to tibble for convenience
    df <- tibble::as_tibble(data_raw())
    
    # Ensure the selected metric is numeric for plotting
    # (This is typically already numeric, but we can do a safe check.)
    # Also handle potential issues with missing values.
    df <- df %>%
      mutate(
        across(all_of(input$metric), as.numeric)
      ) %>%
      filter(!is.na(.data[[input$metric]]))
    
    # Sort ascending or descending
    if (input$sort_desc) {
      df <- df %>% arrange(desc(.data[[input$metric]]))
    } else {
      df <- df %>% arrange(.data[[input$metric]])
    }
    
    # Keep only the top N rows for plotting
    df <- df %>% slice_head(n = input$top_n)
    
    df
  })

  # Render bar plot of the selected metric for top N countries
  output$bar_plot <- renderPlot({
    df <- data_prepared()
    
    # Convert Country to factor for a nice ordered plot
    df <- df %>%
      mutate(Country = factor(Country, levels = Country))
    
    ggplot(df, aes(x = Country, y = .data[[input$metric]])) +
      geom_col(fill = "steelblue") +
      coord_flip() +
      labs(
        x = "Country",
        y = paste0(input$metric, " Index"),
        title = paste("Top", input$top_n, "Countries by", input$metric)
      ) +
      theme_minimal(base_size = 14)
  })
  
  # Render data table of the entire dataset
  output$data_table <- renderDT({
    req(data_raw())
    datatable(
      data_raw(),
      options = list(pageLength = 10, autoWidth = TRUE),
      filter = "top",
      rownames = FALSE
    )
  })

  # Render summary statistics text for the selected metric
  output$summary_text <- renderPrint({
    df <- data_raw()
    req(df)
    # Convert the chosen metric to numeric if needed
    df[[input$metric]] <- suppressWarnings(as.numeric(df[[input$metric]]))
    
    valid_metric <- df[[input$metric]][!is.na(df[[input$metric]])]
    if (length(valid_metric) == 0) {
      cat("No valid numeric data for the selected metric.")
    } else {
      summary_stats <- summary(valid_metric)
      cat("Summary of", input$metric, "across all countries:\n")
      print(summary_stats)
    }
  })
}

shinyApp(ui = ui, server = server)