-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.R
2285 lines (1973 loc) · 60.4 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
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
#------------------------------------------------------------------------------#
#--------------------------Start shiny and variables---------------------------#
#------------------------------------------------------------------------------#
# Size limit of the files that can be loaded
options(shiny.maxRequestSize = 50 * 1024^2)
shiny::shinyServer(function(input, output, session) {
aux_soldier <- file.exists("aux_soldier.R")
# Start reactive variables
values <- reactiveValues(
dat = NULL,
only_train_hull = FALSE,
back_colour_scatter_plot = FALSE,
ignore = FALSE,
points_pd2 = 10,
train_data = NULL,
test_data = NULL
)
scat <- reactiveValues(
ini = NULL,
end = NULL
)
influ <- reactiveValues(
inf = NULL,
less = NULL,
mean = NULL
)
model_p <- reactiveValues(pre = NULL)
model_e <- reactiveValues(error = NULL)
results <- reactiveValues(
obb = NULL,
error = NULL,
residual = NULL,
train_residual = NULL,
test_residual = NULL
)
mae_train <- NULL
r2_train <- NULL
mae_test <- NULL
r2_test <- NULL
models <- reactiveValues(num = NULL)
old_names <- NULL
compatible <- "Checking"
#----------------------------------------------------------------------------#
#---------------------TabItem 1: Select files and images---------------------#
#----------------------------------------------------------------------------#
output$i_data_type <- renderUI({
if (aux_soldier) {
radioButtons(
inputId = "data_type",
label = NULL,
choices = list("Time series data" = 1, "Other data" = 2),
selected = character(0)
)
} else {
return(NULL)
}
})
# Menu for selecting plot type
output$iPlot <- renderUI({
# Adapt options to the existence of "aux_soldier.R"
if (aux_soldier && (is.null(input$data_type) || input$data_type != 2)) {
aux_soldier <- "menu_plot_type"
source("aux_soldier.R", local = TRUE)$value
} else {
radioButtons(
inputId = "plot_type",
label = NULL,
choices = list("Show scatterplot" = 2, "Show scatterplot 4D" = 3),
selected = 0
)
}
})
# Load new data file
observeEvent(input$file_new_data, {
# "file_new_data": file with new data
in_file <- input$file_new_data
# Check if there is any data
if (is.null(in_file)) {
return(NULL)
}
# Search for correct extension
csv_ext <- max(0, grep("csv", in_file$datapath))
rds_ext <- max(0, grep("rds", in_file$datapath))
excel <- max(
0,
grep("xlsx", in_file$datapath),
grep("xls", in_file$datapath)
)
print("Loading file")
allowed_date_formats <- c("%d-%m-%Y %H:%M:%OS",
"%d/%m/%Y %H:%M:%OS",
"%m-%d-%Y %H:%M:%OS",
"%m/%d/%Y %H:%M:%OS",
"%Y-%m-%d %H:%M:%OS",
"%Y/%m/%d %H:%M:%OS",
"%d-%m-%Y %H:%M",
"%d/%m/%Y %H:%M",
"%m-%d-%Y %H:%M",
"%m/%d/%Y %H:%M",
"%Y-%m-%d %H:%M",
"%Y/%m/%d %H:%M",
"%d-%m-%Y",
"%d/%m/%Y",
"%m-%d-%Y",
"%m/%d/%Y",
"%Y-%m-%d",
"%Y/%m/%d")
if (csv_ext == 1) { # Check if the file is CSV
values$dat <- read.csv(in_file$datapath)
# Check if the CSV file has the adequate class
if (class(values$dat[]) != "data.frame") {
showModal(
modalDialog(
title = "Warning",
"Loaded file is not a data frame. Close the app and run it again",
size = c("s"),
easyClose = TRUE
)
)
quit(save = "default")
}
values$dat <- as.data.frame(values$dat)
# If time series data
if ((is.null(input$data_type) || input$data_type != 2)) {
# Remove rows with NA or empty values in the first column
values$dat <- values$dat[!(values$dat[, 1] %in% c(NA, "", "N/A")), ]
values$dat[, 1] <- as.POSIXct(
values$dat[, 1],
tz = "UTC",
tryFormats = allowed_date_formats
)
# Warning message
aux_soldier <- "warning_msg"
source("aux_soldier.R", local = TRUE)$value
}
} else if (rds_ext == 1) { # Check if the file is RDS
values$dat <- readRDS(in_file$datapath)
# Check if the RDS file has the adequate class
if (class(values$dat[]) != "data.frame") {
showModal(
modalDialog(
title = "Warning",
"Loaded file is not a data frame. Close the app and run it again",
size = c("s"),
easyClose = TRUE
)
)
quit(save = "default")
}
values$dat <- as.data.frame(values$dat)
# If time series data
if ((is.null(input$data_type) || input$data_type != 2)) {
# Remove rows with NA or empty values in the first column
values$dat <- values$dat[!(values$dat[, 1] %in% c(NA, "", "N/A")), ]
values$dat[, 1] <- as.POSIXct(
values$dat[, 1],
tz = "UTC",
tryFormats = allowed_date_formats
)
# Warning message
aux_soldier <- "warning_msg"
source("aux_soldier.R", local = TRUE)$value
}
} else if (excel == 1) { # Check if the file is EXCEL
values$dat <- as.data.frame(
read_excel(
in_file$datapath,
col_names = TRUE,
guess_max = 20000000
)
)
# If time series data
if ((is.null(input$data_type) || input$data_type != 2)) {
# Remove rows with NA or empty values in the first column
values$dat <- values$dat[!(values$dat[, 1] %in% c(NA, "", "N/A")), ]
values$dat[, 1] <- as.POSIXct(
values$dat[, 1],
tz = "UTC",
tryFormats = allowed_date_formats
)
}
# Identify columns class
classes <- identify_classes(values$dat)
# Search for wrongly classified columns
values$dat <- search_wrong_class_columns(values$dat, classes)
# If time series data
if ((is.null(input$data_type) || input$data_type != 2)) {
# Warning message
aux_soldier <- "warning_msg"
source("aux_soldier.R", local = TRUE)$value
}
} else {
showModal(
modalDialog(
title = "Warning",
"Must be a .CSV, .RDS, .XLSX or .XLS file",
size = c("s"),
easyClose = TRUE
)
)
}
if (input$data_type == 1) {
if ((class(values$dat[, 1])[1] == "Date") || (class(values$dat[, 1])[1] == "POSIXct")) {
# Create "Month" and "Year" columns (if "Month" doesn't exist)
n_initial_columns <- ncol(values$dat)
datum <- values$dat
datum <- generate_month_year(datum, n_initial_columns)
values$dat <- datum
}
}
# Change character variables to factor variables
datum <- change_char_to_factor(values$dat)
# Initialize results$residual
results$residual <- vector("numeric", length = nrow(datum))
values$dat <- datum
values$train_test_data <- datum
})
# Load front image and help image (for both kinds of plots)
output$frontImage <- renderImage(
{
return(list(
src = "www/FrontImage.jpg",
contentType = "image/jpg",
alt = "frontImage",
width = 850,
height = 520
))
},
deleteFile = FALSE
)
output$image1 <- renderImage(
{
in_file <- input$fileImg
if (is.null(in_file)) {
return(list(
src = "www/FrontImage.jpg",
contentType = "image/jpg",
alt = "image",
width = 1,
height = 1
))
}
return(list(
src = in_file$datapath,
contentType = "image/*",
alt = "image",
width = 890
))
},
deleteFile = FALSE
)
output$image2 <- renderImage(
{
in_file <- input$fileImg2
if (is.null(in_file)) {
return(list(
src = "www/FrontImage.jpg",
contentType = "image/jpg",
alt = "image",
width = 1,
height = 1
))
}
return(list(
src = in_file$datapath,
contentType = "image/*",
alt = "image",
width = 890
))
},
deleteFile = FALSE
)
output$image3 <- renderImage(
{
in_file <- input$fileImg3
if (is.null(in_file)) {
return(list(
src = "www/FrontImage.jpg",
contentType = "image/jpg",
alt = "image",
width = 1,
height = 1
))
}
return(list(
src = in_file$datapath,
contentType = "image/*",
alt = "image",
width = 890
))
},
deleteFile = FALSE
)
output$image4 <- renderImage(
{
in_file <- input$fileImg4
if (is.null(in_file)) {
return(list(
src = "www/FrontImage.jpg",
contentType = "image/jpg",
alt = "image",
width = 1,
height = 1
))
}
return(list(
src = in_file$datapath,
contentType = "image/*",
alt = "image",
width = 890
))
},
deleteFile = FALSE
)
#----------------------------------------------------------------------------#
#------------------------TabItem 1: Time series plot-------------------------#
#----------------------------------------------------------------------------#
# Menus for selecting left variables
output$i_variables_left <- renderUI({
datum <- values$dat
# Check if there is any data
if (is.null(datum) || is.null(input$plot_type)) {
return(HTML("Please load some data file and select plot"))
}
if (aux_soldier) {
# Calculate classes for columns
classes <- identify_classes(datum)
num_class <- which(classes %in% c("numeric", "integer"))
items <- sort(names(datum)[num_class])
# Find groups of variables
groups_list <- create_variables_groups(classes, datum)
mat_gr <<- groups_list[[1]]
items <- groups_list[[2]]
if (sum(results$residual[!is.na(results$residual)]) != 0) {
items <- c(items, "Residual")
}
pickerInput(
"vars_left",
"Left Axis",
items,
multiple = TRUE,
options = list(
"actions-box" = TRUE
)
)
}
})
# Menus for selecting right and colour variables
output$i_back_colour_time_plot <- renderUI({
if (is.null(values$dat) || is.null(input$plot_type)) {
return(NULL)
} # Check if there is any data
if (input$plot_type != 1) {
return(NULL)
}
if (aux_soldier) {
checkboxInput("colours2", label = "Alternative color", value = FALSE)
} else {
return(NULL)
}
})
output$i_variables_right <- renderUI({
# "values": dataframe with new data
datum <- values$dat
# Check if there is any data
if (is.null(datum)) {
return(NULL)
}
if (aux_soldier) {
items <- sort(names(datum))
if (sum(results$residual[!is.na(results$residual)]) != 0) {
items <- c(items, "Residual")
}
pickerInput(
"vars_right",
"Right Axis",
items[-1],
multiple = TRUE, options = list(`actions-box` = TRUE))
}
})
# Save columns of variables for plotting time series
var_cols <- eventReactive(input$refresh5, {
showModal(
modalDialog(
title = "Drawing time series plot",
footer = "Click anywhere to close",
size = c("s"),
easyClose = TRUE
)
)
datum <- values$dat
if (is.null(datum)) {
return(NULL)
} # Check if there is any data
if (aux_soldier) {
aux_soldier <- "vars_time_sieries_plot"
source("aux_soldier.R", local = TRUE)$value
var_cols
}
})
# Generate graph output for time series
output$time_graph <- renderPlotly({
if (is.null(values$train_test_data)) {
return(NULL)
} # Check if there is any data
if (aux_soldier) {
input$refresh5
datum <- cbind(values$train_test_data, as.data.frame(results$residual))
names(datum)[ncol(datum)] <- "Residual"
# Sort selected data by first variable
data_sort <- xts::xts(datum[, var_cols()], order.by = datum[, 1])
# Fix names when there is only one variable
if (length(var_cols()) == 1) {
names(data_sort) <- vars_left
}
isolate({
# Search groups of variables
vars_left <- select_variables(values$train_test_data, input$vars_left)
unique_vars <- unique(c(vars_left, input$vars_right))
if (length(vars_left) < 1) {
showModal(
modalDialog(
title = "Warning",
"There are no variables on the left axis.",
size = c("s")
)
)
}
if (length(unique_vars) > 8) {
showModal(
modalDialog(
title = "Warning",
"Too many variables, the number of variables that can be plotted
simultaneously is limited to 8",
size = c("s")
)
)
time_plot <- NULL
} else {
aux_soldier <- "generate_time_series_plot"
source("aux_soldier.R", local = TRUE)$value
}
time_plot
})
}
})
#----------------------------------------------------------------------------#
#---------------------------TabItem 1: Scatterplot---------------------------#
#----------------------------------------------------------------------------#
# Let select horizontal, vertical and color variables for scatterplot
output$x_var_scat <- renderUI({
# "values": dataframe with new data
datum <- values$train_test_data
# Check if there is any data
if (
is.null(datum) || is.null(input$plot_type)
) {
return(HTML("Please load some data file and select plot"))
}
nam <- names(datum)
items <- sort(nam)
if (sum(results$residual[!is.na(results$residual)]) != 0) {
items <- c(items, "Residual")
}
first <- which(items == nam[1])
selectInput("x_scat", "Horizontal Axis", items, items[first])
})
output$y_var_scat <- renderUI({
datum <- values$train_test_data
# Check if there is any data
if (
is.null(datum) || is.null(input$plot_type) || (input$plot_type == 4)
) {
return(NULL)
}
# Calculate classes for columns and only add factor and numeric classes
classes <- identify_classes(datum)
num_class <- which(classes %in% c("numeric", "integer"))
factor_class <- which("factor" == classes)
items <- sort(names(datum)[c(num_class, factor_class)])
if (sum(results$residual[!is.na(results$residual)]) != 0) {
items <- c(items, "Residual")
}
selectInput("y_scat", "Vertical Axis", items)
})
# Variable to colour the scatter plot
output$i_color_scat <- renderUI({
datum <- values$train_test_data
# Check if there is any data
if (is.null(datum) || is.null(input$plot_type)) {
return(NULL)
}
if (input$plot_type == 4) {
return(NULL)
}
# Calculate classes for columns and only add factor and numeric classes
classes <- identify_classes(datum)
num_class <- which(classes %in% c("numeric", "integer"))
items <- sort(names(datum)[num_class])
if (sum(results$residual[!is.na(results$residual)]) != 0) {
items <- c(items, "Residual")
}
selectInput("color_scat", "Colors", items)
})
# Only train option
output$i_only_train_hull <- renderUI({
if (!aux_soldier) {
return(NULL)
}
datum <- values$train_test_data
## Check if there is any data
if (is.null(datum) || is.null(input$plot_type) || input$plot_type != 2) {
return(NULL)
}
# Check if there is any train data
if (is.null(values$train_data)) {
return(NULL)
}
checkboxInput(
inputId = "only_train_hull",
label = "Convex hull only for training data",
value = FALSE
)
})
observeEvent(input$only_train_hull, {
values$only_train_hull <- input$only_train_hull
})
# Background colour of the scatter plot
output$i_back_colour_scatter_plot <- renderUI({
# Check if there is any data
if (is.null(values$train_test_data) || is.null(input$plot_type)) {
return(NULL)
}
if (input$plot_type != 2) {
return(NULL)
}
if (aux_soldier) {
checkboxInput(
inputId = "back_colour_scatter_plot",
label = "Alternative color",
value = FALSE
)
} else {
return(NULL)
}
})
observeEvent(input$back_colour_scatter_plot, {
values$back_colour_scatter_plot <- input$back_colour_scatter_plot
})
# Write message for scatterplot
output$i_scat_message <- renderUI({
# Check if there is any data
if (is.null(values$train_test_data)) {
return(NULL)
}
scat_message <- mess_fun(input$color_scat, values$train_test_data)
scat_message
})
# Drawing buttons
output$i_draw_scat <- renderUI({
# Check if there is any data
if (is.null(values$train_test_data)) {
return(NULL)
}
if (is.null(input$plot_type) || (input$plot_type != 2)) {
return(NULL)
}
actionButton("refresh6", label = "Draw/Refresh", icon = icon("signal"))
})
ref_plot6 <- eventReactive(input$refresh6, {
showModal(modalDialog(title = "Drawing scatterplot",
footer = "Click anywhere to close",
size = c("s"),
easyClose = TRUE))
refresh <- TRUE
})
# Plotting scatterplot
output$scatter_plot <- renderPlotly({
# Check if there is any data
if (is.null(values$train_test_data)) {
return(NULL)
}
back_color <- "white"
if (values$back_colour_scatter_plot) {
back_color <- "darkgrey"
}
datum <- cbind(values$train_test_data, as.data.frame(results$residual))
names(datum)[ncol(datum)] <- "Residual"
refresh <- ref_plot6()
col_nam <- colnames(datum)
point_size <- 8
if (nrow(datum) > 2000) {
point_size <- 6
}
# Check if it is the same dataset than for the last scatterplot
if (!is.null(old_names)) {
if (
old_names[length(old_names)] != names(values$train_test_data[ncol(values$train_test_data)])
) {
old_names <<- names(values$train_test_data)
return(NULL)
}
}
old_names <<- names(values$train_test_data)
x <- datum[, match(input$x_scat, col_nam)]
y <- datum[, match(input$y_scat, col_nam)]
color <- datum[, match(input$color_scat, col_nam)]
# Remove rows with NaN values in x and y
valid_indices <- complete.cases(x, y)
x_valid <- x[valid_indices]
y_valid <- y[valid_indices]
color_valid <- color[valid_indices]
data_df <- data.frame(x = x_valid, y = y_valid, color = color_valid)
# Draw plot
isolate({
plot <- plot_ly()
if (values$only_train_hull) {
datum_train <- cbind(
values$train_data,
as.data.frame(results$residual_train)
)
names(datum_train)[ncol(datum_train)] <- "Residual"
x_hull <- datum_train[, match(input$x_scat, col_nam)]
y_hull <- datum_train[, match(input$y_scat, col_nam)]
# Combine x and y coordinates into a matrix
points <- cbind(x_hull, y_hull)
# Remove rows with NaN values
points <- points[complete.cases(points), ]
if (nrow(points) >= 3) {
# Calculate the convex hull indices
hull <- chull(points)
plot <- add_trace(
x = x_hull[hull],
y = y_hull[hull],
p = plot,
type = "scatter",
mode = "none",
fill = "toself",
fillcolor = "blue, 0.5",
hoverinfo = "none"
)
}
}
# This is needed for printing properly the hover info in case there is a date
if (inherits(x_valid[1], "POSIXct") || inherits(x_valid[1], "POSIXlt")) {
plot <- add_trace(
x = ~x_valid,
y = ~y_valid,
p = plot,
type = "scatter",
mode = "markers",
marker = list(
size = point_size,
color = ~color_valid,
colorbar = list(
title = paste(input$color_scat),
titlefont = list(size = 18),
tickfont = list(size = 18)
),
colorscale = "Rainbow",
showscale = TRUE
),
hovertemplate = paste(
input$x_scat, ": %{x}<br>",
input$y_scat, ": %{y:.2f}<br>",
input$color_scat, ": %{marker.color:.2f}",
"<extra></extra>" # Removes trace0
)
)
} else {
plot <- add_trace(
x = ~x_valid,
y = ~y_valid,
p = plot,
type = "scatter",
mode = "markers",
marker = list(
size = point_size,
color = ~color_valid,
colorbar = list(
title = paste(input$color_scat),
titlefont = list(size = 18),
tickfont = list(size = 18)
),
colorscale = "Rainbow",
showscale = TRUE
),
hovertemplate = paste(
input$x_scat, ": %{x:.2f}<br>",
input$y_scat, ": %{y:.2f}<br>",
input$color_scat, ": %{marker.color:.2f}",
"<extra></extra>" # Removes trace0
)
)
}
if (class(datum[, match(input$i_color_scat, col_nam)]) == "factor") {
showModal(modalDialog(
title = "Colors doesn't work with factor variables",
NULL,
size = c("s")
))
}
plot <- plot %>%
plotly::layout(
xaxis = list(
title = input$x_scat,
zeroline = FALSE,
showgrid = TRUE,
titlefont = list(size = 18),
tickfont = list(size = 18),
mirror = TRUE, # axis lines mirrored to the opposite side of the plotting area
ticks = "outside",
showline = TRUE, # Show the x-axis line
linewidth = 1, # Line width
linecolor = "black" # Line color
),
yaxis = list(
title = input$y_scat,
zeroline = FALSE,
showgrid = TRUE,
titlefont = list(size = 18),
tickfont = list(size = 18),
mirror = TRUE, # axis lines mirrored to the opposite side of the plotting area
ticks = "outside",
showline = TRUE, # Show the x-axis line
linewidth = 1, # Line width
linecolor = "black" # Line color
),
margin = list(l = 50, r = 50, b = 50, t = 50, pad = 2),
plot_bgcolor = back_color,
showlegend = FALSE
)
})
# Display the plot
plot
})
#----------------------------------------------------------------------------#
#-------------------------TabItem 1: 4D scatterplot--------------------------#
#----------------------------------------------------------------------------#
# Let select horizontal, vertical and color variables for scatterplot
output$x_var_scat4d <- renderUI({
datum <- values$train_test_data # "values": dataframe with new data
# Check if there is any data
if (is.null(datum) || is.null(input$plot_type)) {
return(HTML("Please load some data file and select plot"))
}
nam <- names(datum)
items <- sort(nam)
if (sum(results$residual[!is.na(results$residual)]) != 0) {
items <- c(items, "Residual")
}
first <- which(items == nam[1])
selectInput("x_scat4d", "X Axis", items, items[first])
})
x_var <- eventReactive(input$refresh3, {
x_var <- input$x_scat4d
x_var
})
output$y_var_scat4d <- renderUI({
datum <- values$train_test_data
# Check if there is any data
if (is.null(datum) ||
is.null(input$plot_type) ||
(input$plot_type == 4)) {
return(NULL)
}
# Calculate classes for columns
classes <- identify_classes(datum)
num_class <- which(classes %in% c("numeric", "integer"))
items <- sort(names(datum)[num_class])
if (sum(results$residual[!is.na(results$residual)]) != 0) {
items <- c(items, "Residual")
}
selectInput("y_scat4d", "Y Axis", items)
})
y_var <- eventReactive(input$refresh3, {
y_var <- input$y_scat4d
y_var
})
# Let select z axis variables for scatterplot 4D
output$z_var_scat4d <- renderUI({
datum <- values$train_test_data
# Check if there is any data
if (is.null(datum) ||
is.null(input$plot_type) ||
(input$plot_type == 4)) {
return(NULL)
}
# Calculate classes for columns
classes <- identify_classes(datum)
num_class <- which(classes %in% c("numeric", "integer"))
items <- sort(names(datum)[num_class])
if (sum(results$residual[!is.na(results$residual)]) != 0) {
items <- c(items, "Residual")
}
selectInput("z_scat4d", "Z Axis", items)
})
z_var <- eventReactive(input$refresh3, {
z_var <- input$z_scat4d
z_var
})
output$i_color_scat4d <- renderUI({
datum <- values$train_test_data
# Check if there is any data
if (is.null(datum) || is.null(input$plot_type)) {
return(NULL)
}
if (input$plot_type == 4) {
return(NULL)
}
# Calculate classes for columns
classes <- identify_classes(datum)
num_class <- which(classes %in% c("numeric", "integer"))
items <- sort(names(datum)[num_class])
if (sum(results$residual[!is.na(results$residual)]) != 0) {
items <- c(items, "Residual")
}
selectInput("color_scat4d", "Colors", items)
})
col_var <- eventReactive(input$refresh3, {
col_var <- input$color_scat4d
col_var
})
# Write message for scatterplot 4D
output$i_scat_message <- renderUI({
# Check if there is any data
if (is.null(values$train_test_data)) {
return(NULL)
}
scat_message <- mess_fun(input$color_scat4d, values$train_test_data)
scat_message
})
# Drawing buttons
output$i_draw_scat4d <- renderUI({
# Check if there is any data
if (is.null(values$train_test_data)) {
return(NULL)
}
if (is.null(input$plot_type) || (input$plot_type != 3)) {
return(NULL)
}
actionButton("refresh3", label = "Draw/Refresh", icon = icon("signal"))
})
ref_plot3 <- eventReactive(input$refresh3, {
showModal(modalDialog(title = "Drawing scatterplot 4D",
footer = "Click anywhere to close",
size = c("s"),
easyClose = TRUE))
refresh <- TRUE
})
# Plotting scatterplot 4D
output$scatter_plot4d <- renderPlotly({