cjerzak commited on
Commit
8aa102f
·
verified ·
1 Parent(s): 8797ce2

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +132 -45
app.R CHANGED
@@ -1,58 +1,145 @@
 
 
1
  library(shiny)
2
- library(bslib)
3
  library(dplyr)
4
  library(ggplot2)
 
 
 
 
5
 
6
- df <- readr::read_csv("penguins.csv")
7
- # Find subset of columns that are suitable for scatter plot
8
- df_num <- df |> select(where(is.numeric), -Year)
9
-
10
- ui <- page_sidebar(
11
- theme = bs_theme(bootswatch = "minty"),
12
- title = "Penguins explorer",
13
- sidebar = sidebar(
14
- varSelectInput("xvar", "X variable", df_num, selected = "Bill Length (mm)"),
15
- varSelectInput("yvar", "Y variable", df_num, selected = "Bill Depth (mm)"),
16
- checkboxGroupInput("species", "Filter by species",
17
- choices = unique(df$Species), selected = unique(df$Species)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  ),
19
- hr(), # Add a horizontal rule
20
- checkboxInput("by_species", "Show species", TRUE),
21
- checkboxInput("show_margins", "Show marginal plots", TRUE),
22
- checkboxInput("smooth", "Add smoother"),
23
- ),
24
- plotOutput("scatter")
 
 
 
 
 
 
 
 
 
 
 
 
25
  )
26
 
27
  server <- function(input, output, session) {
28
- subsetted <- reactive({
29
- req(input$species)
30
- df |> filter(Species %in% input$species)
31
- })
32
 
33
- output$scatter <- renderPlot(
34
- {
35
- p <- ggplot(subsetted(), aes(!!input$xvar, !!input$yvar)) +
36
- theme_light() +
37
- list(
38
- theme(legend.position = "bottom"),
39
- if (input$by_species) aes(color = Species),
40
- geom_point(),
41
- if (input$smooth) geom_smooth()
42
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- if (input$show_margins) {
45
- margin_type <- if (input$by_species) "density" else "histogram"
46
- p <- p |> ggExtra::ggMarginal(
47
- type = margin_type, margins = "both",
48
- size = 8, groupColour = input$by_species, groupFill = input$by_species
49
- )
50
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- p
53
- },
54
- res = 100
55
- )
 
 
 
 
 
 
 
 
 
 
 
 
56
  }
57
 
58
- shinyApp(ui, server)
 
1
+ # install.packages(c("shiny", "dplyr", "ggplot2", "DT"))
2
+
3
  library(shiny)
 
4
  library(dplyr)
5
  library(ggplot2)
6
+ library(DT)
7
+
8
+ ui <- fluidPage(
9
+ titlePanel("Country Representation Rankings Explorer"),
10
 
11
+ sidebarLayout(
12
+ sidebarPanel(
13
+ h4("Controls"),
14
+ selectInput(
15
+ inputId = "metric",
16
+ label = "Select a Representation Metric:",
17
+ choices = c(
18
+ "Overall",
19
+ "Representation Gap",
20
+ "Ethnicity",
21
+ "Gender",
22
+ "Religion",
23
+ "Language"
24
+ ),
25
+ selected = "Overall"
26
+ ),
27
+ checkboxInput(
28
+ inputId = "sort_desc",
29
+ label = "Sort in descending order",
30
+ value = TRUE
31
+ ),
32
+ hr(),
33
+ sliderInput(
34
+ inputId = "top_n",
35
+ label = "Number of Countries to Display in Plot:",
36
+ min = 5,
37
+ max = 50,
38
+ value = 10,
39
+ step = 1
40
+ )
41
  ),
42
+
43
+ mainPanel(
44
+ tabsetPanel(
45
+ tabPanel(
46
+ title = "Plot",
47
+ plotOutput("bar_plot", height = "600px")
48
+ ),
49
+ tabPanel(
50
+ title = "Data Table",
51
+ DTOutput("data_table")
52
+ ),
53
+ tabPanel(
54
+ title = "Summary",
55
+ verbatimTextOutput("summary_text")
56
+ )
57
+ )
58
+ )
59
+ )
60
  )
61
 
62
  server <- function(input, output, session) {
 
 
 
 
63
 
64
+ # Load the data (adjust the path as necessary)
65
+ data_raw <- reactive({
66
+ read.csv("CountryRepresentationRankings.csv", stringsAsFactors = FALSE)
67
+ })
68
+
69
+ # Reactively prepare the data for plotting and table display
70
+ data_prepared <- reactive({
71
+ req(data_raw())
72
+ # Convert to tibble for convenience
73
+ df <- tibble::as_tibble(data_raw())
74
+
75
+ # Ensure the selected metric is numeric for plotting
76
+ # (This is typically already numeric, but we can do a safe check.)
77
+ # Also handle potential issues with missing values.
78
+ df <- df %>%
79
+ mutate(
80
+ across(all_of(input$metric), as.numeric)
81
+ ) %>%
82
+ filter(!is.na(.data[[input$metric]]))
83
+
84
+ # Sort ascending or descending
85
+ if (input$sort_desc) {
86
+ df <- df %>% arrange(desc(.data[[input$metric]]))
87
+ } else {
88
+ df <- df %>% arrange(.data[[input$metric]])
89
+ }
90
+
91
+ # Keep only the top N rows for plotting
92
+ df <- df %>% slice_head(n = input$top_n)
93
+
94
+ df
95
+ })
96
 
97
+ # Render bar plot of the selected metric for top N countries
98
+ output$bar_plot <- renderPlot({
99
+ df <- data_prepared()
100
+
101
+ # Convert Country to factor for a nice ordered plot
102
+ df <- df %>%
103
+ mutate(Country = factor(Country, levels = Country))
104
+
105
+ ggplot(df, aes(x = Country, y = .data[[input$metric]])) +
106
+ geom_col(fill = "steelblue") +
107
+ coord_flip() +
108
+ labs(
109
+ x = "Country",
110
+ y = paste0(input$metric, " Index"),
111
+ title = paste("Top", input$top_n, "Countries by", input$metric)
112
+ ) +
113
+ theme_minimal(base_size = 14)
114
+ })
115
+
116
+ # Render data table of the entire dataset
117
+ output$data_table <- renderDT({
118
+ req(data_raw())
119
+ datatable(
120
+ data_raw(),
121
+ options = list(pageLength = 10, autoWidth = TRUE),
122
+ filter = "top",
123
+ rownames = FALSE
124
+ )
125
+ })
126
 
127
+ # Render summary statistics text for the selected metric
128
+ output$summary_text <- renderPrint({
129
+ df <- data_raw()
130
+ req(df)
131
+ # Convert the chosen metric to numeric if needed
132
+ df[[input$metric]] <- suppressWarnings(as.numeric(df[[input$metric]]))
133
+
134
+ valid_metric <- df[[input$metric]][!is.na(df[[input$metric]])]
135
+ if (length(valid_metric) == 0) {
136
+ cat("No valid numeric data for the selected metric.")
137
+ } else {
138
+ summary_stats <- summary(valid_metric)
139
+ cat("Summary of", input$metric, "across all countries:\n")
140
+ print(summary_stats)
141
+ }
142
+ })
143
  }
144
 
145
+ shinyApp(ui = ui, server = server)