-
Notifications
You must be signed in to change notification settings - Fork 0
/
general_data_analyzer_integrated.R
1616 lines (1458 loc) · 68 KB
/
general_data_analyzer_integrated.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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#---------------------------------
#Packages
#---------------------------------
#User interface
if (require("shiny") == FALSE) {
install.packages("shiny")
}
library(shiny)
#Reading and extracting tables from Word documents
if (require("docxtractr") == FALSE) {
install.packages("docxtractr")
}
library(docxtractr)
#Making some data manipulation easier through functions present in these packages
if (require("tidyverse") == FALSE) {
install.packages("tidyverse")
}
if (require("gdata") == FALSE) {
install.packages("gdata")
}
library(tidyverse)
library(gdata)
#Writing the final excel files
if (require("openxlsx") == FALSE) {
install.packages("openxlsx")
}
library(openxlsx)
#---------------------------------
#Custom functions
#---------------------------------
#Function for adding suffixes in the user interface (for example the "st" in "1st")
ordinalSuffix <- function(x) {
if (x %% 100 %in% c(11, 12, 13)) {
suffix <- "th"
} else {
suffix <- switch(x %% 10,
"st",
"nd",
"rd",
"th"
)
}
paste0(x, suffix)
}
# Function for cleaning values
cleanAndConvert <- function(x) {
x <- gsub("[°C]", "", x)
x <- gsub("[J/g]", "", x)
x <- gsub("[%]", "", x)
x <- gsub("[mg]", "", x)
x <- gsub(",", ".", x)
}
ui <- navbarPage(
"Thermal Data Analyzer",
id = "navbar",
lang = "en",
#-----------------------------------------------------------
#Static user interface: user input tabs
#-----------------------------------------------------------
tabPanel(
title= " Analysis settings",
icon = icon("gears", class = "fa-solid"),
value = "analysisTab",
#---------------------------------------------------------------------------------------------------------------------------
#Static user interface: all of the styling is put in a tabPanel (see above), since putting it as a separate entity results in errors
#---------------------------------------------------------------------------------------------------------------------------
tags$head(
tags$style(
HTML("
/* Apply a background color to the entire page */
body {
background-color: #f4f7fa; /* Light blue-gray background */
color: #333; /* Dark gray text */
margin: 0;
padding: 0;
}
.main-header {
background-color: #3c8dbc;
color: white;
padding: 10px;
font-size: 20px;
font-weight: bold;
}
.secondary-header {
font-size: 18px;
color: #3c8dbc; /* Dark gray color */
font-weight: bold;
}
.ordered-list {
font-size: medium;
text-align: left;
color: #333;
}
.ordered-list li, .nested-list li {
padding-bottom: 5px;
}
.nested-list {
padding-left: 20px;
list-style-type: lower-alpha;
padding-bottom: 10px;
padding-top: 10px;
}
/* Ensure the navbar takes full width */
.navbar {
width: 100%;
}
/* Ensure the tab content takes full width */
.tab-content {
width: 100%;
}
/* Style the title panel with a blue background and white text */
.navbar-default .navbar-brand,
.navbar-default .navbar-nav>li>a {
color: #fff; /* White text */
}
.navbar-default {
background-color: #3c8dbc; /* Main blue color for the navbar */
}
/* Apply a white background to each tab panel */
.navbar-default .navbar-nav>li>a:hover,
.navbar-default .navbar-nav>li>a:focus,
.navbar-default .navbar-nav>li>a:active,
.navbar-default .navbar-nav>.open>a,
.navbar-default .navbar-nav>.open>a:hover,
.navbar-default .navbar-nav>.open>a:focus,
.navbar-default .navbar-nav>.open>a:active {
background-color: #fff; /* White background on hover and active */
color: #3c8dbc; /* Main blue text on hover and active */
}
/* Style the input section with a light blue background */
.tab-content {
background-color: #ecf5fb;
padding: 20px;
}
/* Style the text input and file input */
input[type='text'],
select {
width: 100%;
padding: 10px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}
/* Style the file input menu */
.file-input-label {
background-color: #2c3e50; /* Darker blue color for the label */
color: #fff; /* White text on the label */
border: none;
padding: 10px;
text-align: center;
cursor: pointer;
border-radius: 4px;
display: block;
width: 100%;
}
/* Style the 'Run Analysis' button */
#runAnalysis {
background-color: #2c3e50; /* Darker blue color for the button */
color: #fff; /* White text on the button */
border: none;
padding: 10px 30px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 25px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
margin-left: 50%;
}
/* Style the 'Next' button */
#Next {
background-color: #2c3e50; /* Darker blue color for the button */
color: #fff; /* White text on the button */
border: none;
padding: 10px 30px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 25px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
margin-left: 50%;
}
/* Style the analysis message */
#analysisMessageContainer {
font-size: 18px;
font-weight: bold;
padding: 10px;
width: 150%;
color: #3c8dbc
}
/* Style the error messages */
#errorMessageContainer {
font-size: 18px;
font-weight: bold;
padding: 10px;
width: 150%;
color: #e04f30
}
.nested-list table {
border-collapse: collapse;
width: 100%;
}
.nested-list th, .nested-list td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.nested-list-container {
padding-bottom: 20px;
}
")
)
),
#Actual input tabs are here---------------------------------------------------------------------------------------------------------------------------
fluidPage(
titlePanel(
tags$p(
style = "text-align: center; color: #3c8dbc;",
"Analysis settings: what runs did you perform?"),
windowTitle = "Thermal Data Analyzer"),
tags$br(),
tags$br(),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"Welcome to the Thermal Data Analyzer! It will help you analyse your thermal data by taking word documents as input and spitting out the cleaned up and analysed data in Excel tables. If you don't know how to start, navigate to the \"tutorial\" tab in the menu above. This page requires you to tell the app what your analysis looks like. Unsure about what you did exactly? Check the word documents! Press the \"next tab\" button once you're done."
),
tags$br(),
fluidRow(
column(
6,
selectInput(
"pans",
"How many pans did you run?",
choices = c("2", "3", "4", "5")),
selectInput(
"heatingCycle",
"How many heating cycles did you run?",
choices = c("1", "2", "3", "4")),
uiOutput("tablesDropdowns"),
checkboxInput(
"keepTitles",
"Are you happy with the titles you used for your table columns in your word documents? If no, uncheck this box.",
value = TRUE),
uiOutput("coltitlesInput"),
),
column(
6,
checkboxInput(
"saveRaw",
"Do you want to save the raw data in an excel file too?",
value = FALSE),
checkboxInput(
"round1",
"Do you want to have a different number of decimals than 2 in the final analysis output (raw data is never rounded)? If left unchecked, the program will round everything to 2 decimals.",
value = FALSE),
conditionalPanel(
condition = "input.saveRaw == true",
textInput("excelName2", "What should the excel sheet be called?")
),
conditionalPanel(
condition = "input.round1 == true",
selectInput("round", "To how many decimals should your results be rounded?", choices = c("0", "1", "2", "3", "4", "5")),
),
)
),
fluidRow(
column(
12,
mainPanel(
actionButton("Next", "Next tab")
),
)
),
)
),
tabPanel(
"Output and input files",
icon = icon("file-import", class = "fa-solid"),
value = "outputInputTab",
fluidPage(
titlePanel(
tags$p(style = "text-align: center; color: #3c8dbc;", "Input and output files"),
windowTitle = "DSC Data Analyzer"),
tags$br(),
tags$br(),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"You're almost good to go. In this tab, please upload the word documents to analyse. You also need to specify where the file is to be saved, and you'll need to give everything a suitable name. When you're all set, press \"Run Analysis\". Note that you can group several analyses in the same Excel file by simply changing the sheet name without closing the app (don't forget the raw data sheet as well in case you have one)."
),
tags$br(),
fluidRow(
column(
6,
fileInput(
"files",
"What files do you want to analyze?",
multiple = TRUE),
textInput(
"outputPath",
"Output Folder Path",
placeholder = "e.g., C:/Users/YourUsername/Documents"),
),
column(
6,
textInput("excelName", "Output Excel File Name"),
textInput("excelSheet", "Output Sheet Name"),
textInput("sampleName", "Sample Name"),
)
),
tags$br(),
fluidRow(
column(
12,
mainPanel(
actionButton("runAnalysis", "Run Analysis")
),
)
),
tags$br(),
fluidRow(
mainPanel(
div(
id = "analysisMessageContainer",
textOutput("analysisMessage")
)
)
),
fluidRow(
mainPanel(
div(
id = "errorMessageContainer",
textOutput("errorMessage")
)
)
),
)
),
#-----------------------------------------------------------
#Static user interface: tutorial tab
#-----------------------------------------------------------
tabPanel(
"Tutorial",
icon = icon("book", class = "fa-solid"),
value = "tutorialTab",
fluidPage(
titlePanel(tags$p(style = "text-align: left; color: #3c8dbc;", "DSC data analyzer tutorial"), windowTitle = "DSC Data Analyzer"),
tags$br(),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"Hey there fellow scientist! Welcome to the tutorial to the thermal data analyzer. This page gives some background on the app, gives some additional tips and tricks, and can be considered as a general user manual.
As a first important point, please note that this app was developed primarily for differential scanning calorimetry – hence the terminology, such as the constant use of heating cycle. However, if you want it to use for any other technique compatible with TRIOS, it works just as well! For any further questions not answered in the tutorial, contact me at tom.konings@kuleuven.be"
),
tags$br(),
tags$p(
style = "font-size: medium; text-align: left; color: #333; font-weight: bold",
"In case you came here to look for troubleshooting: most errors are covered and will result in an error message. However, one notable error that will still crash the app is having the Excel file you want to write to open when you execute the app. Close the Excel file and try again."
),
tags$br(),
tags$div(
class = "main-header",
"Basic principle of the app"
),
tags$br(),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"The app takes word documents as input and outputs formatted Excel tables. These Word files are generated using a semi-automised process in TRIOS, software by TA instruments (another section of the tutorial instructs you on how to do this). Word files are used because they are the most customizable and straightforward to generate in TRIOS. The input required in the various menus helps the program to separate your heating cycles. They are basically landmarks for the program. The main purpose of the program is to generate descriptive statistics for your data, but you can also export raw data."
),
tags$br(),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"Does it work? Yes it does! The program has been checked manually for accuracy. Moreover, if you happen to make a mistake in your input, the software will let you know using an error message. There are a few limitations left that are outlined in this tutorial (look for the title mentioning limiations)."
),
tags$br(),
tags$div(
class = "main-header",
"Steps in TRIOS"
),
tags$br(),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"The first thing you’ll need to do is generate the word documents you’ll feed into the app in TRIOS. Here is how you do it. This part assumes you know the TRIOS software well already, and is not a full tutorial for the software."
),
tags$ol(
class = "ordered-list",
tags$li("Open the files you want to analyze."),
tags$li("Don't split the files into the different cycles, but simply send each cycle to a new graph. Do so by right clicking on a step in the file manager on the left and selecting \"send to new graph\"."),
tags$li("Perform the analysis manually like you normally would for each heating or cooling cycle. When you save your analysis (see next point), TRIOS can distinguish the different cycles and knows what analysis was performed for which cycle."),
tags$li("Go to format > save analysis. This will save a file."),
tags$li("Now, go to format > generate report. A new screen pops up. You will see many options on the right side of the screen. In these options, you will find the analyses you did, grouped per heating cycle. Dragging one of the options (for example the onset of a certain integration you did) to the screen on the left displays the value. However, in order for the app to work, you’ll need to follow these very specific instructions."),
tags$ol(
class = "nested-list",
tags$li("Every event you want to analyze is one single table. For example, if you have a glass transition and a melting point of interest in heating cycle 1, you’ll have two tables for that heating cycle."),
tags$li("Implement tables in the TRIOS report file by clicking the “table” option in the top area."),
tags$li("Every table has one “title column”: the first (most left) column is always considered to contain some kind of title, so do not put any values there. Besides this limitation, you may have as many columns as you wish in your tables, and different tables can have different numbers of columns."),
tags$li("Every table has two rows. One is the “title row” containing more detailed information on the values in the row below. For example, you might want to put something like \"Tg onset (°C)\", \"Tg midpoint (°C)\", and \"Tg end (°C)\". The second row contains the values matching each title. Making your titles nicely from the start is helpful because the program can read those and use them for the output Excel."),
tags$li("The final result should look something like this, for every table:"),
div(
class = "nested-list-container",
tags$table(
class = "nested-list",
tags$tr(
tags$th("-general title-"),
tags$th("-title-"),
tags$th("-title-"),
tags$th("-title-"),
tags$th("-title-")
),
tags$tr(
tags$td("-nothing-"),
tags$td("-value-"),
tags$td("-value-"),
tags$td("-value-"),
tags$td("-value-")
)
)
),
tags$li(
style = "font-weight: bold;",
"Important note: when making your tables in TRIOS, you’ll see an option regarding table headers. DO NOT select this option, as otherwise the code will read your table as having just one row, violating the rule above stating that every table needs two rows. Also note that when you open the documents in word afterwards and change something in the tables, word might chagne the layout for some reason and still add a header row. The program will spot this and give an error, but be aware that this can cause this particular error."),
tags$li("As long as you follow the rules above, you may have as many tables as you wish and as many tables per heating cycle as you wish. There’s no further need for consistency.")
),
tags$li("Next, you will want to save your report as a template. Do this by clicking “save template” in the options at the top."),
tags$li("So far, everything was manual, but here is where the automation comes in. You can apply the saved analysis and the saved report template to a new file, and the analysis will be carried out automatically, including the generation of a report. As a matter of fact, you can also only save the report as a template and apply that directly. This is a bit quicker, but the downside is that you won't be able to drag new analysis elements to add new values in the report. If one sample requires slightly different integration limits, you can simply modify the values in the report by editing the analysis in the the tab that was generated when you applied the report template (see tab list on the bottom of the screen, -your sample title- (Report 1). The Word documents you just made semi-automatically serve as the app's input."),
tags$li("Export the reports you made as word documents by clicking the TRIOS logo on the top left, and you’re all set!"),
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"Note: something that happens when you apply an analysis to a file is that the curves are superposed. If you want to avoid this, pull the curves apart BEFORE conducting the analysis; this fixes the issue. This only works when you also apply an analysis template, not when you only apply a report template"
),
tags$br(),
tags$div(
class = "main-header",
"Installing and running the app"
),
tags$br(),
tags$div(
class = "secondary-header",
"Running the app by installing R on your computer"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"This is the more complicated option, but certainly the more practical once you get there since you don’t have to download/upload files to the cloud. Follow these steps, in exactly this order:"
),
tags$ol(
class = "ordered-list",
tags$li("Install Java. Make sure you pick the 64bit (x64) option if your system is x64 (offline version). This is the download link : ", tags$a(href = "https://www.java.com/en/download/manual.jsp", "https://www.java.com/en/download/manual.jsp"),"."),
tags$li("Install RTools: ", tags$a(href = "https://cran.r-project.org/bin/windows/Rtools/", "https://cran.r-project.org/bin/windows/Rtools/"),"."),
tags$li("Install R: follow the left link here: ", tags$a(href = "https://posit.co/download/rstudio-desktop/", "https://posit.co/download/rstudio-desktop/"),"."),
tags$li("Install RStudio (right side): ", tags$a(href = "https://posit.co/download/rstudio-desktop/", "https://posit.co/download/rstudio-desktop/"),"."),
tags$li("Open the code file. Running the code (Run App on the top right) will install all additional packages"),
),
tags$div(
class = "secondary-header",
"Running the app via the cloud"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"The easiest option for people without experience. The website that is best used for this is posit, the official R website. For you to be able to see the code after clicking the link in the GitHub Readme file, you’ll still need to select the correct file on the right.
If you’re running R locally (on your computer) and want to transition to the online version, you’ll need to remove all code setting the working directory (ctrl+ F to look up the command ‘setwd’). This is because you can’t change the working directory in posit: you'll need to download the analysed files from the environment (panel on the right)."
),
tags$br(),
tags$div(
class = "main-header",
"App features and limitations"
),
tags$br(),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"The whole point of the app is to calculate the means, standard deviations and relative standard deviations of the files you upload. It then groups the results per heating cycle, adds all relevant titles, rounds to the amount of decimals you tell it to, and writes a nicely formatted table to an Excel file. Here is an example of an output table:"
),
tags$table(
class = "data-table",
style = "width: 100%;",
tags$tr(
tags$th("-Your sample title-: heating cycle 1"),
tags$th("Means"),
tags$th("Standard Deviations"),
tags$th("Relative Standard Deviations")
),
tags$tr(
tags$td("Solvent Peak Onset (°C)"),
tags$td("8,75"),
tags$td("0,15"),
tags$td("1,66")
),
tags$tr(
tags$td("Solvent Peak location (°C)"),
tags$td("46,65"),
tags$td("0,99"),
tags$td("2,11")
),
tags$tr(
tags$td("Melting Peak Onset (°C)"),
tags$td("135,44"),
tags$td("0,19"),
tags$td("0,14")
),
tags$tr(
tags$td("Melting Peak location (°C)"),
tags$td("146,9"),
tags$td("0,05"),
tags$td("0,03")
),
tags$tr(
tags$td("Melting Peak enthalpy (J/g)"),
tags$td("17,4"),
tags$td("0,39"),
tags$td("2,22")
),
tags$tr(
tags$td("Tg Onset (°C)"),
tags$td("25,61"),
tags$td("0,28"),
tags$td("1,08")
),
tags$tr(
tags$td("Tg midpoint (°C)"),
tags$td("30,67"),
tags$td("1"),
tags$td("3,26")
),
tags$tr(
tags$td("Tg end (°C)"),
tags$td("34,93"),
tags$td("1,19"),
tags$td("3,41")
),
),
tags$br(),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"This is the main feature, and much of the app is focused around it. Your input boils down to the following:"
),
tags$ul(
class = "ordered-list",
tags$li("In the first tab (analysis settings):"),
tags$ol(
class = "ordered-list",
tags$li("How many heating cycles, pans, and tables per heating cycle you have. If you aren’t happy with the titles in each first row of every table that you generated via TRIOS, you can also untick the box asking about that and give custom titles. If you do this, you will need to input new titles for everything, however."),
tags$li("Whether you want to save the raw data as well"),
tags$li("The name of the excel sheet the data should be saved in"),
tags$li("Whether you want to round to two decimals (analysed data only, not the raw data)"),
tags$li("If the answer to the previous question was no, to how many decimals you want ro round"),
),
tags$li("In the second tab (input and output files)"),
tags$ol(
class = "ordered-list",
tags$li("The files you want to analyze, by uploading them."),
tags$li("The name of the Excel file."),
tags$li("The name of the Excel file sheet (if there is already an Excel file with the same name, but you change the sheet name, it will write to the same Excel but a different sheet!)."),
tags$li("The sample name: this is a name displayed at the top left of all the exported tables, for example “spray dried powder”. Since the results are grouped per heating cycle, a “heating cycle X” is added after the sample name for every table, where X varies between 1 and the number of heating cycles you have."),
tags$li("Where you want to export the excel file to, so a file directory."),
),
),
tags$div(
class = "secondary-header",
"Error handling"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"The code is able to deal with user error and report on it in a clear way. If, for example, you have one table with only 1 row or with 3 rows, you will get an error message explaining this upon running the code. If the number of tables you say you have in your document in the input doesn’t match with the actual number of tables, you’ll get another error, and so forth."
),
tags$div(
class = "secondary-header",
"Compatibility"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"The code is compatible with any word documents. TRIOS can analyze many different files in the manner described above, including DSC, TGA, DMA, etc. It can also analyze other CSV files. Finally, it can handle files from Universal Analysis as well. If you want to open data from Universal analysis, select \"Thermal data\" (bottom right) when opening files in TRIOS."
),
tags$div(
class = "secondary-header",
"Limitations"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"First and foremost, the tables must be consistent in the sense that the first column is never taken into account and the first row doesn’t contain any values, as mentioned above."
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"Another important point is the fact that whatever files you upload must be consistent within them. You cannot have one file where you have an additional melting peak, for example. The number of tables per file and number of columns for any given table must be the same in the different documents you calculate in order to calculate the mean."
),
tags$br(),
tags$div(
class = "main-header",
"How the code works"
),
tags$br(),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"This section gives a very short overview of different code snippets. If you want to know the details, go have a look at the code itself 😊. "
),
tags$div(
class = "secondary-header",
"Packages"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"Packages are essentially expansions of R that make life easier. They introduce new commands that don’t need to be coded all the way. In order to use packages, they need to be called with the (library) function."
),
tags$div(
class = "secondary-header",
"Functions"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
'There are two functions at the top of the code that are defined here and called later on. The first is clean and convert, which will remove all units from the data and convert everything to numeric values (in the original word document, everything is encoded as characters, which is a problem). The ordinal suffix helps generate the correct menus for the interactive user interface (for example if you have two heating cycles, it will ask “how many tables do you have in your 2nd heating cycle”. This function generates the "nd".'
),
tags$div(
class = "secondary-header",
"ShinyR"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"The whole app is based on ShinyR. ShinyR has three components:"
),
tags$ol(
class = "ordered-list",
tags$li("Code defining the user interface."),
tags$li("Server logic (the code that does the actual analysis). "),
tags$li("The line shinyApp(ui = ui, server = server)"),
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"Some important aspects of ShinyR are the reactive values and CSS code. Look into the relevant documentation if you wish to know more, as this is outside of the scope of this tutorial."
),
tags$div(
class = "secondary-header",
"Generating the general UI"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"CSS code and HTML are used to style and generate the user interface. This is also where parts like the three menus “input”, “methods” and “tutorial” are defined."
),
tags$div(
class = "secondary-header",
"Generating the interactive UI "
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"This piece of code changes the menus the user sees based on the values of other menus. For example, if you indicate having 3 heating cycles, you will be asked thrice about how many tables you have in each heating cycle. This is also for a large part where user input variables are defined. "
),
tags$div(
class = "secondary-header",
"Loading functionality"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"This small block of code defines values according to a template you loaded. "
),
tags$div(
class = "secondary-header",
"Extracting tables"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"This is the first actual analysis part of the code. Tables in word documents have assigned numbers in the xml file. You don’t see this, but the code can read this. This is the whole principle behind extracting the data.
From every “table 1” of every file, the second row of values is extracted. The rows thus extracted are grouped in one long vector called tempDf. tempDf is then cleaned and rendered numeric.
You’ll notice that everything works based on for-loops in the code. Important user inputs are the number of pans, the number of heating cycles, and the number of tables per heating cycles. The former two are values, while the latter is a vector. "
),
tags$div(
class = "secondary-header",
"Grouping tables into df"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"The different tempDf vectors are then grouped into a table called df. This works smoothly when df and tempDf are of the same length, but this is often not the case. Hence, when needed, NAs are inserted at the right locations so as not to influence the descriptive statistics."
),
tags$div(
class = "secondary-header",
"Grouping dfs into allCycles"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"All the dfs are then grouped into another data frame called allCycles, again taking into account different lengths. allCycles is printed in case the user wants to export their raw data as well. "
),
tags$div(
class = "secondary-header",
"Generating dataFrameCycle from df"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"DataFrameCycles is composed of different rows which are in turn composed of the different statistics. This is very easy to edit, and if reader wishes to have additional statistics (more than just means, SDs and relative SDs), they can edit this part relatively quickly. "
),
tags$div(
class = "secondary-header",
"Binding the dataFrameCycles to combinedStats"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"The different dataFrameCycles, which regroup the data per heating cycle, are combined into combinedStats."
),
tags$div(
class = "secondary-header",
"Generating the vectors containing the titles"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"The next block of code makes a list containing all the column titles, but only if the user indicates that they want to keep the titles of their original tables."
),
tags$div(
class = "secondary-header",
"Picking appropriate entries from combinedStats and grouping them per heating cycle, adding titles, writing to an excel"
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"Finally, data is extract back to several different data frames from combined stats using some arithmetic, is bound with the appropriate title vectors, and is written to an excel. "
),
tags$div(
class = "secondary-header",
"What’s left "
),
tags$p(
style = "font-size: medium; text-align: left; color: #333;",
"Other code snippets are mainly error- and exception handling. Error handling gives a clear output to the user in case they did something wrong, while exception handling ensures that the code can work no matter the data structure. For example, the rest of the code would sometimes cause issues when all the tables only have two columns."
),
)
)
)
#-----------------------------------------------------------
#Actual code (not stricly related to the UI) starts here
#----------------------------------------------------------
server <- function(input, output, session) {
#-----------------------------------------------------------------------------------
# Dynamic menus: include numTables, numColHeatingCycle, colTitles, Nrheatingcycles
#-----------------------------------------------------------------------------------
#Define numTables and colTitles as reactive values
numTables <- reactiveVal(NULL)
colTitles <- reactiveVal(NULL)
#Extract numCycles in the context of the dynamic dropdown menus - will be done again later for the rest of the code
output$tablesDropdowns <- renderUI({
numCycles <- as.numeric(input$heatingCycle)
# Generate dynamic dropdown menus based on the number of cycles selected
lapply(1:numCycles, function(i) {
selectInput(paste0("tables_cycle", i),
paste("How many tables do you have in your", ordinalSuffix(i), "heating cycle?"),
choices = 1:10
)
})
})
# Update numTables vector
observe({
numCycles <- as.numeric(input$heatingCycle)
numTables(sapply(1:numCycles, function(i) {
as.numeric(input[[paste0("tables_cycle", i)]])
}))
})
# Generation of the dynamic dropdown menus for column titles depending on the number of cycles and tables
output$coltitlesInput <- renderUI({
if (input$keepTitles == FALSE) {
numCycles <- as.numeric(input$heatingCycle)
if (length(numCycles) == 0) {
NULL
} else {
lapply(1:numCycles, function(i) {
numTables <- as.numeric(input[[paste0("tables_cycle", i)]])
if (length(numTables) == 0) {
NULL
} else {
lapply(1:numTables, function(j) {
textInput(paste0("coltitles_cycle", i, "_table", j),
paste("What are the column names in table", j, "of heating cycle", i, "?"),
placeholder = "Enter column names, separated by commas"
)
})
}
})
}
}
})
#Block coding for custom user input in case the user wants to manually change the titles in their table
observe({
if (input$keepTitles == FALSE) {
numCycles <- as.numeric(input$heatingCycle)
# Initialize a list to store colTitles
colTitlesList <- list()
# Update colTitles vector based on user inputs
if (length(numCycles) == 0) {
colTitles(NULL)
} else {
colTitlesList <- lapply(1:numCycles, function(i) {
numTables <- as.numeric(input[[paste0("tables_cycle", i)]])
if (length(numTables) == 0) {
NULL
} else {
unlist(sapply(1:numTables, function(j) {
# Split the input string at commas and remove leading/trailing spaces
if (!is.null(input[[paste0("coltitles_cycle", i, "_table", j)]])) {
columnNames <- strsplit(input[[paste0("coltitles_cycle", i, "_table", j)]], ",")[[1]]
columnNames <- trimws(columnNames)
columnNames <- unlist(columnNames, use.names = FALSE)
columnNames
} else {
NULL
}
}), use.names = FALSE) %>%
c()
}
})
# Update colTitles with the list of vectors
colTitles(colTitlesList)
}
}
})
#-----------------------------------------------------------------------------------
#Switch to "Output and input files settings" tab if Next is pressed
#-----------------------------------------------------------------------------------
observeEvent(input$Next, {
updateNavbarPage(session, "navbar", selected = "outputInputTab")
})
#-----------------------------------------------------------------------------------
observeEvent(input$runAnalysis, {
cat("Run analysis button clicked.\n")
#------------------------------------------------------------------------------------------------------------------------------------
# Reset outputmessages in case user runs multiple analyses and one of them results in an error
#------------------------------------------------------------------------------------------------------------------------------------
output$errorMessage <- renderText({
NULL
})
output$analysisMessage <- renderText({
NULL
})
#------------------------------------------------------------------------------------------------------------------------------------
# Extract variables: include files, numCycles, tableTitle, outputLocation, outputExcel, outputSheet, Number of pans, outputSheetRaw
#------------------------------------------------------------------------------------------------------------------------------------
# Extract uploaded files and get their paths
files <- input$files
filePaths <- files$datapath
# Counting given files and assigning file path to file1, file2, or file3 based on the loop iteration
fileCounter <- 0
for (i in seq_along(filePaths)) {
filePath <- filePaths[i]
inputName <- paste0("file", i)
assign(inputName, filePath)
fileCounter <- fileCounter + 1
}
# Extract other input data
numCycles <- as.numeric(input$heatingCycle)
tableTitle <- input$sampleName
outputLocation <- input$outputPath
outputExcel <- input$excelName
outputSheet <- input$excelSheet
pans <- as.numeric(input$pans)
outputSheetRaw <- input$excelName2
# Set rounding of the values according to the user input
if (input$round1 == FALSE) {
round <- 2
} else {
round <- as.numeric(input$round)
}
#---------------------------------------------------------------------------------------------------------------
# General error handling: several internal checks to make sure the user didn't make any mistakes.
# Output consists of clear error messages.
#---------------------------------------------------------------------------------------------------------------
# Check if files were uploaded
if (is.null(files)) {
print("Error: No files were uploaded. Please upload the input files.")
output$errorMessage <- renderText({
"Error: No files were uploaded. Please upload the input files."
})
return(NULL) # No files uploaded, exit the function
}
# Check if output location was given
if (is.null(outputLocation) || outputLocation == "") {
print("Error: Output location not specified. Please provide the output folder path.")
output$errorMessage <- renderText({
"Error: Output location not specified. Please provide the output folder path."
})
return(NULL)
}
#Check if uploaded files are indeed word documents
for (p in 1:pans) {
ext <- tools::file_ext(get(paste0("file", p)))
valid_ext <- tolower(ext) %in% c("docx", "doc")
if (valid_ext) {
} else {
print("Error: It seems you didn't just upload word documents, but at least one document is another type of file.")
output$errorMessage <- renderText({
"Error: It seems you didn't just upload word documents, but at least one document is another type of file."
})
return(NULL)
}
}
# Set working directory for excel files
if (dir.exists(outputLocation)) {
setwd(outputLocation)
cat("Changed working directory to:", outputLocation, "\n")
} else {
output$errorMessage <- renderText({
"Error: Excel file output directory does not exist or cannot be accessed."
})
return(NULL)
}
# Check if outputExcel was given
if (is.null(outputExcel) || outputExcel == "") {
print("Error: Output file name not specified. Please provide the name of the output Excel file.")
output$errorMessage <- renderText({
"Error: Output file name not specified. Please provide the name of the output Excel file."
})
return(NULL)
}
# Check if the extension is already present
if (!grepl("\\.xlsx$", outputExcel)) {
outExcel <- paste0(outputExcel, ".xlsx") # Add .xlsx extension
} else {
outExcel <- outputExcel
}
# Check if given outputSheet already exists
if (file.exists(outExcel)) {
sheet_names <- getSheetNames(outExcel)
if (outputSheet %in% sheet_names) {
print("Error: The Sheet you want to write to already exists in an excel file with the name you specified. Please choose a different file name or a different sheet name if you want another sheet to be written to the same excel.")