-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.R
2463 lines (1750 loc) · 89 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
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
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# PlotTwist: Shiny app for plotting and comparing continuous data, with a focus on time-dependent data
# Created by Joachim Goedhart (@joachimgoedhart), first version 2018
# Takes non-tidy, spreadsheet type data as input assuming first column is "Time"
# Non-tidy data is converted into tidy format
# Raw data is displayed with user-defined visibility (alpha)
# The mean is displayed with user-defined visibility (alpha)
# Inferential statistics (95%CI) can be displayed as ribbon
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Copyright (C) 2019 Joachim Goedhart
# electronic mail address: j #dot# goedhart #at# uva #dot# nl
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ToDo
# Print variables on the axis from the tidy column names
# Optimize facetting of heatmap (complicated, especially in combination with annotation)
# Add option to invert colors of heatmap?
# look into Partitioning Around Medoids -> pam{cluster}
# look into fuzzy clustering
# Correlation-based distance matrix: http://girke.bioinformatics.ucr.edu/GEN242/pages/mydoc/Rclustering.html
options(shiny.maxRequestSize=90*1024^2)
library(shiny)
library(ggplot2)
library(dplyr)
library(tidyr)
library(readr)
library(magrittr)
library(readxl)
library(DT)
library(dtw)
library(ggrepel)
library(shinycssloaders)
library(RCurl)
#library(fpc)
library(NbClust)
#library(TSclust)
#library(tsmp)
library(gridExtra)
source("themes.R")
#Confidence level
Confidence_Percentage = 95
Confidence_level = Confidence_Percentage/100
#Several qualitative color palettes that are colorblind friendly
#Code to generate vectors in R to use these palettes
#From Paul Tol: https://personal.sron.nl/~pault/
Tol_bright <- c('#EE6677', '#228833', '#4477AA', '#CCBB44', '#66CCEE', '#AA3377', '#BBBBBB')
Tol_muted <- c('#88CCEE', '#44AA99', '#117733', '#332288', '#DDCC77', '#999933','#CC6677', '#882255', '#AA4499', '#DDDDDD')
Tol_light <- c('#BBCC33', '#AAAA00', '#77AADD', '#EE8866', '#EEDD88', '#FFAABB', '#99DDFF', '#44BB99', '#DDDDDD')
#From Color Universal Design (CUD): https://jfly.uni-koeln.de/color/
Okabe_Ito <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#000000")
#Read a text file with demo data (comma separated values)
df_wide_example <- read.csv("Data_wide_example_time_single.csv", na.strings = "")
df_tidy_example <- read.csv("Data_tidy_example_time_multi.csv")
# Create a reactive object here that we can share between all the sessions.
vals <- reactiveValues(count=0)
###### UI: User interface #########
ui <- fluidPage(
titlePanel("PlotTwist - a web app for plotting continuous data"),
sidebarLayout(
sidebarPanel(width=3,
conditionalPanel(
condition = "input.tabs=='Plot'",
h4("Data"),
radioButtons("data_form", "Data as:", choices = list("Lines" = "dataasline", "Dots" = "dataasdot", "Lines & Dots"="dataasboth","Heatmap" = "dataaspixel"), selected = "dataasline"),
conditionalPanel(condition = "input.data_form != 'dataaspixel'",
checkboxInput("multiples", "Small multiples", value = FALSE),
checkboxInput("thicken", "The plot thickens", value = FALSE),
sliderInput("alphaInput", "Visibility of the data", 0, 1, 0.3),
h4("Statistics"),
sliderInput("alphaInput_summ", "Visibility of the statistics", 0, 1, 1),
checkboxInput("summaryInput", "Show the mean", value=FALSE),
# sliderInput("Input_CI", "Confidence Level", 90, 100, 95),
checkboxInput(inputId = "add_CI", label = HTML("Show the 95% CI"), value = FALSE),
NULL
),
h4("Plot Layout"),
checkboxInput(inputId = "change_scale",
label = "Change scale",
value = FALSE),
conditionalPanel(
condition = "input.change_scale == true",
textInput("range_x", "Range x-axis (min,max)", value = "")
),
conditionalPanel(
condition = "input.change_scale == true && input.data_form !='dataaspixel'",
textInput("range_y", "Range y-axis (min,max)", value = ""),
checkboxInput(inputId = "scale_log_10",
label = "Log scale",
value = FALSE)
),
conditionalPanel(
condition = "input.change_scale == true && input.data_form =='dataaspixel'",
textInput("range_y2", "Range of the signal (min,max)", value = "")
),
conditionalPanel(condition = "input.data_form != 'dataaspixel'",
checkboxInput(inputId = "no_grid",
label = "Remove gridlines",
value = FALSE),
checkboxInput("color_data", "Use color for the data", value=FALSE),
checkboxInput("color_stats", "Use color for the stats", value=FALSE),
# selectInput("colour_list", "Colour:", choices = ""),
conditionalPanel(condition = "input.color_data == true || input.color_stats == true",
radioButtons("adjustcolors", "Color palette:", choices =
list(
#"Standard" = 1,
"Okabe&Ito; CUD" = 6,
"Tol; bright" = 2,
"Tol; muted" = 3,
"Tol; light" = 4,
"Viridis" = 7,
"User defined"=5),
selected = 6),
conditionalPanel(condition = "input.adjustcolors == 5",
textInput("user_color_list", "List of names or hexadecimal codes", value = "turquoise2,#FF2222,lawngreen")),
h5("",
a("Click here for more info on color names",
href = "http://www.endmemo.com/program/R/color.php", target="_blank"))
),
checkboxInput(inputId = "dark", label = "Dark Theme", value = FALSE),
NULL
),
conditionalPanel(condition = "input.data_form == 'dataaspixel'",
radioButtons(inputId = "ordered",
label= "Order of the lines:",
choices = list("Alphabetical" = "none", "By maximum value" = "max_int", "By amplitude" = "amplitude", "By integrated response" = "int_int", "From hierarchical clustering" = "hc"),
selected = "none")
),
numericInput("plot_height", "Height (# pixels): ", value = 480),
numericInput("plot_width", "Width (# pixels):", value = 600),
NULL),
####################### UI Panel for Data Upload ###############
conditionalPanel(
condition = "input.tabs=='Data upload'",
h4("Data upload (First column used for x-axis)"),
radioButtons(
"data_input", "",
choices =
list("Example 1 (wide format)" = 1,
"Example 2 (tidy format)" = 2,
"Upload (multiple) file(s)" = 3,
"Paste data" = 4,
"URL (csv files only)" = 5,
"Copy&Paste from FIJI"=6)
,
selected = 1),
conditionalPanel(
condition = "input.data_input=='3'",
h5(""),
fileInput("upload", "Each file is a group:", multiple = TRUE),
selectInput("file_type", "Type of file:",
list("text (csv)" = "text",
"Excel" = "Excel"
),
selected = "text")
),
#########
conditionalPanel(
condition = "input.data_input=='4'",
h5("Paste data below:"),
tags$textarea(id = "data_paste",
placeholder = "Add data here",
rows = 10,
cols = 20, ""),
actionButton("submit_data_button", "Submit data"),
radioButtons(
"text_delim", "Delimiter",
choices =
list("Tab (from Excel)" = "\t",
"Space" = " ",
"Comma" = ",",
"Semicolon" = ";"),
selected = "\t")),
### csv via URL as input
conditionalPanel(
condition = "input.data_input=='5'",
# textInput("URL", "URL", value = "https://zenodo.org/record/2545922/files/FRET-efficiency_mTq2.csv"),
textInput("URL", "URL", value = ""),
NULL
),
########
conditionalPanel(
condition = "input.data_input=='6'",
h5("Paste data below:"),
tags$textarea(id = "data_paste2",
placeholder = "Add data here",
rows = 10,
cols = 20, "")),
#
checkboxInput(inputId = "tidyInput",
label = "These data are Tidy",
value = FALSE),
conditionalPanel(
condition = "input.tidyInput==false", selectInput("data_remove", "Deselect these columns:", "", multiple = TRUE)),
conditionalPanel(condition = "input.tidyInput==true",
selectInput("x_var", "Select variable for x-axis", choices = ""),
selectInput("y_var", "Select variable for y-axis", choices = ""),
selectInput("g_var", "Identifier of samples", choices = ""),
selectInput("c_var", "Identifier of conditions", choices = ""),
selectInput("filter_column", "Filter based on this parameter:", choices = ""),
selectInput("remove_these_conditions", "Deselect these conditions:", "", multiple = TRUE)
),
downloadButton("downloadData", "Download (tidy) data (csv)"),
hr(),
h4("Normalization"),
checkboxInput(inputId = "normalization",
label = "Data normalization",
value = FALSE),
conditionalPanel(condition = "input.normalization==true",
radioButtons("norm_type",
"Method:",
choices = list("Divide by maximal value" = "max",
"Divide by minimal value" = "min",
"Rescale between 0 and 1" = "zero_one",
"Divide by area/integrated response (I/sum(I))" = "integral",
"Difference from baseline (I-I0)" = "diff",
"Z-score ((I-I0)/SD(I0))" = "z-score",
"Difference divided by baseline ((I-I0)/I0)" = "perc",
"Fold change over baseline (I/I0)" = "fold"),
selected = "fold"),
conditionalPanel(
condition = "input.norm_type=='fold' || input.norm_type=='perc' || input.norm_type=='diff' || input.norm_type=='z-score' ",
textInput("base_range", "Define baseline by rownumbers (start,end)", value = "1,5"))
),
conditionalPanel(
condition = "input.normalization==true", (downloadButton("downloadNormalizedData", "Download normalized tidy data (csv)"))),
hr(),
checkboxInput(inputId = "info_data",
label = "Show information on data formats",
value = FALSE),
conditionalPanel(
condition = "input.info_data==true",
img(src = 'Data_format.png', width = '100%'), h5(""), a("Information on converting wide data to the tidy format", href = "http://thenode.biologists.com/converting-excellent-spreadsheets-tidy-data/education/")
),
NULL
),
####################### UI Panel for Clustering ###############
conditionalPanel(
condition = "input.tabs=='Clustering'",
h4("Clustering"),
radioButtons(inputId = "method",
label= "Clustering method:",
choices = list("Euclidean distance" = "euclidean", "Manhattan distance"="manhattan", "Dynamic Time Warping" ="DTW", "k-means" = "kmeans"),
selected = "euclidean"),
conditionalPanel(
condition = "input.method != 'kmeans'",
radioButtons(inputId = "linkage",
label= "Linkage method:",
choices = list("Ward.D2"="ward.D2", "Centroid" ="centroid", "Average"="average", "Complete" = "complete"),
selected = "ward.D2")
),
checkboxInput(inputId = "show_proportions",
label = "Show contributions per condition",
value = FALSE),
numericInput ("groups", "Number of clusters/groups:", value=2, min = 1, max = 100, step = 1),
checkboxInput(inputId = "validate_clusters",
label = "Verify clustering",
value = FALSE),
conditionalPanel(
condition = "input.validate_clusters==true",
radioButtons(inputId = "cvi",
label= "Cluster Validation Index (higher is better)",
choices = list("Calinski Harabasz index" = "ch", "Dunn's index" ="dunn", "Silhouette index"="silhouette"),
selected = "ch"),
# h5(" Verify # of clusters:"),
tableOutput("cvi")
),
hr(),
sliderInput("limits", "Set lower and upper limit of x-axis", 0, 120, value=c(1,100)),
numericInput ("binning", "Binning of x-axis (1=no binning):", value=1, min = 1, max = 100, step = 1),
h4("Plot Layout"),
textInput("range_y3", "Y-axis (min,max); blank for autoscaling", value = ""),
numericInput("clusterplot_height", "Height (# pixels): ", value = 350),
numericInput("clusterplot_width", "Width (# pixels):", value = 600),
downloadButton("downloadClusteredData", "Download clustered data (csv)"),
NULL ####### End of Cluster UI #######
),
conditionalPanel(condition = "input.tabs=='Plot'",
h4("Labels"),
checkboxInput(inputId = "indicate_stim",
label = "Add treatment/condition",
value = FALSE),
conditionalPanel(
condition = "input.indicate_stim == true",
radioButtons(inputId = "stim_shape",
label = NULL, choices = list("Bar on top" = "bar", "Box in plot" = "box", "Bar&Box"="both"), selected = "bar"),
textInput("stim_range", "Range of grey box (from,to,from,to,...)", value = "46,146"),
textInput("stim_text", "Text (condition1, condition2,...)", value = ""),
textInput("stim_colors", "Colors (condition1, condition2,...)", value = ""),
NULL),
checkboxInput(inputId = "add_title",
label = "Add title",
value = FALSE),
conditionalPanel(
condition = "input.add_title == true",
textInput("title", "Title:", value = "")
),
checkboxInput(inputId = "label_axes",
label = "Change axis labels",
value = FALSE),
conditionalPanel(
condition = "input.label_axes == true",
textInput("lab_x", "X-axis:", value = ""),
textInput("lab_y", "Y-axis:", value = "")
),
checkboxInput(inputId = "adj_fnt_sz",
label = "Change font size",
value = FALSE),
conditionalPanel(
condition = "input.adj_fnt_sz == true",
numericInput("fnt_sz_title", "Plot title:", value = 24),
numericInput("fnt_sz_labs", "Axis titles:", value = 24),
numericInput("fnt_sz_ax", "Axis labels:", value = 18),
numericInput("fnt_sz_stim", "Treatment/condition labels:", value = 8)
),
conditionalPanel(
condition = "input.color_data == true || input.color_stats == true || input.data_form=='dataaspixel'",
checkboxInput(inputId = "add_legend",
label = "Add legend",
value = FALSE)),
conditionalPanel(
condition = "input.add_legend == true",
textInput("legend_title", "Legend title:", value = "")
),
checkboxInput("show_labels_y", "Add labels to objects", value=FALSE),
NULL
),
conditionalPanel(
condition = "input.tabs=='About'",
#Session counter: https://gist.github.com/trestletech/9926129
h4("About"), "There are currently",
verbatimTextOutput("count"),
"session(s) connected to this app.",
hr(),
h4("Find our other dataViz apps at:"),a("https://huygens.science.uva.nl/", href = "https://huygens.science.uva.nl/")
),
conditionalPanel(
condition = "input.tabs=='Data Summary'",
h4("Data summary")
)
),
mainPanel(
tabsetPanel(id="tabs",
tabPanel("Data upload", h4("Data as provided"), dataTableOutput("data_uploaded")),
tabPanel("Plot", downloadButton("downloadPlotPDF", "Download pdf-file"),
downloadButton("downloadPlotEPS", "Download eps-file"),
downloadButton("downloadPlotSVG", "Download svg-file"),
downloadButton("downloadPlotPNG", "Download png-file"),
actionButton("settings_copy", icon = icon("clone"),
label = "Clone current setting"),
div(`data-spy`="affix", `data-offset-top`="10", withSpinner(plotOutput("coolplot", height="100%"))),
# htmlOutput("LegendText", width="200px", inline =FALSE),
NULL
),
tabPanel("Clustering", downloadButton("downloadClusteringPDF", "Download pdf-file"), downloadButton("downloadClusteringPNG", "Download png-file"),
withSpinner(plotOutput("plot_clust")),
#div(`data-spy`="affix", `data-offset-top`="10", plotOutput("plot_clust", height="100%")),
NULL
),
tabPanel("Data Summary", tableOutput('data_summary')),
tabPanel("About", includeHTML("about.html")
)
)
)
)
)
server <- function(input, output, session) {
isolate(vals$count <- vals$count + 1)
vals$Datum <- FALSE
x_var.selected <- "Time"
y_var.selected <- "Value"
c_var.selected <- "id"
g_var.selected <- "Sample"
##################### Synchronize scales between tabs ##################
observeEvent(input$change_scale, {
if (input$change_scale==TRUE) {
updateCheckboxInput(session, "change_scale2", value = TRUE)
} else if (input$change_scale==FALSE) {
updateCheckboxInput(session, "change_scale2", value = FALSE)
}
})
observeEvent(input$change_scale2, {
if (input$change_scale2==TRUE) {
updateCheckboxInput(session, "change_scale", value = TRUE)
} else if (input$change_scale2==FALSE) {
updateCheckboxInput(session, "change_scale", value = FALSE)
}
})
###### DATA INPUT ###################
df_upload <- reactive({
if (input$data_input == 1) {
data <- df_wide_example
data$id <- "1"
} else if (input$data_input == 2) {
#
updateSelectInput(session, "tidyInput", selected = TRUE)
data <- df_tidy_example
x_var.selected <<- "Time"
y_var.selected <<- "Value"
c_var.selected <<- "id"
g_var.selected <<- "Sample"
} else if (input$data_input == 3) {
if (is.null(input$upload)) {
return(data.frame(x = "Select your datafile", Time=1,Cell=1, id=1))
} else {
isolate({
if (input$file_type == "text") {
#Read the selected files (with read.csv)
df_input_list <- lapply(input$upload$datapath, function(x) read.csv(x, na.strings = c("",".","NA", "NaN", "#N/A", "#VALUE!", "#DIV/0!")))
} else if (input$file_type == "Excel"){
df_input_list <- lapply(input$upload$datapath, read_excel)
}
#Take the filenames input$cvcs$name and remove anything after "." to get rid of extension
names(df_input_list) <- gsub(input$upload$name, pattern="\\..*", replacement="")
observe({ print(names(df_input_list)) })
df_input <- bind_rows(df_input_list, .id = "ids")
#If there is no id columns add it
if(!"id" %in% colnames(df_input)) {
df_input <- df_input %>% rename(id =ids)
}
#If the uploaded has an id column, use it
else if("id" %in% colnames(df_input)) {
df_input <- df_input %>% select(-one_of("ids"))
}
#Force the first column from the csv file to be labeled as "Time" if this columns is absent
if(input$tidyInput == FALSE && !"Time" %in% colnames(df_input)) {
names(df_input)[2] <- "Time"
}
data <- df_input
})
}
} else if (input$data_input == 5) {
#Read data from a URL
#This requires RCurl
if(input$URL == "") {
return(data.frame(x = "Enter a full HTML address, for example: https://raw.githubusercontent.com/JoachimGoedhart/PlotTwist/master/PlotTwist_tidy-RGS.csv"))
} else if (url.exists(input$URL) == FALSE) {
return(data.frame(x = paste("Not a valid URL: ",input$URL)))
} else {data <- read_csv(input$URL)}
if(input$tidyInput == FALSE ) {
data$id <- "1"
}
} else if (input$data_input == 6) {
if (input$data_paste2 == "") {
data <- data.frame(x = "Paste your data into the textbox", Time="1", id="1")
} else {
isolate({
data <- read_delim(input$data_paste2,
delim = '\t',
col_names = TRUE)
#Check that data has at least 2 columns (1st is Time) and 2 rows (upper row is header)
if (ncol(data)<2 || nrow(data)<1) {return(data.frame(x = "Number of columns and rows should be >2", Time="1", id="1"))}
#The first column is defined as Time, id is added for compatibility
#Check that one of the column names is "label"
if ("Label" %in% colnames(data) == FALSE) {return(data.frame(x = "Make sure that column names are copied in FIJI and that 'Display label' is checked in 'Set Measurements'", Time="1", id="1"))}
# Seperate 'Label' and rename columns
data <- data %>% separate(Label, c('file','Sample','frame'), sep = ':')
#
# # Make sure that x- and y-variable are numbers
data <- data %>% mutate (Slice = as.numeric(Slice), Mean = as.numeric(Mean))
#
updateSelectInput(session, "tidyInput", selected = TRUE)
#
data$id <- "1"
x_var.selected <<- "Slice"
y_var.selected <<- "Mean"
c_var.selected <<- "id"
g_var.selected <<- "Sample"
})
}
} else if (input$data_input == 4) {
if (input$data_paste == "" || input$submit_data_button == 0) {
data <- data.frame(x = "Copy your data into the textbox,
select the appropriate delimiter, and
press 'Submit data'", Time="1", id="1")
} else {
isolate({
data <- read_delim(input$data_paste,
delim = input$text_delim,
col_names = TRUE)
#Check that data has at least 2 columns (1st is Time) and 2 rows (upper row is header)
if (ncol(data)<2 || nrow(data)<1) {return(data.frame(x = "Number of columns and rows should be >2", Time="1", id="1"))}
#The first column is defined as Time, id is added for compatibility
names(data)[1] <- "Time"
data$id <- "1"
})
}
}
# }
columns_to_remove <- names(data)
#Remove column Time
columns_to_remove <- columns_to_remove[columns_to_remove != "Time"]
#Remove last column (id)
columns_to_remove <- columns_to_remove[columns_to_remove != "id"]
#Show the columns that can be removed
updateSelectInput(session, "data_remove", choices = columns_to_remove)
#ImageJ specific: seperate the column "Label" in multiple columns
if("Label" %in% colnames(data)) {
data <- data %>% separate(Label,c("filename", "Sample","Number"),sep=':')
}
observe({print(head(data))})
return(data)
})
################## DISPLAY UPLOADED DATA (exactly as provided ##################
output$data_uploaded <- renderDataTable({
# observe({ print(input$tidyInput) })
df_filtered()
})
################ REMOVE SELECTED COLUMNS #########
df_filtered <- reactive({
###### REMOVE Columns from wide DATA FRAME ######
if (!is.null(input$data_remove)) {
columns = input$data_remove
df <- df_upload() %>% select(-one_of(columns))
###### FILTER VARIABLES FROM TIDY DATA FRAME ######
} else if (!is.null(input$remove_these_conditions) && input$filter_column != "none") {
filter_column <- input$filter_column
remove_these_conditions <- input$remove_these_conditions
observe({print(remove_these_conditions)})
#Remove the columns that are selected (using filter() with the exclamation mark preceding the condition)
# https://dplyr.tidyverse.org/reference/filter.html
df <- df_upload() %>% filter(!.data[[filter_column[[1]]]] %in% !!remove_these_conditions)
} else {df <- df_upload()}
#Replace space and dot of header names by underscore
df <- df %>%
select_all(~gsub("\\s+|\\.", "_", .))
})
##### CONVERT TO TIDY DATA ##########
#Need to tidy the data?!
#Untidy data will be converted to long format with two columns named 'Condition' and 'Value'
#The input for "Condition" will be taken from the header, i.e. first row
#Tidy data will be used as supplied
df_upload_tidy <- reactive({
koos <- df_upload()
if(input$tidyInput == FALSE ) {
koos <- df_filtered()
klaas <- gather(koos, Sample, Value, -Time, -id)
klaas <- klaas %>% mutate (Time = as.numeric(Time), Value = as.numeric(Value))
}
else if(input$tidyInput == TRUE ) {
# klaas <- koos
klaas <- df_filtered()
}
return(klaas)
})
##### Get Variables from the input ##############
observe({
# var_names <- names(df_upload_tidy())
var_names <- names(df_upload())
var_list <- c("none", var_names)
# updateSelectInput(session, "colour_list", choices = var_list)
updateSelectInput(session, "y_var", choices = var_list, selected=y_var.selected)
updateSelectInput(session, "x_var", choices = var_list, selected=x_var.selected)
updateSelectInput(session, "c_var", choices = var_list, selected=c_var.selected)
updateSelectInput(session, "g_var", choices = var_list, selected=g_var.selected)
updateSelectInput(session, "filter_column", choices = var_list, selected="none")
})
########### When x_var is selected for tidy data, get the list of conditions
observeEvent(input$x_var != 'none' && input$y_var != 'none' && input$filter_column != 'none', {
if (input$filter_column != 'none') {
filter_column <- input$filter_column
if (filter_column == "") {filter_column <- NULL}
koos <- df_upload_tidy() %>% select(for_filtering = !!filter_column)
conditions_list <- levels(factor(koos$for_filtering))
# observe(print((conditions_list)))
updateSelectInput(session, "remove_these_conditions", choices = conditions_list)
}
})
observeEvent(input$tabs, {
# Only after data upload and pressing the "Clustering" tab the min and max valeu for time will be determined and used to update the "Trim" slider
if (input$tabs == "Clustering") {
# observe({print("Cluster tab selected")})
klaas <- df_selected()
# klaas <- df_upload_tidy()
max_Time <- max(klaas$Time)
min_Time <- min(klaas$Time)
updateSliderInput(session, "limits", min = min_Time, max =max_Time, value=c(min_Time, max_Time))
}
})
########### GET INPUT VARIABLEs FROM HTML ##############
observe({
############ ?data ################
query <- parseQueryString(session$clientData$url_search)
if (!is.null(query[['data']])) {
presets_data <- query[['data']]
presets_data <- unlist(strsplit(presets_data,";"))
# observe(print((presets_data)))
updateRadioButtons(session, "data_input", selected = presets_data[1])
updateCheckboxInput(session, "tidyInput", value = presets_data[2])
updateCheckboxInput(session, "normalization", value = presets_data[3])
updateRadioButtons(session, "norm_type", selected = presets_data[4])
updateTextInput(session, "base_range", value= presets_data[5])
if (presets_data[1] == "1" || presets_data[1] == "2" || presets_data[1] == "6") {
updateTabsetPanel(session, "tabs", selected = "Plot")
}
}
############ ?vis ################
if (!is.null(query[['vis']])) {
presets_vis <- query[['vis']]
presets_vis <- unlist(strsplit(presets_vis,";"))
observe(print((presets_vis)))
updateRadioButtons(session, "data_form", selected = presets_vis[1])
updateSliderInput(session, "alphaInput", value = presets_vis[2])
updateRadioButtons(session, "summaryInput", selected = presets_vis[3])
updateCheckboxInput(session, "add_CI", value = presets_vis[4])
updateSliderInput(session, "alphaInput_summ", value = presets_vis[5])
updateCheckboxInput(session, "multiples", value = presets_vis[6])
updateCheckboxInput(session, "thicken", value = presets_vis[7])
}
############ ?layout ################
if (!is.null(query[['layout']])) {
presets_layout <- query[['layout']]
presets_layout <- unlist(strsplit(presets_layout,";"))
observe(print((presets_layout)))
updateCheckboxInput(session, "no_grid", value = (presets_layout[2]))
updateCheckboxInput(session, "change_scale", value = presets_layout[3])
updateTextInput(session, "range_x", value= presets_layout[4])
updateTextInput(session, "range_y", value= presets_layout[5])
updateCheckboxInput(session, "color_data", value = presets_layout[6])
updateCheckboxInput(session, "color_stats", value = presets_layout[7])
updateRadioButtons(session, "adjustcolors", selected = presets_layout[8])
# updateCheckboxInput(session, "add_description", value = presets_layout[9])
if (length(presets_layout)>10) {
updateNumericInput(session, "plot_height", value= presets_layout[10])
updateNumericInput(session, "plot_width", value= presets_layout[11])
}
# updateTabsetPanel(session, "tabs", selected = "Plot")
}
############ ?color ################
if (!is.null(query[['color']])) {
presets_color <- query[['color']]
presets_color <- unlist(strsplit(presets_color,";"))
updateSelectInput(session, "colour_list", selected = presets_color[1])
updateTextInput(session, "user_color_list", value= presets_color[2])
}
############ ?label ################
if (!is.null(query[['label']])) {
presets_label <- query[['label']]
presets_label <- unlist(strsplit(presets_label,";"))
observe(print((presets_label)))
updateCheckboxInput(session, "add_title", value = presets_label[1])
updateTextInput(session, "title", value= presets_label[2])
updateCheckboxInput(session, "label_axes", value = presets_label[3])
updateTextInput(session, "lab_x", value= presets_label[4])
updateTextInput(session, "lab_y", value= presets_label[5])
updateCheckboxInput(session, "adj_fnt_sz", value = presets_label[6])
updateNumericInput(session, "fnt_sz_title", value= presets_label[7])
updateNumericInput(session, "fnt_sz_labs", value= presets_label[8])
updateNumericInput(session, "fnt_sz_ax", value= presets_label[9])
updateNumericInput(session, "fnt_sz_stim", value= presets_label[10])
updateCheckboxInput(session, "add_legend", value = presets_label[11])
updateTextInput(session, "legend_title", value= presets_label[12])
updateCheckboxInput(session, "show_labels_y", value = presets_label[13])
# updateCheckboxInput(session, "add_description", value = presets_label[9])
}
############ ?stim ################
if (!is.null(query[['stim']])) {
presets_stim <- query[['stim']]
presets_stim <- unlist(strsplit(presets_stim,";"))
observe(print((presets_stim)))
updateCheckboxInput(session, "indicate_stim", value = presets_stim[1])
updateRadioButtons(session, "stim_shape", selected = presets_stim[2])
updateTextInput(session, "stim_range", value= presets_stim[3])
updateTextInput(session, "stim_text", value= presets_stim[4])
updateTextInput(session, "stim_colors", value= presets_stim[5])
}
############ ?url ################
if (!is.null(query[['url']])) {
updateRadioButtons(session, "data_input", selected = 5)
updateTextInput(session, "URL", value= query[['url']])
observe(print((query[['url']])))
updateTabsetPanel(session, "tabs", selected = "Plot")
}
})
########### RENDER URL ##############
output$HTMLpreset <- renderText({
url()
})
######### GENERATE URL with the settings #########
url <- reactive({
base_URL <- paste(sep = "", session$clientData$url_protocol, "//",session$clientData$url_hostname, ":",session$clientData$url_port, session$clientData$url_pathname)
data <- c(input$data_input, input$tidyInput, input$normalization, input$norm_type, input$base_range, "")
vis <- c(input$data_form, input$alphaInput, input$summaryInput, input$add_CI, input$alphaInput_summ, input$multiples, input$thicken)
layout <- c(" ", input$no_grid, input$change_scale, input$range_x, input$range_y, input$color_data, input$color_stats,
input$adjustcolors, "X", input$plot_height, input$plot_width)
#Hide the standard list of colors if it is'nt used
if (input$adjustcolors != "5") {
color <- c(input$colour_list, "none")
} else if (input$adjustcolors == "5") {
color <- c(input$colour_list, input$user_color_list)
}
label <- c(input$add_title, input$title, input$label_axes, input$lab_x, input$lab_y, input$adj_fnt_sz, input$fnt_sz_title, input$fnt_sz_labs, input$fnt_sz_ax, input$fnt_sz_stim, input$add_legend, input$legend_title, input$show_labels_y)
stim <- c(input$indicate_stim, input$stim_shape, input$stim_range, input$stim_text, input$stim_colors)
#replace FALSE by "" and convert to string with ; as seperator
data <- sub("FALSE", "", data)
data <- paste(data, collapse=";")
data <- paste0("data=", data)
vis <- sub("FALSE", "", vis)
vis <- paste(vis, collapse=";")
vis <- paste0("vis=", vis)
layout <- sub("FALSE", "", layout)
layout <- paste(layout, collapse=";")
layout <- paste0("layout=", layout)
color <- sub("FALSE", "", color)
color <- paste(color, collapse=";")
color <- paste0("color=", color)
label <- sub("FALSE", "", label)
label <- paste(label, collapse=";")
label <- paste0("label=", label)
stim <- sub("FALSE", "", stim)
stim <- paste(stim, collapse=";")
stim <- paste0("stim=", stim)
if (input$data_input == "5") {url <- paste("url=",input$URL,sep="")} else {url <- NULL}
parameters <- paste(data, vis,layout,color,label,stim,url, sep="&")
preset_URL <- paste(base_URL, parameters, sep="?")
observe(print(parameters))
observe(print(preset_URL))
return(preset_URL)
})
############# Pop-up that displays the URL to 'clone' the current settings ################