cjerzak's picture
Update app.R
f50f92c verified
raw
history blame
3.75 kB
# 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)