-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsb_server.R
618 lines (513 loc) · 29.4 KB
/
dsb_server.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
server <- shinyServer(function(input,output,session){
wd <<- getwd()
## DEPENDENCIES
source("factor_to_integer.R")
source("namegetter.R")
source("sort_signifiers.R")
source("unit2perc.R")
## CHOOSE DATASET
observeEvent(input$dataset,{
dataset <- reactive({switch(input$dataset,
"Moldova" = "moldova",
"Kyrgyzstan UNDP" = "kyrgyzstan",
"Kyrgyzstan Unicef" = "unicef",
"Serbia" = "serbia",
"Yemen" = "yemen",
"Tajikistan" = "tajikistan")})
if (exists("clean_bin", where = .GlobalEnv)) rm(clean_bin, envir = .GlobalEnv)
load(paste0(wd,"/clean_data/",dataset(),"_clean.RData"))
## PREPROCESSING
clean_bin <- factor2int(clean, integrate = TRUE, ignore = 20, keepFactor = FALSE)
## Remove remaining factors ("Other", Q7.Score)
a <- rep(NA,dim(clean_bin)[2])
for (i in 1:dim(clean_bin)[2]){
if (((is.factor(clean_bin[,i])) &&
((length(grep("^Q[0-9]",names(clean_bin)[i]))) ||
(length(grep("^DQ[0-9]",names(clean_bin)[i]))))) ||
(length(grep("Score$",names(clean_bin)[i])))){
a[i] <- i
}
}
a <- a[!is.na(a)]
clean_bin <- clean_bin[,-a]
## Sort by signifier type
sigtypes(clean_bin) # Sig types so stones, triads, and dyads dropdowns work.
# for (i in 1:length(types)){
# assign(names(types)[i],types[[i]], envir = parent.frame(n = 1))
# }
# rm(types)
## Namegetter
factor_names <- namegetter(clean_bin)
qnames <- factor_names[[1]]
dqnames <- factor_names[[2]]
## TIER 2 OPTIONS - SELECT CONTINOUS/FACTOR VARIABLES
output$x1out <<- renderUI({switch(input$x1,
"Triads" = sidebarPanel(width = 12,
selectInput(inputId = "x_t1",
label = "X Triad 1",
choices = c("Select",names(triads))),
sliderInput(inputId = "xt_sld",
label = "Choose Range",
min = 0, max = 10, value = c(0,6), step = 0.5,
dragRange = TRUE)),
"Dyads" = sidebarPanel(width = 12,
selectInput(inputId = "x_d1",
label = "X Dyad 1",
choices = c("Select",names(dyads))),
sliderInput(inputId = "xd_sld",
label = "Choose Range",
min = 0, max = 10, value = c(0,6), step = 0.5,
dragRange = TRUE)),
"Stones" = sidebarPanel(width = 12,
selectInput(inputId = "x_s1",
label = "X Stone 1",
choices = c("Select",names(stones))),
sliderInput(inputId = "xs_sld",
label = "Choose Range",
min = 0, max = 10, value = c(0,6), step = 0.5,
dragRange = TRUE)),
"Questions" = selectInput(inputId = "x_q1",
label = "Questions",
choices = c("Select",qnames),
selected = "Select"),
"Descriptors" = selectInput(inputId = "x_dq1",
label = "Descriptors",
choices = c("Select",dqnames),
selected = "Select")
)
})
output$y1out <<- renderUI({switch(input$y1,
"Triads" = sidebarPanel(width = 12,
selectInput(inputId = "y_t1",
label = "Y Triad 1",
choices = c("Select",names(triads))),
sliderInput(inputId = "yt_sld",
label = "Choose Range",
min = 0, max = 10, value = c(0,6), step = 0.5,
dragRange = TRUE)),
"Dyads" = sidebarPanel(width = 12,
selectInput(inputId = "y_d1",
label = "Y Dyad 1",
choices = c("Select",names(dyads))),
sliderInput(inputId = "yd_sld",
label = "Choose Range",
min = 0, max = 10, value = c(0,6), step = 0.5,
dragRange = TRUE)),
"Stones" = sidebarPanel(width = 12,
selectInput(inputId = "y_s1",
label = "Y Stone 1",
choices = c("Select",names(stones))),
sliderInput(inputId = "ys_sld",
label = "Choose Range",
min = 0, max = 10, value = c(0,6), step = 0.5,
dragRange = TRUE)),
"Questions" = selectInput(inputId = "y_q1",
label = "Questions",
choices = c("Select",qnames),
selected = "Select"),
"Descriptors" = selectInput(inputId = "y_dq1",
label = "Descriptors",
choices = c("Select",dqnames),
selected = "Select")
)
})
## TIER 3 OPTIONS - SELECT FACTOR LEVELS
if (exists("list_qnames")) {rm(list_qnames)}
list_qnames <- "Select"
for (i in 1:length(qnames)){
list_qnames <- c(list_qnames,assign(qnames[i],paste0(i)))
}
names(list_qnames) <- c("Select",qnames)
if (exists("list_dqnames")) {rm(list_dqnames)}
list_dqnames <- "Select"
for (i in 1:length(dqnames)){
list_dqnames <- c(list_dqnames,assign(dqnames[i],paste0(i)))
}
names(list_dqnames) <- c("Select",dqnames)
## Rows
x_q1 <- renderText({list_qnames[match.arg(input$x_q1,c("Select",names(list_qnames)))]})
x_dq1 <- renderText({list_dqnames[match.arg(input$x_dq1,c("Select",names(list_dqnames)))]})
output$x_q1_factor <- renderUI({sidebarPanel(
checkboxInput(inputId = "xq_all","All/None"),
checkboxGroupInput(inputId = "x_q1_level",
label = "Factor Level",
choices = names(clean_bin[,grep(paste0("^Q",x_q1(),"."),names(clean_bin),value = TRUE)])))
})
output$x_dq1_factor <- renderUI({sidebarPanel(
checkboxInput(inputId = "xdq_all","All/None"),
checkboxGroupInput(inputId = "x_dq1_level",
label = "Factor Level",
choices = names(clean_bin[,grep(paste0("^DQ",x_dq1(),"."),names(clean_bin),value = TRUE)])))
})
## Columns
y_q1 <- renderText({list_qnames[match.arg(input$y_q1,c("Select",names(list_qnames)))]})
y_dq1 <- renderText({list_dqnames[match.arg(input$y_dq1,c("Select",names(list_dqnames)))]})
output$y_q1_factor <- renderUI({sidebarPanel(
checkboxInput(inputId = "yq_all","All/None"),
checkboxGroupInput(inputId = "y_q1_level",
label = "Factor Level",
choices = names(clean_bin[,grep(paste0("^Q",y_q1(),"."),names(clean_bin),value = TRUE)])))
})
output$y_dq1_factor <- renderUI({sidebarPanel(
checkboxInput(inputId = "ydq_all","All/None"),
checkboxGroupInput(inputId = "y_dq1_level",
label = "Factor Level",
choices = names(clean_bin[,grep(paste0("^DQ",y_dq1(),"."),names(clean_bin),value = TRUE)])))
})
## CHECKBOXES
## Select All
observe({
req(input$xq_all)
updateCheckboxGroupInput(
session, "x_q1_level", choices = names(clean_bin[,grep(paste0("^Q",x_q1(),"."),names(clean_bin),value = TRUE)]),
selected = if (input$xq_all == TRUE) names(clean_bin[,grep(paste0("^Q",x_q1(),"."),names(clean_bin),value = TRUE)]))
},label = "xq_obs")
observe({
req(input$xdq_all)
updateCheckboxGroupInput(
session, "x_dq1_level", choices = names(clean_bin[,grep(paste0("^DQ",x_dq1(),"."),names(clean_bin),value = TRUE)]),
selected = if (input$xdq_all == TRUE) names(clean_bin[,grep(paste0("^DQ",x_dq1(),"."),names(clean_bin),value = TRUE)]))
},label = "xdq_obs")
observe({
req(input$yq_all)
updateCheckboxGroupInput(
session, "y_q1_level", choices = names(clean_bin[,grep(paste0("^Q",y_q1(),"."),names(clean_bin),value = TRUE)]),
selected = if (input$yq_all == TRUE) names(clean_bin[,grep(paste0("^Q",y_q1(),"."),names(clean_bin),value = TRUE)]))
},label = "yq_obs")
observe({
req(input$ydq_all)
updateCheckboxGroupInput(
session, "y_dq1_level", choices = names(clean_bin[,grep(paste0("^DQ",y_dq1(),"."),names(clean_bin),value = TRUE)]),
selected = if (input$ydq_all == TRUE) names(clean_bin[,grep(paste0("^DQ",y_dq1(),"."),names(clean_bin),value = TRUE)]))
},label = "ydq_obs")
clean <<- clean
clean_bin <<- clean_bin
cat(dataset()," ")
}, label = "Selection Frame")
## SELECTED VARIABLE SUMMARY
output$x_sel <- renderText({
req(length(c(input$x_t1,input$x_d1,input$x_s1)) > 0)
var <- c(input$x_t1,input$x_d1,input$x_s1)
var <- paste0("Summary of ",var[var != "Select"])})
output$x_sum <- renderTable({
req(length(c(input$x_t1,input$x_d1,input$x_s1)) > 0)
var <- renderText(c(input$x_t1,input$x_d1,input$x_s1))
if (var() != "Select") matrix(data = c(names(summary(clean[,var()])),summary(clean[,var()])), ncol = length(summary(clean[,var()])), nrow = 2, byrow = TRUE)
}, include.rownames = FALSE, include.colnames = FALSE)
output$y_sel <- renderText({
req(length(c(input$y_t1,input$y_d1,input$y_s1)) > 0)
var <- c(input$y_t1,input$y_d1,input$y_s1)
var <- paste0("Summary of "var[var != "Select"])})
output$y_sum <- renderTable({
req(length(c(input$y_t1,input$y_d1,input$y_s1)) > 0)
var <- renderText(c(input$y_t1,input$y_d1,input$y_s1))
if (var() != "Select") matrix(data = c(names(summary(clean[,var()])),summary(clean[,var()])), ncol = length(summary(clean[,var()])), nrow = 2, byrow = TRUE)
}, include.rownames = FALSE, include.colnames = FALSE)
## Deselect when unused
observe({
req(input$x_t1,input$x_d1,input$x_s1,input$y_t1,input$y_d1,input$y_s1)
if (input$x1 != "Triads") updateSelectInput(session, "x_t1", choices = c("Select",names(triads)), selected = "Select")
if (input$x1 != "Dyads") updateSelectInput(session, "x_d1", choices = c("Select",names(dyads)), selected = "Select")
if (input$x1 != "Stones") updateSelectInput(session, "x_s1", choices = c("Select",names(stones)), selected = "Select")
if (input$y1 != "Triads") updateSelectInput(session, "y_t1", choices = c("Select",names(triads)), selected = "Select")
if (input$y1 != "Dyads") updateSelectInput(session, "y_t1", choices = c("Select",names(dyads)), selected = "Select")
if (input$y1 != "Stones") updateSelectInput(session, "y_t1", choices = c("Select",names(stones)), selected = "Select")
})
## BUTTONS
observe({
if (input$go == 0) # tells action button to do nothing when not clicked ..
return()
}, label = "go_sub")
observe({
req(input$x_sub)
if (input$x_sub == 0) # tells action button to do nothing when not clicked ..
return()
}, label = "x_sub")
observe({
req(input$y_sub)
if (input$y_sub == 0) # tells action button to do nothing when not clicked ..
return()
}, label = "y_sub")
observe({
req(input$reset)
if (input$reset == 0) # tells action button to do nothing when not clicked ..
return()
}, label = "reset")
## DISPLAY SELECTED ROWS AND COLUMN VARIABLES
## Rows
rows <<- NULL
x_inv <<- NULL
observeEvent(input$x_sub,{
x_name <- switch(input$x1,
"Triads" = input$x_t1,
"Dyads" = input$x_d1,
"Stones" = input$x_s1,
"Questions" = input$x_q1_level,
"Descriptors" = input$x_dq1_level)
x_name <- x_name[x_name != "Select"]
## First Entry
if (is.null(rows)) {
rows <- matrix(data = x_name, byrow = TRUE)
if (dim(rows)[1] == 1) {
rows <- switch(substr(rows[1,1],1,1),
"T" = cbind(rows, paste0(input$xt_sld[1]," : ",input$xt_sld[2])),
"S" = cbind(rows, paste0(input$xs_sld[1]," : ",input$xs_sld[2])),
"D" = cbind(rows, paste0(input$xd_sld[1]," : ",input$xd_sld[2])))
colnames(rows)[1:2] <- switch(substr(rows[1,1],1,1),
"T" = c("Triads", "Triad Range"),
"S" = c("Stones", "Stone Range"),
"D" = c("Dyads", "Dyad Range"))
} else {
x_inv <<- x_name
x <- tstrsplit(rows[1,dim(rows)[2]], c("[DQSTRC][0-9].|_"))
colnames(rows)[dim(rows)[2]] <- x[[2]]
## Remove Factor Name
for(r in 1:length(x_name)){
x <- tstrsplit(rows[r,dim(rows)[2]], c("[DQSTRC][0-9].|_"))
rows[r,dim(rows)[2]] <- x[[3]]
}
}
} else {
## Factor Variables
if (length(x_name) > 1) {
x_inv <<- c(x_inv,x_name)
if (dim(rows)[1] == length(x_name)) rows <- cbind(rows,x_name) else {
if (dim(rows)[1] > length(x_name)) {
rows <- cbind(rows,c(x_name,rep("",dim(rows)[1]-length(x_name))))
} else {
rows <- rbind(rows,matrix("",nrow = length(x_name)-dim(rows)[1],ncol = dim(rows)[2]))
rows <- cbind(rows,x_name)
}
}
## Colnames
x <- tstrsplit(rows[1,dim(rows)[2]], c("[DQSTRC][0-9].|_"))
colnames(rows)[dim(rows)[2]] <- x[[2]]
## Remove Factor Name
for(r in 1:length(x_name)){
x <- tstrsplit(rows[r,dim(rows)[2]], c("[DQSTRC][0-9].|_"))
rows[r,dim(rows)[2]] <- x[[3]]
}
} else {
## Continuous Variables
c <- match("Triads", colnames(rows), nomatch = FALSE)
if (substr(x_name,1,1) == "T") {
if (c) {if (match("",rows[,c],nomatch = FALSE)) {
rows[match("",rows[,c])[1],c:(c+1)] <- c(x_name,paste0(input$xt_sld[1]," : ",input$xt_sld[2]))} else {
rows <- rbind(rows,rep("",dim(rows)[1]))
rows[match("",rows[,c])[1],c:(c+1)] <- c(x_name,paste0(input$xt_sld[1]," : ",input$xt_sld[2]))}} else {
rows <- cbind(rows,c(x_name,rep("",dim(rows)[1]-1)),c(paste0(input$xt_sld[1]," : ",input$xt_sld[2]),rep("",dim(rows)[1]-1)))
colnames(rows)[(dim(rows)[2]-1):dim(rows)[2]] <- c("Triads","Triad Range")}
}
c <- match("Dyads", colnames(rows), nomatch = FALSE)
if (substr(x_name,1,1) == "D") {
if (c) {if (match("",rows[,c],nomatch = FALSE)) {
rows[match("",rows[,c])[1],c:(c+1)] <- c(x_name,paste0(input$xd_sld[1]," : ",input$xd_sld[2]))} else {
rows <- rbind(rows,rep("",dim(rows)[1]))
rows[match("",rows[,c])[1],c:(c+1)] <- c(x_name,paste0(input$xd_sld[1]," : ",input$xd_sld[2]))}} else {
rows <- cbind(rows,c(x_name,rep("",dim(rows)[1]-1)),c(paste0(input$xd_sld[1]," : ",input$xd_sld[2]),rep("",dim(rows)[1]-1)))
colnames(rows)[(dim(rows)[2]-1):dim(rows)[2]] <- c("Dyads","Dyad Range")}
}
c <- match("Stones", colnames(rows), nomatch = FALSE)
if (substr(x_name,1,1) == "S") {
if (c) {if (match("",rows[,c],nomatch = FALSE)) {
rows[match("",rows[,c])[1],c:(c+1)] <- c(x_name,paste0(input$xs_sld[1]," : ",input$xs_sld[2]))} else {
rows <- rbind(rows,rep("",dim(rows)[1]))
rows[match("",rows[,c])[1],c:(c+1)] <- c(x_name,paste0(input$xs_sld[1]," : ",input$xs_sld[2]))}} else {
rows <- cbind(rows,c(x_name,rep("",dim(rows)[1]-1)),c(paste0(input$xs_sld[1]," : ",input$xs_sld[2]),rep("",dim(rows)[1]-1)))
colnames(rows)[(dim(rows)[2]-1):dim(rows)[2]] <- c("Stones","Stone Range")}
}
}
}
rows <<- rows
output$x_chosen <- renderUI({
html <- paste0("$$",print(xtable(rows, align=c("l",rep("c", ncol(rows))), caption = "Selected Rows"),
caption.placement = "top", floating=FALSE, tabular.environment="array", comment=FALSE, print.results=FALSE),"$$")
list(
withMathJax(HTML(html))
)
})
}, label = "x submit")
## Cols
cols <<- NULL
y_inv <<- NULL
observeEvent(input$y_sub,{
y_name <- switch(input$y1,
"Triads" = input$y_t1,
"Dyads" = input$y_d1,
"Stones" = input$y_s1,
"Questions" = input$y_q1_level,
"Descriptors" = input$y_dq1_level)
y_name <- y_name[y_name != "Select"]
## First Entry
if (is.null(cols)) {
cols <- matrix(data = y_name, byrow = TRUE)
if (dim(cols)[1] == 1) {
cols <- switch(substr(cols[1,1],1,1),
"T" = cbind(cols, paste0(input$yt_sld[1]," : ",input$yt_sld[2])),
"S" = cbind(cols, paste0(input$ys_sld[1]," : ",input$ys_sld[2])),
"D" = cbind(cols, paste0(input$yd_sld[1]," : ",input$yd_sld[2])))
colnames(cols)[1:2] <- switch(substr(cols[1,1],1,1),
"T" = c("Triads", "Triad Range"),
"S" = c("Stones", "Stone Range"),
"D" = c("Dyads", "Dyad Range"))
} else {
y_inv <<- y_name
y <- tstrsplit(cols[1,dim(cols)[2]], c("[DQSTRC][0-9].|_"))
colnames(cols)[dim(cols)[2]] <- y[[2]]
## Remove Factor Name
for(r in 1:length(y_name)){
y <- tstrsplit(cols[r,dim(cols)[2]], c("[DQSTRC][0-9].|_"))
cols[r,dim(cols)[2]] <- y[[3]]
}
}
} else {
## Factor Variables
if (length(y_name) > 1) {
y_inv <<- c(y_inv,y_name)
if (dim(cols)[1] == length(y_name)) cols <- cbind(cols,y_name) else {
if (dim(cols)[1] > length(y_name)) {
cols <- cbind(cols,c(y_name,rep("",dim(cols)[1]-length(y_name))))
} else {
cols <- rbind(cols,matrix("",nrow = length(y_name)-dim(cols)[1],ncol = dim(cols)[2]))
cols <- cbind(cols,y_name)
}
}
## Colnames
y <- tstrsplit(cols[1,dim(cols)[2]], c("[DQSTRC][0-9].|_"))
colnames(cols)[dim(cols)[2]] <- y[[2]]
## Remove Factor Name
for(r in 1:length(y_name)){
y <- tstrsplit(cols[r,dim(cols)[2]], c("[DQSTRC][0-9].|_"))
cols[r,dim(cols)[2]] <- y[[3]]
}
} else {
## Continuous Variables
c <- match("Triads", colnames(cols), nomatch = FALSE)
if (substr(y_name,1,1) == "T") {
if (c) {if (match("",cols[,c],nomatch = FALSE)) {
cols[match("",cols[,c])[1],c:(c+1)] <- c(y_name,paste0(input$yt_sld[1]," : ",input$yt_sld[2]))} else {
cols <- rbind(cols,rep("",dim(cols)[1]))
cols[match("",cols[,c])[1],c:(c+1)] <- c(y_name,paste0(input$yt_sld[1]," : ",input$yt_sld[2]))}} else {
cols <- cbind(cols,c(y_name,rep("",dim(cols)[1]-1)),c(paste0(input$yt_sld[1]," : ",input$yt_sld[2]),rep("",dim(cols)[1]-1)))
colnames(cols)[(dim(cols)[2]-1):dim(cols)[2]] <- c("Triads","Triad Range")}
}
c <- match("Dyads", colnames(cols), nomatch = FALSE)
if (substr(y_name,1,1) == "D") {
if (c) {if (match("",cols[,c],nomatch = FALSE)) {
cols[match("",cols[,c])[1],c:(c+1)] <- c(y_name,paste0(input$yd_sld[1]," : ",input$yd_sld[2]))} else {
cols <- rbind(cols,rep("",dim(cols)[1]))
cols[match("",cols[,c])[1],c:(c+1)] <- c(y_name,paste0(input$yd_sld[1]," : ",input$yd_sld[2]))}} else {
cols <- cbind(cols,c(y_name,rep("",dim(cols)[1]-1)),c(paste0(input$yd_sld[1]," : ",input$yd_sld[2]),rep("",dim(cols)[1]-1)))
colnames(cols)[(dim(cols)[2]-1):dim(cols)[2]] <- c("Dyads","Dyad Range")}
}
c <- match("Stones", colnames(cols), nomatch = FALSE)
if (substr(y_name,1,1) == "S") {
if (c) {if (match("",cols[,c],nomatch = FALSE)) {
cols[match("",cols[,c])[1],c:(c+1)] <- c(y_name,paste0(input$ys_sld[1]," : ",input$ys_sld[2]))} else {
cols <- rbind(cols,rep("",dim(cols)[1]))
cols[match("",cols[,c])[1],c:(c+1)] <- c(y_name,paste0(input$ys_sld[1]," : ",input$ys_sld[2]))}} else {
cols <- cbind(cols,c(y_name,rep("",dim(cols)[1]-1)),c(paste0(input$ys_sld[1]," : ",input$ys_sld[2]),rep("",dim(cols)[1]-1)))
colnames(cols)[(dim(cols)[2]-1):dim(cols)[2]] <- c("Stones","Stone Range")}
}
}
}
cols <<- cols
output$y_chosen <- renderUI({
html <- paste0("$$",print(xtable(cols, align=c("l",rep("c", ncol(cols))), caption = "Selected Columns"),
caption.placement = "top", floating=FALSE, tabular.environment="array", comment=FALSE, print.results=FALSE),"$$")
list(
withMathJax(HTML(html))
)
})
}, label = "y submit")
## DATAFRAME CONSTRUCTION
observe({
isolate({
values <- reactiveValues(dynamic_frame = clean_bin)
observeEvent(input$go,{
## Convert continuous variables to binary based on ranges.
row_sig <- colnames(rows)[match(c("Triads","Dyads","Stones"),colnames(rows))]
row_sig <- row_sig[!is.na(row_sig)]
col_sig <- colnames(cols)[match(c("Triads","Dyads","Stones"),colnames(cols))]
col_sig <- col_sig[!is.na(col_sig)]
row_range <- colnames(rows)[match(c("Triad Range","Dyad Range","Stone Range"),colnames(rows))]
row_range <- row_range[!is.na(row_range)]
col_range <- colnames(cols)[match(c("Triad Range","Dyad Range","Stone Range"),colnames(cols))]
col_range <- col_range[!is.na(col_range)]
cont_rows <- as.character(rows[,row_sig])
cont_rows <- cont_rows[cont_rows != ""]
cont_rows <- values$dynamic_frame[,cont_rows]
cont_rows_range <- as.character(rows[,row_range])[as.character(rows[,row_range]) != ""]
cont_rows_range <- tstrsplit(cont_rows_range," : ")
cont_cols <- as.character(cols[,col_sig])
cont_cols <- cont_cols[cont_cols != ""]
cont_cols <- values$dynamic_frame[,cont_cols]
cont_cols_range <- as.character(cols[,col_range])[as.character(cols[,col_range]) != ""]
cont_cols_range <- tstrsplit(cont_cols_range," : ")
if (length(cont_rows) > 0) {
for (c in 1:dim(cont_rows)[2]){
if (substr(colnames(cont_rows)[c],1,1) == "T"){
cont_rows[,c] <- as.integer(((as.numeric(cont_rows_range[[1]][c]) * 10) < cont_rows[,c]) & ((as.numeric(cont_rows_range[[2]][c]) * 10) > cont_rows[,c]))
} else {
cont_rows[,c] <- as.integer(((as.numeric(cont_rows_range[[1]][c]) / 10) < cont_rows[,c]) & ((as.numeric(cont_rows_range[[2]][c]) / 10) > cont_rows[,c]))
}
}
cont_rows <- as.data.frame(ifelse(cont_rows == 1,1,NA))
}
if (length(cont_cols) > 0) {
for (c in 1:dim(cont_cols)[2]){
if (substr(colnames(cont_cols)[c],1,1) == "T"){
cont_cols[,c] <- as.integer(((as.numeric(cont_cols_range[[1]][c]) * 10) < cont_cols[,c]) & ((as.numeric(cont_cols_range[[2]][c]) * 10) > cont_cols[,c]))
} else {
cont_cols[,c] <- as.integer(((as.numeric(cont_cols_range[[1]][c]) / 10) < cont_cols[,c]) & ((as.numeric(cont_cols_range[[2]][c]) / 10) > cont_cols[,c]))
}
}
cont_cols <- as.data.frame(ifelse(cont_cols == 1,1,NA))
}
## Factor Variables
if (length(cont_rows) == 0) desc_rows <<- values$dynamic_frame[,x_inv]
if (length(x_inv) == 0) desc_rows <<- cont_rows
if ((length(x_inv) > 0) && (length(cont_rows) > 0)) desc_rows <<- cbind(cont_rows,values$dynamic_frame[,x_inv])
if (length(cont_cols) == 0) desc_cols <<- values$dynamic_frame[,y_inv]
if (length(y_inv) == 0) desc_cols <<- cont_cols
if ((length(y_inv) > 0) && (length(cont_cols) > 0)) desc_cols <<- cbind(cont_cols,values$dynamic_frame[,y_inv])
desc_stats <<- matrix(NA, nrow = dim(desc_rows)[2], ncol = dim(desc_cols)[2])
for (r in 1:dim(desc_rows)[2]){
for (c in 1:dim(desc_cols)[2]){
desc_stats[r,c] <- table(desc_rows[,r],desc_cols[,c])
}
}
rownames(desc_stats) <- names(desc_rows)
colnames(desc_stats) <- names(desc_cols)
desc_props <- unit2perc(clean_bin,desc_stats)
values$stats <<- desc_stats
values$props <<- desc_props
output$stats <- renderUI({
html <- paste0("$$",print(xtable(values$props, align=c("l",rep("c", ncol(values$stats)))),
floating=FALSE, tabular.environment="array", comment=FALSE, print.results=FALSE),"$$")
list(
withMathJax(HTML(html))
)
})
output$props <- renderUI({
html <- paste0("$$",print(xtable(values$stats, align=c("l",rep("c", ncol(values$props)))),
floating=FALSE, tabular.environment="array", comment=FALSE, print.results=FALSE),"$$")
list(
withMathJax(HTML(html))
)
})
}, label = "df_builder")
})}, label = "main_wrapper")
observeEvent(input$reset,{
rows <<- NULL
cols <<- NULL
desc_stats <<- NULL
desc_props <<- NULL
output$x_chosen <<- renderUI({rows})
output$y_chosen <<- renderUI({cols})
output$stats <<- renderUI({desc_stats})
output$props <<- renderUI({desc_props})
updateSelectInput(session, "x1", choices = c("Select","Triads","Dyads","Stones","Questions","Descriptors"), selected = "Select")
updateSelectInput(session, "y1", choices = c("Select","Triads","Dyads","Stones","Questions","Descriptors"), selected = "Select")
}, label = "reset")
})