-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverErasePreviousFileMemory.R
542 lines (478 loc) · 21 KB
/
serverErasePreviousFileMemory.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
library(shiny)
library(googleVis)
# Paired t-test example data
reg = c(16, 20, 21, 22, 23, 22, 27, 25, 27, 28)
prem = c(19, 22, 24, 24, 25, 25, 26, 26, 28, 32)
# Two group t-test example data
control = c(91, 87, 99, 77, 88, 91)
treat = c(101, 110, 103, 93, 99, 104)
# mtcars dataset
data(mtcars)
d<-mtcars[,c("mpg", "am")]
row.names(d)<-NULL
names(d)<-c("Mileage", "Transmission")
d$Transmission[d$Transmission == 1.0]<-"Automatic"
d$Transmission[d$Transmission == 0.0]<-"Manual"
d<-d[1:10,]
# To erase previous fileInput memory
prevFilePath<-"ABCD"
uploadOnce<-FALSE
shinyServer(function(input,output, session) {
# Output for App Usage Guide tab
usageGuidelines<-renderUI(
withTags({
div(class = "usage",
h3("Notes on using this Shiny App"),
br(),
p("This Shiny App demonstrates, using examples, some of typical applications of Student's t-test.
In particular, it demonstates the use of", b("Paired t-test"), "and", b("Two Sample t-test"), "and how
these tests can be used to compare two sets of data (either paired data or data from two groups) and determine
if there is a statistically significant difference between the two sets of data. It also has a provision
to enable users to upload their data and execute the appropriate t-test on the uploaded data."),
p("Information about Students's t-tests can be found ",
a("here", href = "http://en.wikipedia.org/wiki/Student%27s_t-test", target = "_blank")),
br(),
p("Demonstration of the application of t-test is done using the following three examples:"),
ol(li("Paired t-test"), li("Two Sample t-test"), li("Two Sample t-test using R's", b("mtcars"), "dataset")),
p("Users can use a drop down list to select the different examples, as shown in the image below"),
img(src = "pic8.png", width = 200, height = 400),
p(b("Note:"),"The fourth option in the drop down list enables the user to upload their data and run
t-test on the uploaded data. The", b("Upload File"), "file chooser becomes available once the user
selects to upload data. The user can specify if the file has a header or not (default: No header).
In addition to the file chooser, the App provides options to specify paired or two sample test
(default: two sample test), select the type of alternative hypothesis (default: Not equal or two sided),
and to specify if two sets of data have equal variance (default: not equal); note that the final option
applies only to the two sample test and does not affect the paired test."),
p(b("Note"), "that the app currently handles .csv file only. Only the first two rows of data and
the optional header are accessed."),
br(),
h5("Outputs"),
p("The output of the t-test is shown in the", b("Main"), "panel. A boxplot for the data is shown in
the", b("Boxplot"), "panel."),
br(),
p("Finally, please look up the", b("Limitations"), "tab that describes some of the limitations of this App.")
)
})
)
output$usage<-usageGuidelines
output$usagemain<-usageGuidelines
# Output for Credits tab
output$credits<-renderUI({
withTags({
div(
h4("Credits and other Links"),
p("The first two examples were from this ",
a('document.', href="http://www.stat.columbia.edu/~martin/W2024/R2.pdf", target = "_blank"),
"This was among the top Google search results!!"),
p("A sample file for use with the 'Upload Data' option and tested with this application can be found ",
a("here.", href = "https://github.com/mmulchandani/ttest/blob/master/traintimes.csv",
target = "_blank"), " Once there, click on the", b("Raw"), "button to obtain a text version of the file
that can be saved. The data for this file was obtained from this ",
a("webpage.", href = "http://www.r-bloggers.com/paired-students-t-test/", target = "_blank"))
)
})
})
# Output for limitations tab
output$limits<-renderUI({
withTags({
div(
h4("Limitations"),
ol(
li("The App currently doesn't demonstrate one sample t-test."),
li("The", b("Upload Data"), "feature has not been extensively tested with all different types
of data. It has actually been minimally tested with a simple dataset such as",
a("this.", href = "https://github.com/mmulchandani/ttest/blob/master/traintimes.csv", target = "_blank"),
"In case you have a dataset that this app fails on, please email the same to me at [email protected].")
)
)
})
})
# Renders the left hand navigation panel
output$choose_dataset <- renderUI({
selectInput("option", "Choose Dataset", c("Welcome Message", "Paired t-test Example", "Two Group t-test Example", "R Dataset: mtcars", "Upload Data"),
selected = "Welcome Message")
})
# Dynamic input element (1) for "Upload Data" option
# File Choose box
output$filechoose<-renderUI({
if(is.null(input$option))
return()
switch(input$option,
"Upload Data" = {
fileInput('inputFile', "Upload File",
accept=c('text/csv','text/comma-separated-values','.csv'))
}
)
})
# Dynamic input element (2) for "Upload Data" option
# Check box to specify if input file has header
# Default is No Header
output$cboxheader<-renderUI({
if(is.null(input$option))
return()
switch(input$option,
"Upload Data" = {
checkboxInput('header', "Header Present")
}
)
})
# Dynamic input element (2a) for "Upload Data" option
# Check box to specify if input file has header
# Default is No Header
output$hr<-renderUI({
if(is.null(input$option))
return()
switch(input$option,
"Upload Data" = {
tags$hr(style="height:1px;border-width:0;background-color:blue")
}
)
})
# Dynamic input element (3) for "Upload Data" option
# Check box to specify if Paired t-test is to be
# performed. Default is Two Group test
output$cboxpaired<-renderUI({
if(is.null(input$option))
return()
switch(input$option,
"Upload Data" = {
checkboxInput('paired', "Paired")
}
)
})
# Dynamic input element (3) for "Upload Data" option
# Check box to specify if Paired t-test is to be
# performed. Default is Two Group test
output$text<-renderUI({
if(is.null(input$option))
return()
switch(input$option,
"Upload Data" = {
"t-test options:"
}
)
})
# Dynamic input element (4) for "Upload Data" option
# Check box to specify if variance is equal in case
# of two group test. Default is not equal
output$cboxvar<-renderUI({
if(is.null(input$option))
return()
switch(input$option,
"Upload Data" = {
checkboxInput('variance', "Equal Variance")
}
)
})
# Dynamic input element (5) for "Upload Data" option
# Radio buttons to specify type of test: two sided
# or less than or greater than.
# Default is two sided
output$alternate<-renderUI ({
if(is.null(input$option))
return()
switch(input$option,
"Upload Data" = {
radioButtons("alternative", "Alternative Hypothesis",
c("Not Equal" = "two.sided",
"Less Than" = "less",
"Greater Than" = "greater"), selected = "two.sided")
}
)
})
# Dynamic input element (6) for "Upload Data" option
# Text message and link to sample data file for upload
output$linkmsg<-renderUI ({
if(is.null(input$option))
return()
switch(input$option,
"Upload Data" = {
withTags({
div(
p("A sample file tested with this application can be obtained from",
a("here.", href = "https://github.com/mmulchandani/ttest/blob/master/traintimeshead.csv",
target = "_blank"))
)
})
}
)
})
# The output is rendered in 3 parts. This is part 1
# It just display an introductory message. Part 2 below
# renders the data in a table format. Part 3 after that
# displays the P-value and a conclusion.
# TODO: Combine three parts into one.
output$contents<-renderUI({
if(is.null(input$option))
return()
switch(input$option,
"Welcome Message" = {
if(uploadOnce)
prevFilePath<<-""
withTags({
div(
h3("Welcome"),
p("Here are some notes on how to use this application. You can also access these notes
from the", b("Usage Guide"), "tab"),
p("When you are ready, please select an option from the drop down list"),
br(),
h3("Notes on using this Shiny App"),
br(),
p("This Shiny App demonstrates, using examples, some of typical applications of Student's t-test.
In particular, it demonstates the use of", b("Paired t-test"), "and", b("Two Sample t-test"), "and how
these tests can be used to compare two sets of data (either paired data or data from two groups) and determine
if there is a statistically significant difference between the two sets of data. It also has a provision
to enable users to upload their data and execute the appropriate t-test on the uploaded data."),
p("Information about Students's t-tests can be found ",
a("here", href = "http://en.wikipedia.org/wiki/Student%27s_t-test", target = "_blank")),
br(),
p("Demonstration of the application of t-test is done using the following three examples:"),
ol(li("Paired t-test"), li("Two Sample t-test"), li("Two Sample t-test using R's", b("mtcars"), "dataset")),
p("Users can use a drop down list to select the different examples, as shown in the image below"),
img(src = "pic8.png", width = 200, height = 400),
p(b("Note:"),"The fourth option in the drop down list enables the user to upload their data and run
t-test on the uploaded data. The", b("Upload File"), "file chooser becomes available once the user
selects to upload data. The user can specify if the file has a header or not (default: No header).
In addition to the file chooser, the App provides options to specify paired or two sample test
(default: two sample test), select the type of alternative hypothesis (default: Not equal or two sided),
and to specify if two sets of data have equal variance (default: not equal); note that the final option
applies only to the two sample test and does not affect the paired test."),
p(b("Note"), "that the app currently handles .csv file only. Only the first two rows of data and
the optional header are accessed."),
br(),
h5("Outputs"),
p("The output of the t-test is shown in the", b("Main"), "panel. A boxplot for the data is shown in
the", b("Boxplot"), "panel."),
br(),
p("Finally, please look up the", b("Limitations"), "tab that describes some of the limitations of this App.")
)
})
},
"Paired t-test Example" = {
if(uploadOnce)
prevFilePath<<-""
withTags({
div(class="intro",
h5("Paired t-test example"),
p("We are given data about mileage achieved by cars when using Regular
and Premium gasoline. The same car was tested on both types of gasoline and
mileage data was captured. Hence a Paired t-test is appropriate."),
p("The Null Hypothesis for this t-test is that gasoline type doesn't change the mileage yield.
The Alternative Hypothesis is that Premium Gasoline yields more miles than Regular gasoline."),
h5("Input Data:")
)
})
},
"Two Group t-test Example" = {
if(uploadOnce)
prevFilePath<<-""
withTags({
div(class="intro",
h5("Two Group t-test example"),
p("We are given data about response time (ms) of two groups of patients: one group was administered a drug
(treatment) and another group that was administered a placebo(control). Since two different groups are involved,
a two sample t-test is appropriate."),
p("The Null Hypothesis for this t-test is that the treatment doesn't impact response times.
The Alternative Hypothesis is that treatment does impact response times."),
h5("Input Data:")
)
})
},
"R Dataset: mtcars" = {
if(uploadOnce)
prevFilePath<<-""
withTags({
div(class="intro",
h5("Two Group t-test example for dataset mtcars"),
p("We use data from the ", b("mtcars"), " dataset to determine if there is difference between mileage yield
when comparing cars having automatic transmission with cars having manual transmission."),
p("The Null Hypothesis for this t-test is that there is no difference between mileage, while the
Alterate hypothesis is that there is difference between mileage yield of cars having manual transmission
as compared to cars having automatic transmission."),
h5("Input Data:"),
p(b("Note"), " that only the first 10 rows of data are shown, for brevity.")
)
})
},
"Upload Data" = {
if(is.null(input$inputFile)) {
print('H1')
print(prevFilePath)
withTags({
h5("AClick on the", b("Browse"), "button to select and upload a file")
})
}
else if (prevFilePath == "") {
print('H2')
withTags({
h5("BClick on the", b("Browse"), "button to select and upload a file")
})
}
else {
ttype<-"Two group t-test"
if (input$paired)
ttype<-"Paired t-test"
withTags({
div(class = "intro",
h5(ttype),
br(),
h5("Input Data:")
)
})
}
}
)
})
output$tbl<-renderTable({
if(is.null(input$option))
return()
data<-switch(input$option,
"Paired t-test Example" = {
rnames = c("Regular", "Premium")
cnames = c("Car1","Car2","Car3","Car4","Car5","Car6","Car7","Car8","Car9","Car10")
as.data.frame(matrix(c(reg,prem), nrow = 2, byrow = TRUE, dimnames = list(rnames, cnames)))
},
"Two Group t-test Example" = {
rnames = c("Control", "Treatment")
cnames = c("S1","S2","S3","S4","S5","S6")
as.data.frame(matrix(c(control,treat), nrow = 2, byrow = TRUE, dimnames = list(rnames, cnames)))
},
"R Dataset: mtcars" = {
d
},
"Upload Data" = {
if(is.null(input$inputFile)) {
print("Ma")
return()
}
else if (prevFilePath == "") {
print('Mb')
return()
}
print("Mc")
df<-read.csv(input$inputFile$datapath, header = input$header)
}
)
})
output$contents2<-renderUI({
if(is.null(input$option))
return()
switch(input$option,
"Paired t-test Example" = {
withTags({
div(
br(),
p(b("P Value:"), round(t.test(reg, prem, paired = TRUE, alternative = "less")$p.value,5)),
p("Since the p-value is less than 0.05, we reject the Null Hypothesis of no difference in
mileage yield obtained from Regular and Premium gasoline.")
)
})
},
"Two Group t-test Example" = {
withTags({
div(
br(),
p(b("P Value:"), round(t.test(control, treat, paired = FALSE, var.equal = TRUE, alternative = "less")$p.value,5)),
p("Since the p-value is less than 0.05, we reject the Null Hypothesis that there is no change
in response time due to treatment.")
)
})
},
"R Dataset: mtcars" = {
withTags({
div(
br(),
p(b("P Value:"), round(t.test(mtcars$mpg[mtcars$am == 1],mtcars$mpg[mtcars$am == 0],
paired = FALSE, alternative = 'two.sided', var.equal = FALSE)$p.value,5)),
p("Since the p-value is less than 0.05, we reject the Null Hypothesis that cars with automatic transmission
have same or similar mileage as those with manual transmission."),
p(b("Note:"), "This conclusion is based on just the t-test. Additional tests should be performed to
check for the presence of confounding variables to verify or overturn the outcome of this test.")
)
})
},
"Upload Data" = {
if(is.null(input$inputFile)) {
print("M3a")
return()
}
else if(prevFilePath == "") {
if(uploadOnce) {
prevFilePath<<-input$inputFile$datapath
}
print("M3b")
return()
}
print("M3c")
prevFilePath<<-input$inputFile$datapath
uploadOnce<<-TRUE
df1<-read.csv(input$inputFile$datapath, header = input$header)
ttestres<-t.test(as.numeric(df1[,1]),as.numeric(df1[,2]),
paired = input$paired, alternative = input$alternative, var.equal = input$variance)
withTags({
div(
br(),
p(b("T-statistic:"), round(ttestres$statistic,5)),
p(b("P Value:"), round(ttestres$p.value,5))
)
})
}
)
})
output$boxplot<-renderPlot({
if(is.null(input$option))
return()
switch(input$option,
"Paired t-test Example" = {
boxplot(reg - prem, col = 'tan', main = "Box Plot of Difference between Regular and Premium",
ylab = "Difference in Mileage", names = c("Difference"))
},
"Two Group t-test Example" = {
boxplot(control, treat, col = 'salmon', main = "Box Plot of Control vs Treatment",
ylab = "Response Time", names = c("Control", "Treatment"))
},
"R Dataset: mtcars" = {
boxplot(mtcars$mpg[mtcars$am == 0], mtcars$mpg[mtcars$am == 1], col = 'orange',
ylab = "Mileage", names = c("Automatic", "Manual"),
main = "Box Plot: Mileage of Automatic vs Manual Transmission")
},
"Upload Data" = {
if(is.null(input$inputFile))
return()
df1<-read.csv(input$inputFile$datapath, header = input$header)
if(!is.null(input$header)) {
colnames<-names(df1)
}
else {
colnames<-c("Var1", "Var2")
}
if(input$paired) {
if(input$header) {
title<-paste("Box Plot of Difference between", names(df1)[1], "and", names(df1)[2], sep = " ")
}
else {
title<-"Box Plot of Difference between two measurements"
}
boxplot(as.numeric(df1[1,]) - as.numeric(df1[2,]),
names = c("Difference"), ylab = "Value", col = 'lightblue', main = title)
}
else {
if(input$header) {
title<-paste("Box Plot of", names(df1)[1], "vs", names(df1)[2], sep = " ")
}
else {
title<-"Box Plot of Var1 vs Var2"
}
boxplot(as.numeric(df1[1,]),
as.numeric(df1[2,]),
names = colnames, ylab = "Value", col = 'lightblue', main = title)
}
}
)
})
# To switch to 'Main' tab, when a new option is selected
observe({
input$option
updateTabsetPanel(session, "maintabs", selected = "Main")
})
})