-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
executable file
·687 lines (488 loc) · 25.5 KB
/
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
library(shiny)
library(png)
library(shinyBS)
library(shinyjs)
library(shinyDND)
library(ggplot2)
library(dplyr) ###NEW PACKAGE
library(shinydashboard)
library(simstudy) ### NEW PACKAGE
library(lubridate)###NEW PACKGE
library(shinyalert)##NEW PACKAGE
library(shinyWidgets)
####read in dataset###
seaotters <- read.csv("Otter.csv",header=T)
diet <- read.csv("Diet.csv",header=T)
diet$Diet<-as.character(diet$Diet)
bank = read.csv("questionbank.csv")
bank = data.frame(lapply(bank, as.character), stringsAsFactors = FALSE)
shinyServer(function(input, output,session) {
observeEvent(input$info,{
sendSweetAlert(
session = session,
title = "Instructions:",
type = NULL,
closeOnClickOutside = TRUE,
text = "Pick a data set and variables to view the interaction plot and the associated ANOVA table."
)
})
output$ack2<-renderUI((
h4('Thanks for the data set and code provided by The University of Sheffield (',url,') and Dr.Dylan Childs(',url2,').')
))
url <- a("www.sheffield.ac.uk/mash/data", href="https://www.sheffield.ac.uk/mash/data",target="_blank")
url2 <- a("github.com/dzchilds", href="https://github.com/dzchilds",target="_blank")
output$box1<-renderUI(h4('ANOVA is used for comparing three or more group means.
Different groups are different levels of categorical variables, and group means are calculated from continuous variables.
',br(),br(),'EX. Are the average score of three STAT 200 sections significantly different from each other?'))
output$box2<-renderUI(h4('Regression is used for determining the relationship between two continuous variables. One dependent variable (Y) can also be affected by multiple independent variables (X).
',br(),br(),'EX. How will crime rate be impacted by population density, unemployment rate, and income.'))
output$box3<-renderUI(h4('ANCOVA is adding continuous variables onto ANOVA analysis, which is called covariate.
Significant different between group means and significant relationship between continuous variables will both be analyzed.
',br(),br(),'EX. Who makes the most money? Will gender or years after graduation influence the income? '))
####button###
observeEvent(input$go,{
updateTabItems(session,"tabs","exploring")
})
observeEvent(input$game,{
updateTabItems(session,"tabs","game")
})
observeEvent(input$pre2,{
updateTabItems(session,"tabs","box")
})
observeEvent(input$go2,{
updateTabItems(session,"tabs","instruction")
})
observeEvent(input$start,{
updateTabItems(session,"tabs","instruction")
})
##########################Download the dataset#################
# Reactive value for selected dataset ----
datasetInput <- reactive({
switch(input$menu1,
'Otter'=seaotters,
'Diet'=diet)
})
# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
filename = function() {
paste(input$menu1, ".csv", sep = "")
},
content = function(con) {
write.csv(datasetInput(), con)
}
)
############################### Exploring ##############################
###prep the data###
otters.model <- lm(Otters ~ Location + Year + Location:Year, data = seaotters)
pred.data <- expand.grid(Year = 1992:2003, Location = c("Lagoon", "Bay"))
pred.data <- mutate(pred.data, Otters = predict(otters.model, pred.data))
diet.model<-lm(ab_change~gender+Diet+Age+Height+pre.weight+gender:Age+gender:Height+gender:pre.weight+
Diet:Age+Diet:Height+Diet:pre.weight,data=diet)
diet.model2<-lm(ab_change~Age+gender+Age:gender,data=diet)
pred.data2 <- expand.grid(Age = 16:60, gender = c("M", "F"))
pred.data2 <- mutate(pred.data2, ab_change = predict(diet.model2, pred.data2))
diet.model3<-lm(ab_change~Height+gender+Height:gender,data=diet)
pred.data3 <- expand.grid(Height = 141:201, gender = c("M", "F"))
pred.data3 <- mutate(pred.data3, ab_change = predict(diet.model3, pred.data3))
diet.model4<-lm(ab_change~pre.weight+gender+pre.weight:gender,data=diet)
pred.data4 <- expand.grid(pre.weight=58:103, gender = c("M", "F"))
pred.data4 <- mutate(pred.data4, ab_change = predict(diet.model4, pred.data4))
diet.model5<-lm(ab_change~Age+Diet+Age:Diet,data=diet)
pred.data5 <- expand.grid(Age = 16:60, Diet = c('1','2','3'))
pred.data5 <- mutate(pred.data5, ab_change = predict(diet.model5, pred.data5))
diet.model6<-lm(ab_change~Height+Diet+Height:Diet,data=diet)
pred.data6<- expand.grid(Height = 141:201, Diet = c('1','2','3'))
pred.data6 <- mutate(pred.data6, ab_change = predict(diet.model6, pred.data6))
diet.model7<-lm(ab_change~pre.weight+Diet+pre.weight:Diet,data=diet)
pred.data7 <- expand.grid(pre.weight=58:103, Diet = c('1','2','3'))
pred.data7 <- mutate(pred.data7, ab_change = predict(diet.model7, pred.data7))
###save random model
rand<-reactiveValues(rand_mod=NULL)
###Graph the plot of interaction###
output$plot_gg<-renderPlot(
if (input$menu1=='Otter') {ggplot(pred.data, aes(x = Year, y = Otters, colour = Location)) +
geom_line() + geom_point(data = seaotters) +
xlab("Year") + ylab("Otters")+theme(text = element_text(size=20),panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))}
else if (input$menu1=='Diet'){
if (input$select_conti=='Age' & input$select_covar=='Gender'){ggplot(pred.data2, aes(x = Age, y = ab_change, colour = gender)) +
geom_line() + geom_point(data = diet) +
xlab("Age") + ylab("Decrease in Weight")+theme(text = element_text(size=20),panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))}
else if (input$select_conti=='Height' & input$select_covar=='Gender'){ggplot(pred.data3, aes(x = Height, y = ab_change, colour = gender)) +
geom_line() + geom_point(data = diet) +
xlab("Height") + ylab("Decrease in Weight")+theme(text = element_text(size=20),panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))}
else if (input$select_conti=='Pre-diet Weight' & input$select_covar=='Gender'){ggplot(pred.data4, aes(x = pre.weight, y = ab_change, colour = gender)) +
geom_line() + geom_point(data = diet) +
xlab("Pre-diet Weight") + ylab("Decrease in Weight")+theme(text = element_text(size=20),panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))}
else if (input$select_conti=='Age' & input$select_covar=='Diet'){ggplot(pred.data5, aes(x = Age, y = ab_change, colour =Diet)) +
geom_line() + geom_point(data = diet) +
xlab("Age") + ylab("Decrease in Weight")+theme(text = element_text(size=20),panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))}
else if (input$select_conti=='Height' & input$select_covar=='Diet'){ggplot(pred.data6, aes(x = Height, y = ab_change, colour = Diet)) +
geom_line() + geom_point(data = diet) +
xlab("Height") + ylab("Decrease in Weight")+theme(text = element_text(size=20),panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))}
else if (input$select_conti=='Pre-diet Weight' & input$select_covar=='Diet'){ggplot(pred.data7, aes(x = pre.weight, y = ab_change, colour = Diet)) +
geom_line() + geom_point(data = diet) +
xlab("Pre-diet Weight") + ylab("Decrease in Weight")+theme(text = element_text(size=20),panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))}
}
else if (input$menu1=='Random'){
###create data with label A and B with different slope and intersection
A<-'A'
B<-'B'
a<-input$inter1
b<-input$inter2
slope1<-input$slope1
slope2<-input$slope2
def <- defData(varname = "inter", dist = "nonrandom", formula = a, id = "id")
def<- defData(def,varname = "slope", dist = "nonrandom", formula = slope1, id = "slope")
def <- defData(def, varname = "X", dist = "uniform", formula = "0;20")
def <- defData(def, varname = "Y", formula = "inter + X * slope", variance = 11)
def2<- defData(varname = "inter", dist = "nonrandom", formula = b, id = "id")
def2 <- defData(def2,varname = "slope", dist = "nonrandom", formula = slope2, id = "slope")
def2<- defDataAdd(def2, varname = "X", dist = "uniform", formula = "0;20")
def2 <- defDataAdd(def2, varname = "Y", formula = "inter + X * slope", variance =11)
dt <- genData(input$sample, def)
dt2<-genData(input$sample,def2)
names(dt2)[1]<-'id'
dt$cov<-'A'
dt2$cov<-'B'
comb<-rbind(dt,dt2)
aov.model<-lm(Y~X+cov+cov:X,data=comb)
pred.aov <- expand.grid(X =0:20, cov = c("A","B"))
pred.aov <- mutate(pred.aov, Y = predict(aov.model, pred.aov))
ggplot(pred.aov, aes(x = X, y = Y, colour = cov)) +
geom_line() + geom_point(data = comb) +
xlab("X") + ylab("Y")+theme(text = element_text(size=20),panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))}
)
###ANCOVA analysis table###
output$analysis1<-renderPrint(
if (input$menu1=='Otter') {anova(otters.model)}
else if (input$menu1=='Diet'){
if (input$select_conti=='Age' & input$select_covar=='Gender'){anova(diet.model2)}
else if (input$select_conti=='Height' & input$select_covar=='Gender'){anova(diet.model3)}
else if (input$select_conti=='Pre-diet Weight' & input$select_covar=='Gender'){anova(diet.model4)}
else if (input$select_conti=='Age' & input$select_covar=='Diet'){anova(diet.model5)}
else if (input$select_conti=='Height' & input$select_covar=='Diet'){anova(diet.model6)}
else if (input$select_conti=='Pre-diet Weight' & input$select_covar=='Diet'){anova(diet.model7)}
}
else if (input$menu1=='Random'){
A<-'A'
B<-'B'
a<-input$inter1
b<-input$inter2
slope1<-input$slope1
slope2<-input$slope2
def <- defData(varname = "inter", dist = "nonrandom", formula = a, id = "id")
def<- defData(def,varname = "slope", dist = "nonrandom", formula = slope1, id = "slope")
def <- defData(def, varname = "X", dist = "uniform", formula = "10;20")
def <- defData(def, varname = "Y", formula = "inter + X * slope", variance = 11)
def2<- defData(varname = "inter", dist = "nonrandom", formula = b, id = "id")
def2 <- defData(def2,varname = "slope", dist = "nonrandom", formula = slope2, id = "slope")
def2<- defDataAdd(def2, varname = "X", dist = "uniform", formula = "10;20")
def2 <- defDataAdd(def2, varname = "Y", formula = "inter + X * slope", variance = 11)
dt <- genData(input$sample, def)
dt2<-genData(input$sample,def2)
names(dt2)[1]<-'id'
dt$cov<-'A'
dt2$cov<-'B'
comb<-rbind(dt,dt2)
aov.model<-lm(Y~X+cov+cov:X,data=comb)
##testing passing the model
rand$rand_mod<-anova(aov.model)[3,"Pr(>F)"]
anova(aov.model)
}
)
#####get p values for each interaction
var<-reactiveValues(p=NULL)
observe({
# if (is.null(input$menu1)){
# return()
# }
#
# isolate({
# var$p<-as.numeric(anova(otters.model)[3,"Pr(>F)"])
#
# })
if (input$menu1=='Otter') {var$p<-as.numeric(anova(otters.model)[3,"Pr(>F)"])}
else if (input$menu1=='Diet'){
if (input$select_conti=='Age' & input$select_covar=='Gender'){var$p<-as.numeric(anova(diet.model2)[3,"Pr(>F)"])}
else if (input$select_conti=='Height' & input$select_covar=='Gender'){var$p<-as.numeric(anova(diet.model3)[3,"Pr(>F)"])}
else if (input$select_conti=='Pre-diet Weight' & input$select_covar=='Gender'){var$p<-as.numeric(anova(diet.model4)[3,"Pr(>F)"])}
else if (input$select_conti=='Age' & input$select_covar=='Diet'){var$p<-as.numeric(anova(diet.model5)[3,"Pr(>F)"])}
else if (input$select_conti=='Height' & input$select_covar=='Diet'){var$p<-as.numeric(anova(diet.model6)[3,"Pr(>F)"])}
else if (input$select_conti=='Pre-diet Weight' & input$select_covar=='Diet'){var$p<-as.numeric(anova(diet.model7)[3,"Pr(>F)"])}
}
else if (input$menu1=='Random'){var$p<-as.numeric(rand$rand_mod)}
})
output$p<-renderUI(
if (var$p<=0.05){
h4(strong('P-value for this interaction is',signif(var$p,4),'.' ,br(),'Since the p-value is smaller than 0.05
(α=0.05), there is a statistically significant interaction between these two variables.'))}
else {h4(strong('P-value for this interaction is',signif(var$p,4),'.' ,br(),'Since the p-value is greater than 0.05
(α=0.05), there is NOT a statistically significant interaction between these two variables.'))}
)
###################################### Bank B #############################################################
numbers <- reactiveValues(strong = c(), moderate = c(), insig = c(), index = c(), question = data.frame())
observeEvent(input$go,{
numbers$strong = sample(1:12,1)
numbers$moderate = sample(13:24,1)
numbers$insig= sample(25:36,1)
numbers$index =c("A","B","C")
numbers$question = cbind(bank[c(numbers$strong,numbers$moderate,numbers$insig),],numbers$index)
})
observeEvent(input$new,{
numbers$strong = sample(1:12,1)
numbers$moderate = sample(13:24,1)
numbers$insig= sample(25:36,1)
numbers$index = c("A","B","C")
numbers$question = cbind(bank[c(numbers$strong,numbers$moderate,numbers$insig),],numbers$index)
updateRadioButtons(session, 'radio1','',c('A','B','C'),selected='',inline=TRUE)
updateRadioButtons(session,"radio2", '',c('A','B','C'),selected='',inline=TRUE)
updateRadioButtons(session, "radio3", '',c('A','B','C'),selected='',inline=TRUE)
})
observeEvent(input$tabs,{
numbers$strong = sample(1:12,1)
numbers$moderate = sample(13:24,1)
numbers$insig= sample(25:36,1)
numbers$index = c("A","B","C")
numbers$question = cbind(bank[c(numbers$strong,numbers$moderate,numbers$insig),],numbers$index)
})
output$plot1 <- renderUI({
img(src = numbers$question[numbers$question[5] == "A",4], width = "100%", height = "107%", style = "text-align: center")
})
# output$table1 <- renderUI({
# img(src = numbers$question[numbers$question[5] == "A",3], width = "100%", height = "100%", style = "text-align: center")
# })
output$plot2 <- renderUI({
img(src = numbers$question[numbers$question[5] == "B",4], width = "100%", height = "107%", style = "text-align: center")
})
# output$table2 <- renderUI({
# img(src = numbers$question[numbers$question[5] == "B",3], width = "100%", height = "100%", style = "text-align: center")
# })
output$plot3 <- renderUI({
img(src = numbers$question[numbers$question[5] == "C",4], width = "100%", height = "107%", style = "text-align: center")
})
# output$table3 <- renderUI({
# img(src = numbers$question[numbers$question[5] == "C",3], width = "100%", height = "100%", style = "text-align: center")
# })
#######randomize the table######
index2 <- reactiveValues(index2 = 3)
observeEvent(input$new,{index2$index2 <- sample(1:4,1, replace=TRUE, prob=NULL)
})
observeEvent(input$reset,{index2$index2 <- sample(1:4,1, replace=TRUE, prob=NULL)
})
output$table1<-renderUI({
if (index2$index2==1){img(src = numbers$question[numbers$question[5] == "A",3], width = "105%", height = "105%", style = "text-align: center")}
else if (index2$index2==2){img(src = numbers$question[numbers$question[5] == "B",3], width = "105%", height = "105%", style = "text-align: center")}
else if (index2$index2==3){img(src = numbers$question[numbers$question[5] == "C",3], width = "105%", height = "105%", style = "text-align: center")}
else if (index2$index2==4){img(src = numbers$question[numbers$question[5] == "A",3], width = "105%", height = "105%", style = "text-align: center")}
})
output$table2<-renderUI({
if (index2$index2==1){img(src = numbers$question[numbers$question[5] == "B",3], width = "105%", height = "105%", style = "text-align: center")}
else if (index2$index2==2){img(src = numbers$question[numbers$question[5] == "C",3], width = "105%", height = "105%", style = "text-align: center")}
else if (index2$index2==3){img(src = numbers$question[numbers$question[5] == "A",3], width = "105%", height = "105%", style = "text-align: center")}
else if (index2$index2==4){img(src = numbers$question[numbers$question[5] == "C",3], width = "105%", height = "105%", style = "text-align: center")}
})
output$table3<-renderUI({
if (index2$index2==1){img(src = numbers$question[numbers$question[5] == "C",3], width = "105%", height = "105%", style = "text-align: center")}
else if (index2$index2==2){img(src = numbers$question[numbers$question[5] == "A",3], width = "105%", height = "105%", style = "text-align: center")}
else if (index2$index2==3){img(src = numbers$question[numbers$question[5] == "B",3], width = "105%", height = "105%", style = "text-align: center")}
else if (index2$index2==4){img(src = numbers$question[numbers$question[5] == "B",3], width = "105%", height = "105%", style = "text-align: center")}
})
####letter for the plot
output$a<-renderUI(h4('A'))
output$b<-renderUI(h4('B'))
output$c<-renderUI(h4('C'))
#####buttons####
observeEvent(input$submitA,{
updateButton(session,"submitA",disabled = TRUE)
})
observeEvent(input$new,{
updateButton(session,"submitA",disabled = FALSE)
})
observeEvent(input$submitA,{
updateButton(session,"new",disabled = FALSE)
})
observeEvent(input$new,{
updateButton(session,"new",disabled = TRUE)
})
observeEvent(input$new, {
reset("radio1")
})
###################check answers#####
summationC<-reactiveValues(correct1 = c(0), started=FALSE)
observeEvent(input$submitA,{
observeEvent(input$new,{
output$answer1 <- renderUI({
img(src = NULL,width=30)
})
})
observe({
output$answer1 <- renderUI({
if (!is.null(input$radio1)){
if (index2$index2==1 &input$radio1 == 'A'){
img(src = "check.png",width=30)
}
else if (index2$index2==2 &input$radio1 == 'B') {img(src = "check.png",width=30)}
else if (index2$index2==3 &input$radio1 == 'C'){img(src = "check.png",width=30)}
else if (index2$index2==4 &input$radio1 == 'A'){img(src = "check.png",width =30)}
else{
img(src = "cross.png",width=30)
}
}
})
})
})
observeEvent(input$submitA,{
observeEvent(input$new,{
output$answer2 <- renderUI({
img(src = NULL,width=30)
})
})
observe({
output$answer2 <- renderUI({
if (!is.null(input$radio2)){
if (index2$index2==1 &input$radio2 == 'B'){
img(src = "check.png",width=30)
}
else if (index2$index2==2 &input$radio2 == 'C') {img(src = "check.png",width=30)}
else if (index2$index2==3 &input$radio2 == 'A'){img(src = "check.png",width=30)}
else if (index2$index2==4 &input$radio2 == 'C'){img(src = "check.png",width=30)}
else{
img(src = "cross.png",width=30)
}
}
})
})
})
observeEvent(input$submitA,{
observeEvent(input$new,{
output$answer3 <- renderUI({
img(src = NULL,width=30)
})
})
observe({
output$answer3 <- renderUI({
if (!is.null(input$radio3)){
if (index2$index2==1 &input$radio3 == 'C'){
img(src = "check.png",width=30)
}
else if (index2$index2==2 &input$radio3 == 'A') {img(src = "check.png",width=30)}
else if (index2$index2==3 &input$radio3 == 'B'){img(src = "check.png",width=30)}
else if (index2$index2==4 &input$radio3 == 'B'){img(src = "check.png",width=30)}
else{
img(src = "cross.png",width=30);
}
}
})
})
})
#####count correct answer ########
summationC<-reactiveValues(correct1 = c(0),total=c(), started=FALSE)
observeEvent(input$submitA,{
for (i in input$radio1){
summationC$total = c(summationC$total,1)
if (index2$index2==1 &input$radio1 == 'A'){
summationC$correct1 = c(summationC$correct1,1)
}
else if (index2$index2==2 &input$radio1 == 'B') { summationC$correct1 = c(summationC$correct1,1)}
else if (index2$index2==3 &input$radio1 == 'C'){summationC$correct1 = c(summationC$correct1,1)}
else if (index2$index2==4 &input$radio1 == 'A'){summationC$correct1 = c(summationC$correct1,1)}
else{
summationC$correct1 = c(summationC$correct1,0)}
}
for (i in input$radio2){
summationC$total = c(summationC$total,1)
if (index2$index2==1 &input$radio2 == 'B'){
summationC$correct1 = c(summationC$correct1,1)
}
else if (index2$index2==2 &input$radio2 == 'C') {summationC$correct1 = c(summationC$correct1,1)}
else if (index2$index2==3 &input$radio2 == 'A'){summationC$correct1 = c(summationC$correct1,1)}
else if (index2$index2==4 &input$radio2 == 'C'){summationC$correct1 = c(summationC$correct1,1)}
else{
summationC$correct1 = c(summationC$correct1,0)}
}
for (i in input$radio3){
summationC$total = c(summationC$total,1)
if (index2$index2==1 &input$radio3 == 'C'){
i
summationC$correct1 = c(summationC$correct1,1)
}
else if (index2$index2==2 &input$radio3 == 'A') {summationC$correct1 = c(summationC$correct1,1)}
else if (index2$index2==3 &input$radio3 == 'B'){summationC$correct1 = c(summationC$correct1,1)}
else if (index2$index2==4 &input$radio3 == 'B'){ summationC$correct1 = c(summationC$correct1,1)}
else{
summationC$correct1 = c(summationC$correct1,0)}
}
})
output$correctC <- renderPrint({
if (sum(c(summationC$correct1))==0) {cat("You have earned 0 points")}
else{
cat("You have earned",sum(c(summationC$correct1)),'points')}
})
###########timer####################
# Initialize the timer, 60 seconds, not active.
timer <- reactiveVal(60)
active <- reactiveVal(FALSE)
# Output the time left.
output$timeleft <- renderText({
paste("Time left: ", seconds_to_period(timer()))
})
########show up the scoreing panel and popup####
# observer that invalidates every second. If timer is active, decrease by one.
observe({
invalidateLater(1000, session)
isolate({
if(active())
{
timer(timer()-1)
if(timer()<1)
{
active(FALSE)
shinyalert('Count Down Complete','Click to see your score',
type = "success")
output$scoreBox <- renderValueBox({
valueBox(
paste0(sum(c(summationC$correct1))), "Totel Score", icon = icon("list"),
color = "purple"
)
})
output$percentBox <- renderValueBox({
if (sum(c(summationC$correct1))==0){valueBox(
paste0('0%'), "Accuracy", icon = icon("thumbs-up", lib = "glyphicon"),
color = "light-blue"
)}
else{
valueBox(
paste0(round(sum(c(summationC$correct1))/sum(c(summationC$total))*100,digit=1),'%'), "Accuracy", icon = icon("thumbs-up", lib = "glyphicon"),
color = "light-blue"
)}
})
output$timeBox <- renderValueBox({
valueBox(
paste0(input$seconds,'s'), "Time Used", icon = icon("time",lib = "glyphicon"),
color = "maroon"
)
})
}
}
})
})
# observers for actionbuttons
observeEvent(input$start_timer, {active(TRUE)})
#observeEvent(input$stop, {active(FALSE)})
observeEvent(input$start_timer, {timer(input$seconds)})
observeEvent(input$reset, {timer(input$seconds);active(FALSE)
output$scoreBox<-NULL;
output$percentBox<-NULL;
output$timeBox<-NULL;
summationC$correct1 <- c(0); summationC$total=c()
})
##closing for ui DON'T DELET####
})