-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
343 lines (306 loc) · 13.1 KB
/
app.R
File metadata and controls
343 lines (306 loc) · 13.1 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# ==============================================================================
# Mapping Left-Right Associations - Interactive Visualization
# ==============================================================================
# Shiny application visualizing results from:
# "Mapping left-right associations: a framework using open-ended survey
# responses and political positions" by Lukas Warode (2025)
# ==============================================================================
# ------------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------------
# Set UTF-8 locale for proper German umlaut handling
Sys.setlocale("LC_ALL", "en_US.UTF-8")
# Set to TRUE when real data files are available in the data/ folder
USE_REAL_DATA <- TRUE
# Debug mode - set to FALSE for production deployment
DEBUG_MODE <- FALSE
# ------------------------------------------------------------------------------
# Load Packages
# ------------------------------------------------------------------------------
library(shiny)
library(bslib)
library(plotly)
library(ggplot2)
library(ggrepel)
library(dplyr)
library(tidyr)
library(readr)
library(htmltools)
library(showtext)
library(sysfonts)
# Load Source Sans Pro font for UI elements (with error handling for shinyapps.io)
tryCatch({
font_add_google("Source Sans Pro", "sourcesans")
showtext_auto()
}, error = function(e) {
message("Could not load Google Font via showtext (using CSS fallback): ", e$message)
})
# Optional: shinycssloaders for loading spinners
HAS_SPINNERS <- requireNamespace("shinycssloaders", quietly = TRUE)
if (HAS_SPINNERS) {
library(shinycssloaders)
} else {
# Fallback: create a dummy withSpinner function
withSpinner <- function(ui, ...) ui
}
# ------------------------------------------------------------------------------
# Source Modules and Utilities
# ------------------------------------------------------------------------------
source("R/generate_placeholder_data.R")
source("R/utils_data.R")
source("R/utils_plots.R")
source("R/mod_scatter_plot.R")
source("R/mod_word_details.R")
# ------------------------------------------------------------------------------
# Load Data
# ------------------------------------------------------------------------------
# Load data at startup (either real or placeholder)
app_data <- load_app_data(use_real_data = USE_REAL_DATA)
if (DEBUG_MODE) message(paste("Loaded", nrow(app_data$word_associations), "word associations"))
# ------------------------------------------------------------------------------
# UI Definition
# ------------------------------------------------------------------------------
ui <- page_fluid(
# Theme - Grey/neutral design (blue reserved for semantically right words)
theme = bs_theme(
version = 5,
bootswatch = "flatly",
primary = "#5F6368",
secondary = "#9E9E9E",
success = "#34A853",
warning = "#FBBC05",
danger = "#EA4335",
base_font = font_google("Source Sans Pro"),
heading_font = font_google("Source Sans Pro"),
font_scale = 0.95
),
title = "Mapping Left-Right Associations",
# Custom CSS
tags$head(
# Viewport meta for mobile
tags$meta(name = "viewport", content = "width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"),
tags$link(rel = "stylesheet", type = "text/css", href = "custom.css"),
# Google Fonts: Source Sans Pro for UI
tags$link(
rel = "stylesheet",
href = "https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@300;400;600;700&display=swap"
),
# Bootstrap Icons
tags$link(
rel = "stylesheet",
href = "https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css"
),
# Favicon (optional)
tags$link(rel = "icon", type = "image/x-icon", href = "favicon.ico"),
# JavaScript for clearing selectize and user guide toggle
tags$script(HTML("
Shiny.addCustomMessageHandler('clearSelectize', function(message) {
var selectize = $('#' + message.id)[0];
if (selectize && selectize.selectize) {
selectize.selectize.clear();
}
});
// Toggle aria-expanded on user guide header click
$(document).on('click', '.user-guide-header', function() {
var expanded = $(this).attr('aria-expanded') === 'true';
$(this).attr('aria-expanded', !expanded);
});
"))
),
# Main container
div(
class = "main-container",
# App Header - Grey background, more prominent
div(
class = "app-header",
style = "background: linear-gradient(135deg, #5F6368 0%, #424242 100%); padding: 1.5rem;",
div(
class = "row align-items-center",
div(
class = "col-md-7",
h1("Mapping Left-Right Associations", style = "font-size: 1.8rem; font-weight: 700; margin-bottom: 0.5rem;"),
p(
class = "subtitle mb-0",
style = "font-size: 1rem; opacity: 0.9;",
"Interactive visualization of word associations in political ideology"
)
),
div(
class = "col-md-5 text-md-end mt-3 mt-md-0",
# Citation with Please cite message
div(
style = "background: rgba(255,255,255,0.1); padding: 0.75rem 1rem; border-radius: 8px; display: inline-block;",
tags$span(style = "font-weight: 600; font-size: 0.95rem;", "Lukas Warode (2025)"),
tags$br(),
div(
class = "d-flex align-items-center justify-content-end gap-2 mt-2",
tags$span(style = "font-weight: 400; font-size: 0.85rem; opacity: 0.9;", "Please cite:"),
tags$a(
href = "https://doi.org/10.1057/s41599-025-05679-x",
target = "_blank",
class = "btn btn-outline-light btn-sm",
tags$i(class = "bi bi-journal-text me-1"),
"Paper"
)
)
)
)
)
),
# User Guide - Collapsible on mobile, always visible on desktop
div(
class = "user-guide",
style = "background-color: #f8f9fa; margin-bottom: 1rem; border-radius: 6px; border-left: 4px solid #5F6368;",
# Mobile: clickable header to toggle
div(
class = "user-guide-header d-lg-none",
style = "padding: 0.75rem 1rem; cursor: pointer;",
`data-bs-toggle` = "collapse",
`data-bs-target` = "#userGuideContent",
`aria-expanded` = "false",
div(
class = "d-flex justify-content-between align-items-center",
tags$span(style = "font-size: 0.9rem; font-weight: 500; color: #424242;", "About this visualization"),
tags$i(class = "bi bi-chevron-down", style = "color: #5F6368;")
)
),
# Desktop: always visible header text (no toggle)
div(
class = "d-none d-lg-block",
style = "padding: 0.75rem 1rem;",
tags$p(
class = "mb-0",
style = "font-size: 0.9rem; color: #424242; line-height: 1.5;",
HTML(paste0(
"This visualization maps how political words are associated with 'left' and 'right' based on open-ended survey responses from German electoral candidates (2013\u20132021). ",
"Each point represents a <strong>word association</strong>, not an individual person \u2013 positioned by the average left-right self-placement of respondents who mentioned it. ",
"Words in the <span style='color: #E30019;'>lower-left</span> and <span style='color: #4285F4;'>upper-right</span> quadrants reflect <em>in-ideological</em> associations (alignment with one's political side), while the opposite quadrants show <em>out-ideological</em> associations.",
"<br><br>",
"The panel next to the main figure summarizes each selected word. Position (mean) shows the average left-right self-placement of candidates who used it. The semantic score indicates how left (-1) or right (+1) the word is used. The frequency below shows how often it appeared (left, right, total). The left-right self-placement distribution below is based on individual candidates who used the word and is separated by those who associated it with the left or right."
))
)
),
# Mobile: collapsible content
div(
id = "userGuideContent",
class = "collapse d-lg-none",
style = "padding: 0 1rem 0.75rem 1rem;",
tags$p(
class = "mb-0",
style = "font-size: 0.9rem; color: #424242; line-height: 1.5;",
HTML(paste0(
"This visualization maps how political words are associated with 'left' and 'right' based on open-ended survey responses from German electoral candidates (2013\u20132021). ",
"Each point represents a <strong>word association</strong>, not an individual person \u2013 positioned by the average left-right self-placement of respondents who mentioned it. ",
"Words in the <span style='color: #E30019;'>lower-left</span> and <span style='color: #4285F4;'>upper-right</span> quadrants reflect <em>in-ideological</em> associations (alignment with one's political side), while the opposite quadrants show <em>out-ideological</em> associations.",
"<br><br>",
"The panel next to the main figure summarizes each selected word. Position (mean) shows the average left-right self-placement of candidates who used it. The semantic score indicates how left (-1) or right (+1) the word is used. The frequency below shows how often it appeared (left, right, total). The left-right self-placement distribution below is based on individual candidates who used the word and is separated by those who associated it with the left or right."
))
)
)
),
# Main Content Row
div(
class = "row g-3",
# Left Column: Scatter Plot (larger)
div(
class = "col-lg-8 col-xl-8",
scatterPlotUI("scatter")
),
# Right Column: Details Panel (word details only, no context viewer)
div(
class = "col-lg-4 col-xl-4",
wordDetailsUI("word_details")
)
),
# Footer - Citation with methods description
div(
class = "mt-4 pt-3 border-top",
style = "background-color: #f8f9fa; padding: 1rem; border-radius: 8px;",
div(
class = "row",
div(
class = "col-12",
# Citation with paper icon
div(
class = "mb-2",
tags$span(
tags$i(class = "bi bi-journal-text me-2"),
HTML("<strong>Please cite:</strong>")
)
),
tags$p(
class = "mb-2",
style = "font-size: 0.95rem;",
HTML(paste0(
"Warode, L. (2025). ",
"Mapping left-right associations: a framework using open-ended ",
"survey responses and political positions. ",
"<em>Humanities and Social Sciences Communications</em>, 12:1318. "
)),
tags$a(
href = "https://doi.org/10.1057/s41599-025-05679-x",
target = "_blank",
style = "font-size: 0.9rem;",
"https://doi.org/10.1057/s41599-025-05679-x"
)
),
# Methods paragraph
tags$p(
class = "mb-0 text-muted",
style = "font-size: 0.85rem;",
HTML(paste0(
"<strong>About the data:</strong> ",
"This visualization is based on open-ended survey responses from German electoral candidates ",
"(Candidate Studies 2013, 2017, and 2021). Respondents were asked what they associate with ",
"'left' and 'right' in political terms. Word positions reflect the mean left-right self-placement ",
"of individuals who mentioned each word."
))
)
)
)
)
)
)
# ------------------------------------------------------------------------------
# Server Logic
# ------------------------------------------------------------------------------
server <- function(input, output, session) {
# Reactive: App data (could be made dynamic for filtering in future)
data <- reactive({
app_data
})
# Selected word state - central source of truth
selected_word <- reactiveVal(NULL)
# Scatter Plot Module - returns clicked/selected word
scatter_selection <- scatterPlotServer("scatter", data, selected_word)
# Word Details Module - displays details for selected word
wordDetailsServer("word_details", data, selected_word)
# Update central state when scatter plot selection changes
observeEvent(scatter_selection(), {
scatter_sel <- scatter_selection()
current_sel <- selected_word()
# Handle clear (NULL) case
if (is.null(scatter_sel)) {
if (!is.null(current_sel)) {
selected_word(NULL)
}
return()
}
if (scatter_sel != "" &&
(is.null(current_sel) || scatter_sel != current_sel)) {
selected_word(scatter_sel)
}
}, ignoreInit = TRUE, ignoreNULL = FALSE)
# Debug: Log selected word changes (only in debug mode)
if (DEBUG_MODE) {
observeEvent(selected_word(), {
if (!is.null(selected_word())) {
message(paste("Selected word:", selected_word()))
}
})
}
}
# ------------------------------------------------------------------------------
# Run Application
# ------------------------------------------------------------------------------
shinyApp(ui = ui, server = server)