-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
538 lines (493 loc) · 15.2 KB
/
app.R
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
# run with shiny::runApp('app.R')
#
library(shiny)
library(knitr)
library(DescTools)
library(data.table)
options_up_to_ordinal <- c(
"Geschlecht",
"Klasse",
"Einstiegshafen",
"Kabinenbuchstabe",
"Kinder und Eltern an Bord",
"Geschwister und Ehepartner an Bord",
"Familienmitglieder an Bord"
)
options_stetig <- c(
"Alter",
"Ticketpreis"
)
options <- c(options_up_to_ordinal, options_stetig)
switch_options <- function(data, selected_option, breaks = NULL) {
cabin_letters <- gsub("[0-9]", "", data$Cabin)
cabin_letter <- substr(cabin_letters, 1, 1)
if (is.null(breaks)) {
cut_alter <- data$Age
cut_fare <- data$Fare
cut_sibsp <- data$SibSp
cut_parch <- data$Parch
} else {
cut_alter <- cut(
data$Age,
breaks = breaks,
dig.lab = 2
)
cut_fare <- cut(data$Fare, breaks = breaks)
cut_sibsp <- cut(data$SibSp, breaks = breaks)
cut_parch <- cut(data$Parch, breaks = breaks)
}
switch(selected_option,
"Alter" = cut_alter,
"Klasse" = data$Pclass,
"Geschlecht" = data$Sex,
"Kinder und Eltern an Bord" = cut_parch,
"Geschwister und Ehepartner an Bord" = cut_sibsp,
"Familienmitglieder an Bord" = cut_sibsp + cut_parch,
"Einstiegshafen" = data$Embarked,
"Ticketpreis" = cut_fare,
"Kabinenbuchstabe" = cabin_letter,
"Überlebt" = data$Survived
)
}
######################################################
##################### UI #############################
######################################################
# Define UI for application that draws a histogram
ui <- fluidPage(
navbarPage(
"Titanic Überlebensstatistik",
navbarMenu(
"Startseite",
tabPanel(
"Tabelle",
sidebarLayout(
sidebarPanel(
h4("Tabelle der Daten"),
p("Survived: Überlebt (1) oder Gestorben (0)"),
p("Pclass: Kabinenklasse (1 = 1. Klasse, 2 = 2. Klasse, 3 = 3. Klasse)"),
p("SibSp: Anzahl der Geschwister und Ehepartner an Bord"),
p("Parch: Anzahl der Kinder und Eltern an Bord"),
p("Ticket: Ticketnummer"),
p("Fare: Ticketpreis"),
p("Cabin: Kabinennummer"),
p("Embarked: Einstiegshafen (C = Cherbourg, Q = Queenstown, S = Southampton)"),
selectInput(
"survived",
"Überlebt",
choices = c("Überlebt" = TRUE, "Gestorben" = FALSE),
multiple = TRUE,
),
selectInput(
"pclass",
"Klasse",
choices = c(
"1. Klasse" = "1",
"2. Klasse" = "2",
"3. Klasse" = "3"
),
multiple = TRUE,
),
selectInput(
"sex",
"Geschlecht",
choices = c("männlich" = "male", "weiblich" = "female"),
multiple = TRUE,
),
sliderInput("fares",
"Ticketpreis",
min = 0,
max = 513,
value = c(0, 513),
step = 1
),
selectInput(
"harbor",
"Einstiegshafen",
choices = c(
"Cherbourg" = "C",
"Queenstown" = "Q",
"Southampton" = "S"
),
multiple = TRUE
)
),
mainPanel(
dataTableOutput("dataTable")
)
)
),
tabPanel(
"Übersicht",
sidebarLayout(
sidebarPanel(
sliderInput("gruppen",
"Größe der Altersgruppen",
min = 5,
max = 30,
value = 20,
step = 5
)
),
mainPanel(
plotOutput("survival"),
plotOutput("altersverteilungHist"),
plotOutput("survivalClassBarplot"),
)
)
),
tabPanel(
"Zusammenfassungen",
sidebarLayout(
sidebarPanel(
selectInput(
"selected_dimension",
"Merkmal",
choices = c("Überlebt", options_up_to_ordinal),
multiple = FALSE
)
),
mainPanel(
verbatimTextOutput("summary"),
tableOutput("proportions_and_absolute_numbers")
)
)
)
),
navbarMenu(
"Kombination von Merkmalen",
tabPanel(
"nach Überleben",
sidebarLayout(
sidebarPanel(
selectInput(
"additional_dimension",
"zweites Merkmal",
choices = options,
multiple = FALSE
),
uiOutput("slider"),
uiOutput("checkbox_agegroup_only"),
),
mainPanel(
plotOutput("two_dim_mosaic"),
plotOutput("barplot_on_survival"),
"(Kardinalskalierte Merkmale werden gerundet)",
htmlOutput("zusätzliche_bilder"),
tableOutput("contingency_table")
)
)
),
tabPanel(
"Kombination im Mosaikdiagramm",
sidebarLayout(
sidebarPanel(
selectInput(
"first_dimension_mosaic",
"erstes Merkmal",
choices = options_up_to_ordinal,
multiple = FALSE,
selected = "Einstiegshafen"
),
selectInput(
"second_dimension_mosaic",
"zweites Merkmal",
choices = options_up_to_ordinal,
multiple = FALSE,
selected = "Klasse"
)
),
mainPanel(
plotOutput("mosaicplot")
)
)
),
tabPanel(
"Kombination im Streudiagramm",
sidebarLayout(
sidebarPanel(
selectInput(
"first_dimension_scatter",
"erstes Merkmal",
choices = options_stetig,
multiple = FALSE
),
selectInput(
"second_dimension_scatter",
"zweites Merkmal",
choices = rev(options_stetig),
multiple = FALSE
)
),
mainPanel(
plotOutput("scatterplot"),
)
)
)
),
tabPanel(
"Kontingenzmaße",
sidebarLayout(
sidebarPanel(
selectInput(
"contingencyDimensions",
"Merkmale",
choices = options,
multiple = TRUE,
selected = c(
"Ticketpreis",
"Geschlecht",
"Alter",
"Klasse"
)
)
),
mainPanel(
plotOutput("contingency")
)
)
)
)
)
######################################################
##################### Server #########################
######################################################
# Define server logic required to draw a histogram
server <- function(input, output) {
data <- read.csv2("titanic_data.csv", sep = ",", dec = ".")
data$Survived <- as.logical(data$Survived)
################# Startseite #####################
output$survival <- renderPlot({
barplot(
prop.table(table(data$Survived)),
main = "Überlebensstatistik",
ylab = "Schicksal",
xlab = "Relative Häufigkeit",
names.arg = c("Gestorben", "Überlebt"),
horiz = TRUE,
beside = FALSE,
col = c("#b9b9b953", "#00ff003e")
)
})
output$survivalClassBarplot <- renderPlot({
prop_survival_class <- prop.table(table(data$Survived, data$Pclass), 1)
library(knitr)
kable(prop_survival_class)
barplot(prop_survival_class,
main = "Kabinenklasse mit Anteilen der Überlebenden",
ylab = "Relative Häufigkeit",
xlab = "Klasse",
legend = c("Gestorben", "Überlebt"),
col = c("#b9b9b953", "#00ff003e")
)
})
output$altersverteilungHist <- renderPlot({
breaks <- seq(0, 100, input$gruppen)
hist(
data$Age,
main = "Histogramm der Altersverteilung der Passagiere",
xlab = "Alter", ylab = "Häufigkeit",
breaks = breaks
)
})
################# Tabelle #####################
output$dataTable <- renderDataTable({
filtered_data <- data
if (!is.null(input$survived)) {
filtered_data <- filtered_data[data$Survived %in% input$survived, ]
}
if (!is.null(input$fares)) {
filtered_data <- filtered_data[data$Fare >= input$fares[1], ]
filtered_data <- filtered_data[data$Fare <= input$fares[2], ]
}
if (!is.null(input$sex)) {
filtered_data <- filtered_data[data$Sex %in% input$sex, ]
}
if (!is.null(input$pclass)) {
filtered_data <- filtered_data[data$Pclass %in% input$pclass, ]
}
if (!is.null(input$harbor)) {
filtered_data <- filtered_data[data$Embarked %in% input$harbor, ]
}
filtered_data
})
################# Summary einzelner Merkmale #####################
## as in https://stackoverflow.com/a/47206071
output$summary <- renderPrint({
this_data <- switch_options(data, input$selected_dimension)
summary(this_data)
})
output$proportions_and_absolute_numbers <- renderTable({
this_data <- switch_options(data, input$selected_dimension)
h_table <- table(this_data)
p_table <- prop.table(table(this_data))
combined_table <- cbind(h_table, p_table)
combined_table <- data.frame(RowNames = rownames(combined_table), combined_table)
colnames(combined_table) <- c("Merkmal", "Häufigkeit", "relative Häufigkeit")
combined_table$Häufigkeit <- as.integer(combined_table$Häufigkeit)
combined_table
})
############### X unter der Bedingung Überleben #######################
output$slider <- renderUI({
if (input$additional_dimension %in% options_stetig) {
sliderInput(
"breaks",
"Anzahl der Gruppen:",
min = 2,
max = 30,
value = 8,
step = 1
)
}
})
output$zusätzliche_bilder <- renderText(
if(input$additional_dimension == "Kabinenbuchstabe") {
'<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Titanic_cutaway_diagram.png/800px-Titanic_cutaway_diagram.png" alt="Titanic cutaway diagram" width="500" height="500">'
}
)
output$two_dim_mosaic <- renderPlot({
filtered_data <- data
if (
input$additional_dimension == "Kinder und Eltern an Bord" ||
input$additional_dimension == "Geschwister und Ehepartner an Bord" ||
input$additional_dimension == "Familienmitglieder an Bord") {
if (input$only_selected_agegroup == "Nur Kinder einbeziehen") {
filtered_data <- subset(filtered_data, Age <= 18)
} else if (input$only_selected_agegroup == "Nur Erwachsene einbeziehen") {
filtered_data <- subset(filtered_data, Age > 18)
}
}
mosaic_data <- switch_options(filtered_data, input$additional_dimension, breaks = input$breaks)
mosaicplot(
table(
mosaic_data,
filtered_data$Survived
),
main = paste(
"Mosaikplot Überleben in Abhängigkeit von",
input$additional_dimension
),
xlab = input$additional_dimension,
ylab = "Überlebt",
color = TRUE
)
})
output$checkbox_agegroup_only <- renderUI({
altersfiltergruppen <- c("Alle einbeziehen", "Nur Kinder einbeziehen", "Nur Erwachsene einbeziehen")
if (input$additional_dimension == "Kinder und Eltern an Bord" ||
input$additional_dimension == "Geschwister und Ehepartner an Bord" ||
input$additional_dimension == "Familienmitglieder an Bord") {
radioButtons(
"only_selected_agegroup",
"Nur Kinder oder Erwachsene einbeziehen (Kinder: Age < 18)",
choices = altersfiltergruppen,
selected = NULL,
)
}
})
output$barplot_on_survival <- renderPlot({
filtered_data <- data
if (
input$additional_dimension == "Kinder und Eltern an Bord" ||
input$additional_dimension == "Geschwister und Ehepartner an Bord" ||
input$additional_dimension == "Familienmitglieder an Bord") {
if (input$only_selected_agegroup == "Nur Kinder einbeziehen") {
filtered_data <- subset(filtered_data, Age <= 18)
} else if (input$only_selected_agegroup == "Nur Erwachsene einbeziehen") {
filtered_data <- subset(filtered_data, Age > 18)
}
}
this_data <- switch_options(filtered_data, input$additional_dimension, breaks = input$breaks)
this_survival <- factor(filtered_data$Survived, levels = c(TRUE, FALSE))
barplot(
prop.table(table(this_survival, this_data), 2),
main = paste(
"Überlebensstatistik in Abhängigkeit von",
input$additional_dimension
),
ylab = "Relative Häufigkeit von Überleben",
xlab = input$additional_dimension,
beside = FALSE,
col = c("#00ff003e", "#b9b9b953"),
legend = (c("Überlebt", "Gestorben"))
)
})
output$contingency_table <- renderTable(
{
this_data <- switch_options(data, input$additional_dimension, breaks = input$breaks)
this_survival <- data$Survived
frame <- as.data.frame.matrix(prop.table(table(this_data, this_survival), 1))
colnames(frame) <- c("Gestorben", "Überlebt")
frame
},
rownames = TRUE
)
################ Streudiagramme ######################
data_first_selected_dim <- reactive({
switch_options(data, input$first_dimension_scatter)
})
data_second_selected_dim <- reactive({
switch_options(data, input$second_dimension_scatter)
})
output$scatterplot <- renderPlot(
plot(
data_first_selected_dim(),
data_second_selected_dim(),
main = paste(
"Streudiagramm von",
input$first_dimension_scatter,
"und",
input$second_dimension_scatter
),
xlab = input$first_dimension_scatter,
ylab = input$second_dimension_scatter
),
)
################### Mosaikplots ####################
data_first_selected_dim_mosaic <- reactive({
switch_options(data, input$first_dimension_mosaic)
})
data_second_selected_dim_mosaic <- reactive({
switch_options(data, input$second_dimension_mosaic)
})
output$mosaicplot <- renderPlot({
mosaicplot(
table(
data_first_selected_dim_mosaic(),
data_second_selected_dim_mosaic()
),
main = paste(
"Mosaikplot",
input$first_dimension_mosaic,
"in Abhängigkeit von",
input$second_dimension_mosaic
),
xlab = input$first_dimension_mosaic,
ylab = input$second_dimension_mosaic,
color = TRUE
)
})
################## Kontingenzmaße #####################
output$contingency <- renderPlot({
cramer_v_values <- sapply(input$contingencyDimensions, function(dim) {
selected_dim <- switch_options(data, dim)
CramerV(data$Survived, selected_dim)
})
barplot(
cramer_v_values,
xlab = "Cramer V",
ylab = "Merkmale",
main = "Kontingenzmaße vom Überleben (Cramer V)",
xlim = c(0, 1),
horiz = TRUE
)
})
}
# Run the application
shinyApp(ui = ui, server = server)