forked from Tusharjamdade/pr-test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
1262 lines (1110 loc) · 45.5 KB
/
server.R
File metadata and controls
1262 lines (1110 loc) · 45.5 KB
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
# server.R
library(shiny)
library(shinythemes)
library(shinyjs)
library(dplyr)
library(purrr)
library(tibble)
library(sparklyr)
library(DT)
library(ggplot2)
library(survival)
library(lubridate)
library(survminer)
library(readxl)
library(utils)
library(writexl)
if (!exists("ignored_entities")) ignored_entities <- character(0)
if (!exists("ignored_covariates")) ignored_covariates <- character(0)
if (!exists("%||%")) `%||%` <- function(a, b) if (!is.null(a)) a else b
server <- function(input, output, session) {
uploaded_subject_ids_val <- reactiveVal(NULL)
uploaded_subject_file_name_val <- reactiveVal(NULL)
# Initial render of the upload input
output$upload_excel_ui <- renderUI({
fileInput("upload_excel", "Choose Excel / CSV / TXT",
accept = c(".xlsx", ".xls", ".csv", ".txt", ".tsv"))
})
# Holds any uploaded Subject IDs parsed from file (xlsx/csv/tsv/txt)
uploaded_subject_ids_val <- reactiveVal(NULL)
# Holds the basename (sans extension) of the uploaded ID file for labeling
uploaded_subject_file_name_val <- reactiveVal(NULL)
# Core reactive state for the app
rv <- reactiveValues(
sc = NULL,
path = NULL,
dfs = NULL,
entity_types = character(0),
selected_entities = character(0),
flattened = list(),
final_df = NULL,
prep_done = FALSE,
dist_done = FALSE,
os_done = FALSE,
efs_done = FALSE,
dist_data = NULL,
os_fit = NULL,
efs_fit = NULL,
table1_done = FALSE
)
# Session utilities
observeEvent(input$reset_btn, { session$reload() })
output$progress_steps <- renderUI({
tags$div(
class = "muted",
tags$div(
class = paste("step-badge", if (!is.null(rv$dfs)) "done"),
"File Loaded"
),
tags$div(
class = paste("step-badge", if (length(rv$selected_entities) > 0) "done"),
"Entities Chosen"
),
tags$div(
class = paste("step-badge", if (rv$prep_done) "done"),
"Prepare Data"
),
tags$div(
class = paste("step-badge", if (rv$dist_done) "done", if (!rv$prep_done) "locked"),
"Distribution"
),
# CHANGED: OS shows locked only if prep not done (no dependency on Distribution)
tags$div(
class = paste("step-badge", if (rv$os_done) "done", if (!rv$prep_done) "locked"),
"Overall Survival"
),
# CHANGED: EFS shows locked only if prep not done (no dependency on OS)
tags$div(
class = paste("step-badge", if (rv$efs_done) "done", if (!rv$prep_done) "locked"),
"Event-Free Survival"
),
# Keep Summary label; unlocked after prep for consistency
tags$div(
class = paste("step-badge", if (rv$efs_done) "done", if (!rv$prep_done) "locked"),
"Summary Table"
)
)
})
observe({
toggleState("dist_submit", rv$prep_done)
# CHANGED: Enable survival buttons right after prep (no Distribution/OS dependency)
toggleState("overallSurvivalBtn", rv$prep_done)
toggleState("eventFreeSurvivalBtn", rv$prep_done)
toggleState("apply_filters", rv$prep_done)
# CHANGED: Allow navigating to Survival from Distribution area after prep
toggleState("go_surv_from_dist", rv$prep_done)
# Keep existing chaining for these nav buttons as before
toggleState("go_efs", rv$prep_done)
toggleState("go_table1",rv$prep_done)
})
# Upload AVRO
observeEvent(input$avroFile, {
req(input$avroFile)
output$errorMsg <- renderText("")
rv$path <- {
tgt <- file.path("/home/rstudio", input$avroFile$name)
success <- file.copy(input$avroFile$datapath, tgt, overwrite = TRUE)
output$filePath <- renderText({
if (success) paste("File saved to:", tgt) else "Error: Failed to save uploaded file."
})
if (!success) return(NULL)
tgt
}
if (is.null(rv$path)) return()
tryCatch({
rv$sc <- connect_spark_if_needed(rv$sc)
}, error = function(e) {
output$filePath <- renderText(paste("Error connecting to Spark:", conditionMessage(e)))
return()
})
tryCatch({
prep <- prepare_dfs_from_avro(rv$sc, rv$path)
if (is.null(prep$spark_df)) {
output$filePath <- renderText("Error: Invalid file format. Required columns: id, name, object, relations")
return()
}
rv$dfs <- prep$dfs
rv$entity_types <- prep$entity_types
# Reset downstream state on new upload
rv$prep_done <- FALSE
rv$dist_done <- FALSE
rv$os_done <- FALSE
rv$efs_done <- FALSE
rv$final_df <- NULL
rv$flattened <- list()
# Clear any previously uploaded Subject IDs and filename
uploaded_subject_ids_val(NULL)
uploaded_subject_file_name_val(NULL)
}, error = function(e) {
output$filePath <- renderText(paste("Error reading AVRO:", conditionMessage(e)))
return()
})
others <- setdiff(
rv$entity_types,
c("subject", "person", "timing", "survival_characteristic", ignored_entities)
)
output$entity_picker <- renderUI({
if (length(others) == 0) {
div(class = "muted", "No optional entities found.")
} else {
checkboxGroupInput(
"selected_entities",
"Optional entities to include:",
choices = sort(others),
selected = rv$selected_entities,
width = "100%"
)
}
})
})
observe({
rv$selected_entities <- input$selected_entities %||% character(0)
})
observeEvent(input$selected_entities, {
others <- setdiff(
rv$entity_types,
c("subject", "person", "timing", "survival_characteristic", ignored_entities)
)
})
# PREPARE: Flatten required + selected entities; merge
observeEvent(input$prep_btn, {
req(rv$dfs)
output$errorMsg <- renderText("")
withProgress(message = "Preparing data", value = 0, {
needed <- c("subject", "person")
if (!all(needed %in% names(rv$dfs))) {
output$errorMsg <- renderText("Required entities 'subject' and 'person' not found.")
return()
}
incProgress(0.1, detail = "Flattening subject & person")
subject_df <- flatten_entity_df(rv$dfs[["subject"]], output)
person_df <- flatten_entity_df(rv$dfs[["person"]], output)
rv$flattened[["subject"]] <- subject_df
rv$flattened[["person"]] <- person_df
for (sp in c("timing", "survival_characteristic")) if (sp %in% names(rv$dfs)) {
incProgress(0.12, detail = paste("Flattening", sp))
rv$flattened[[sp]] <- flatten_entity_df(rv$dfs[[sp]], output)
}
merged_df <- subject_df %>%
left_join(person_df, by = c("dst_id" = "id"), suffix = c("_subject", "_person"))
sel <- rv$selected_entities
if (length(sel) > 0) {
step <- 0.6 / length(sel)
for (i in seq_along(sel)) {
ent <- sel[i]
incProgress(0.25 + i * step, detail = paste("Flattening", ent))
flat_df <- flatten_entity_df(rv$dfs[[ent]], output)
rv$flattened[[ent]] <- flat_df
merged_df <- merged_df %>%
left_join(flat_df, by = c("id" = "dst_id"), suffix = c("", paste0("_", ent)))
}
}
rv$final_df <- merged_df
try(write_xlsx(merged_df, "final_merged.xlsx"), silent = TRUE)
incProgress(1, detail = "Done")
})
# Columns to ignore by prefix
ignored_prefixes <- c("id", "dst_id", "dst_name", "created_datetime", "updated_datetime")
# Keep only columns that do NOT start with ignored prefixes
dist_columns <- names(rv$final_df)[
!vapply(names(rv$final_df), function(col) any(startsWith(col, ignored_prefixes)), logical(1))
]
# Initialize vectors
cat_cols <- c()
num_cols <- c()
# Loop through columns to classify
for (col in dist_columns) {
vec <- rv$final_df[[col]]
# Flatten list columns if any
if (is.list(vec)) vec <- sapply(vec, function(x) if (length(x) > 1) paste0(x, collapse=";") else as.character(x))
vec_chr <- as.character(vec)
# Determine if numeric-like
numeric_prop <- sum(!is.na(suppressWarnings(as.numeric(vec_chr)))) / max(1, sum(nzchar(vec_chr)))
if (numeric_prop >= 0.9) {
num_cols <- c(num_cols, col)
} else {
cat_cols <- c(cat_cols, col)
}
}
# Update dropdown with categorical columns
updateSelectInput(session, "dist_col", choices = sort(cat_cols))
rv$prep_done <- TRUE
showNotification("Data prepared. You can now run Distribution, Survival, EFS, or Table 1.", type = "message")
updateTabsetPanel(session, "mainTabs", selected = "Distribution")
})
output$status <- renderText({
if (is.null(rv$final_df)) return("Status: waiting for data preparation ...")
paste0("Merged dataset rows: ", nrow(rv$final_df), " | columns: ", ncol(rv$final_df))
})
# Distribution
observeEvent(input$dist_submit, {
req(rv$final_df, input$dist_col)
tryCatch({
column_name <- input$dist_col
df <- rv$final_df
validate(need(column_name %in% names(df), "Selected column not found."))
validate(need(is.character(df[[column_name]]) || is.factor(df[[column_name]]),
"Only categorical columns are allowed for distribution."))
df_unique <- df %>% select(id, !!sym(column_name)) %>% distinct()
value_percentages <- df_unique %>%
count(!!sym(column_name)) %>%
mutate(Percentage = round((n / sum(n)) * 100, 2),
Label = paste0(Percentage, "%"))
rv$dist_data <- value_percentages
output$percentagePlot <- renderPlot({
ggplot(
value_percentages,
aes(x = reorder(!!sym(column_name), -Percentage),
y = Percentage,
fill = !!sym(column_name))
) +
geom_bar(stat = "identity", width = 0.7) +
geom_text(aes(label = Label), vjust = -0.5, size = 4) +
labs(
title = paste("Distribution of", column_name),
x = column_name,
y = "Percentage (%)"
) +
coord_flip() +
theme_minimal(base_size = 14) +
theme(legend.position = "none")
}, res = 120)
output$dist_msg <- renderText("Distribution ready. Download or click 'Next' to continue.")
rv$dist_done <- TRUE
}, error = function(e) {
output$percentagePlot <- renderPlot({ NULL })
output$errorMsg <- renderText(paste("Error:", conditionMessage(e)))
})
})
output$download_dist_xlsx <- downloadHandler(
filename = function() paste0("value_percentages_", Sys.Date(), ".xlsx"),
content = function(file) {
req(rv$dist_data)
export_df <- rv$dist_data %>%
mutate(across(1, ~ ifelse(is.na(.), "N/A", as.character(.)))) %>%
select(1, Percentage)
write_xlsx(export_df, file)
}
)
output$download_dist_png <- downloadHandler(
filename = function() paste0("distribution_", input$dist_col %||% "column", "_", Sys.Date(), ".png"),
content = function(file) {
req(rv$dist_data, input$dist_col)
gp <- ggplot(
rv$dist_data,
aes(x = reorder(!!sym(input$dist_col), -Percentage),
y = Percentage,
fill = !!sym(input$dist_col))
) +
geom_bar(stat = "identity", width = 0.7) +
geom_text(aes(label = paste0(Percentage, "%")), vjust = -0.5, size = 4) +
labs(
title = paste("Distribution of", input$dist_col),
x = input$dist_col,
y = "Percentage (%)"
) +
coord_flip() +
theme_minimal(base_size = 14) +
theme(legend.position = "none")
ggsave(file, gp, width = 10, height = 6, dpi = 150)
}
)
observeEvent(input$go_surv_from_dist, {
req(rv$final_df)
updateTabsetPanel(session, "mainTabs", selected = "Survival")
})
# Utilities for datetime detection/parsing
parse_datetime_vec <- function(x) {
x_char <- as.character(x)
res <- tryCatch(ymd_hms(x_char, tz = "UTC"),
error = function(e) rep(as.POSIXct(NA), length(x_char)))
if (all(is.na(res))) {
orders <- c("Ymd HMS", "YmdHMS", "Y-m-dT%H:%M:%OS", "Y-m-d H:M:S", "Y-m-d")
res2 <- tryCatch(parse_date_time(x_char, orders = orders, tz = "UTC"),
error = function(e) rep(as.POSIXct(NA), length(x_char)))
if (!all(is.na(res2))) res <- res2
}
if (all(is.na(res))) {
res3 <- tryCatch(as_datetime(x_char, tz = "UTC"),
error = function(e) rep(as.POSIXct(NA), length(x_char)))
if (!all(is.na(res3))) res <- res3
}
res
}
find_datetime_col <- function(df) {
nm <- names(df)
if (length(nm) == 0) return(NULL)
prefs <- c(
"create_date", "created_date", "created_datetime", "create_datetime",
"create_date_", "create_dateentity", "create"
)
patt1 <- paste0(prefs, collapse = "|")
m1 <- nm[grepl(patt1, nm, ignore.case = TRUE)]
if (length(m1) > 0) return(m1[1])
m2 <- nm[grepl("date|time|timestamp|datetime", nm, ignore.case = TRUE)]
if (length(m2) > 0) return(m2[1])
for (col in nm) {
vals <- as.character(head(na.omit(df[[col]]), 30))
if (length(vals) < 3) next
iso_hits <- sum(grepl("^\\d{4}-\\d{2}-\\d{2}T", vals) |
grepl("^\\d{4}-\\d{2}-\\d{2} ", vals))
if ((iso_hits / length(vals)) >= 0.6) return(col)
}
NULL
}
ensure_datetime_col <- function(df, col) {
if (is.null(col)) return(list(df = df, col = NULL))
if (inherits(df[[col]], "POSIXt")) return(list(df = df, col = col))
parsed <- parse_datetime_vec(df[[col]])
df[[col]] <- parsed
list(df = df, col = col)
}
# Build survival data (OS)
survival_df <- reactive({
req(rv$flattened[["timing"]], rv$flattened[["survival_characteristic"]])
timing_df <- rv$flattened[["timing"]]
survchar_df <- rv$flattened[["survival_characteristic"]]
dt_timing <- find_datetime_col(timing_df)
dt_surv <- find_datetime_col(survchar_df)
timing_res <- ensure_datetime_col(timing_df, dt_timing)
timing_df <- timing_res$df
dt_timing <- timing_res$col
surv_res <- ensure_datetime_col(survchar_df, dt_surv)
survchar_df <- surv_res$df
dt_surv <- surv_res$col
diagnosis_age_df <- NULL
if (!is.null(dt_timing) && any(!is.na(timing_df[[dt_timing]]))) {
diagnosis_age_df <- timing_df %>%
filter(disease_phase == "Initial Diagnosis") %>%
group_by(dst_id) %>%
slice_max(order_by = .data[[dt_timing]], n = 1, with_ties = FALSE) %>%
ungroup() %>%
select(dst_id, age_at_disease_phase)
} else {
if ("age_at_disease_phase" %in% names(timing_df)) {
timing_df <- timing_df %>%
mutate(age_at_disease_phase = suppressWarnings(as.numeric(age_at_disease_phase)))
diagnosis_age_df <- timing_df %>%
filter(disease_phase == "Initial Diagnosis") %>%
group_by(dst_id) %>%
slice_max(order_by = age_at_disease_phase, n = 1, with_ties = FALSE) %>%
ungroup() %>%
select(dst_id, age_at_disease_phase)
output$errorMsg <- renderText(
"Warning: no create-date found in timing; using age_at_disease_phase to pick record."
)
} else {
diagnosis_age_df <- timing_df %>%
filter(disease_phase == "Initial Diagnosis") %>%
distinct(dst_id, .keep_all = TRUE) %>%
select(dst_id, age_at_disease_phase)
output$errorMsg <- renderText(
"Warning: no create-date or age_at_disease_phase available; using first diagnosis record per subject."
)
}
}
if (!is.null(dt_surv) && any(!is.na(survchar_df[[dt_surv]]))) {
surv_rows <- survchar_df %>%
group_by(dst_id) %>%
slice_max(order_by = .data[[dt_surv]], n = 1, with_ties = FALSE) %>%
ungroup()
} else if ("age_at_lkss" %in% names(survchar_df) &&
any(!is.na(suppressWarnings(as.numeric(survchar_df$age_at_lkss))))) {
survchar_df <- survchar_df %>%
mutate(age_at_lkss = suppressWarnings(as.numeric(age_at_lkss)))
surv_rows <- survchar_df %>%
group_by(dst_id) %>%
slice_max(order_by = age_at_lkss, n = 1, with_ties = FALSE) %>%
ungroup()
output$errorMsg <- renderText(
"Warning: no create-date found in survival_characteristic; using max age_at_lkss per subject."
)
} else {
surv_rows <- survchar_df %>% group_by(dst_id) %>% slice(1) %>% ungroup()
output$errorMsg <- renderText(
"Warning: no create-date or age_at_lkss found; using first survival_characteristic row per subject."
)
}
survival_data_local <- surv_rows %>%
inner_join(diagnosis_age_df, by = "dst_id") %>%
mutate(
age_at_lkss = suppressWarnings(as.numeric(age_at_lkss)),
age_at_disease_phase = suppressWarnings(as.numeric(age_at_disease_phase)),
time_years = (age_at_lkss - age_at_disease_phase) / 365.25,
event = ifelse(tolower(as.character(lkss)) == "dead", 1L, 0L)
) %>%
filter(!is.na(time_years), !is.na(event), time_years >= 0)
survival_data_local <- survival_data_local %>%
group_by(dst_id) %>%
slice_max(order_by = time_years, n = 1, with_ties = FALSE) %>%
ungroup()
validate(need(nrow(survival_data_local) > 0, "No valid rows for Overall Survival."))
survival_data_local
})
# Build EFS data
efs_df <- reactive({
req(rv$flattened[["subject"]], rv$flattened[["timing"]])
subject_df <- rv$flattened[["subject"]]
timing_df <- rv$flattened[["timing"]]
dt_timing <- find_datetime_col(timing_df)
dt_subject <- find_datetime_col(subject_df)
timing_res <- ensure_datetime_col(timing_df, dt_timing)
timing_df <- timing_res$df
dt_timing <- timing_res$col
subject_res <- ensure_datetime_col(subject_df, dt_subject)
subject_df <- subject_res$df
dt_subject <- subject_res$col
if (!is.null(dt_timing) && any(!is.na(timing_df[[dt_timing]]))) {
diagnosis_age_df <- timing_df %>%
filter(disease_phase == "Initial Diagnosis") %>%
group_by(dst_id) %>%
slice_max(order_by = .data[[dt_timing]], n = 1, with_ties = FALSE) %>%
ungroup() %>%
select(dst_id, age_at_disease_phase)
} else if ("age_at_disease_phase" %in% names(timing_df)) {
timing_df <- timing_df %>%
mutate(age_at_disease_phase = suppressWarnings(as.numeric(age_at_disease_phase)))
diagnosis_age_df <- timing_df %>%
filter(disease_phase == "Initial Diagnosis") %>%
group_by(dst_id) %>%
slice_max(order_by = age_at_disease_phase, n = 1, with_ties = FALSE) %>%
ungroup() %>%
select(dst_id, age_at_disease_phase)
output$errorMsg <- renderText(
"Warning: no create-date in timing; using age_at_disease_phase for diagnosis."
)
} else {
diagnosis_age_df <- timing_df %>%
filter(disease_phase == "Initial Diagnosis") %>%
distinct(dst_id, .keep_all = TRUE) %>%
select(dst_id, age_at_disease_phase)
output$errorMsg <- renderText(
"Warning: no create-date or age_at_disease_phase available; using first diagnosis record."
)
}
if (!is.null(dt_subject) && any(!is.na(subject_df[[dt_subject]]))) {
subject_rows <- subject_df %>%
group_by(id) %>%
slice_max(order_by = .data[[dt_subject]], n = 1, with_ties = FALSE) %>%
ungroup()
} else {
subject_rows <- subject_df %>% group_by(id) %>% slice(1) %>% ungroup()
output$errorMsg <- renderText("Warning: no create-date in subject; using first subject row for each id.")
}
efs_data_local <- subject_rows %>%
inner_join(diagnosis_age_df, by = c("id" = "dst_id")) %>%
mutate(
age_at_censor_status = suppressWarnings(as.numeric(age_at_censor_status)),
age_at_disease_phase = suppressWarnings(as.numeric(age_at_disease_phase)),
time_years = (age_at_censor_status - age_at_disease_phase) / 365.25,
event = ifelse(as.character(censor_status) == "Subject has had one or more events", 1L, 0L)
) %>%
filter(!is.na(time_years), !is.na(event), time_years >= 0) %>%
group_by(id) %>%
slice_max(order_by = time_years, n = 1, with_ties = FALSE) %>%
ungroup()
validate(need(nrow(efs_data_local) > 0, "No valid rows for Event-Free Survival."))
efs_data_local
})
survival_df_clean <- reactive({
df <- survival_df()
df <- df %>% group_by(dst_id) %>% slice_max(order_by = time_years, n = 1, with_ties = FALSE) %>% ungroup()
validate(need(nrow(df) > 0, "No valid rows after deduplication."))
df
})
efs_df_clean <- reactive({
df <- efs_df()
df <- df %>% group_by(id) %>% slice_max(order_by = time_years, n = 1, with_ties = FALSE) %>% ungroup()
validate(need(nrow(df) > 0, "No valid rows after deduplication."))
df
})
observeEvent(input$overallSurvivalBtn, {
tryCatch({
req(survival_df_clean())
rv$os_fit <- survfit(Surv(time_years, event) ~ 1, data = survival_df_clean())
rv$os_done <- TRUE
showNotification("Overall Survival fitted — plot will render below.", type = "message")
}, error = function(e) {
output$errorMsg <- renderText(paste("ERROR preparing Overall Survival:", conditionMessage(e)))
})
})
observeEvent(input$eventFreeSurvivalBtn, {
tryCatch({
req(efs_df_clean())
rv$efs_fit <- survfit(Surv(time_years, event) ~ 1, data = efs_df_clean())
rv$efs_done <- TRUE
showNotification("Event-Free Survival fitted — plot will render below.", type = "message")
}, error = function(e) {
output$errorMsg <- renderText(paste("ERROR preparing Event-Free Survival:", conditionMessage(e)))
})
})
output$overallSurvivalPlot <- renderPlot({
req(rv$os_fit)
# check median survival
med <- suppressWarnings(summary(rv$os_fit)$table["median"])
surv.median.opt <- if (!is.na(med) && med > 0) "hv" else NULL
gp <- ggsurvplot(
rv$os_fit,
data = survival_df_clean(),
conf.int = TRUE,
risk.table = TRUE,
risk.table.col = "black",
risk.table.height = 0.25,
surv.median.line = surv.median.opt,
xlab = "Time Since Diagnosis (Years)",
ylab = "Overall Survival Probability",
title = "Kaplan-Meier Overall Survival Curve",
palette = "Dark2",
ggtheme = theme_minimal(base_size = 14)
)
print(gp)
}, res = 120)
output$eventFreeSurvivalPlot <- renderPlot({
req(rv$efs_fit)
# check median survival
med <- suppressWarnings(summary(rv$efs_fit)$table["median"])
surv.median.opt <- if (!is.na(med) && med > 0) "hv" else NULL
gp <- ggsurvplot(
rv$efs_fit,
data = efs_df_clean(),
conf.int = TRUE,
risk.table = TRUE,
risk.table.col = "black",
risk.table.height = 0.25,
surv.median.line = surv.median.opt,
xlab = "Time Since Diagnosis (Years)",
ylab = "Event-Free Survival Probability",
title = "Kaplan-Meier Event-Free Survival Curve",
palette = "Dark2",
ggtheme = theme_minimal(base_size = 14)
)
print(gp)
}, res = 120)
observeEvent(input$go_table1, {
updateTabsetPanel(session, "mainTabs", selected = "Table1")
})
# Overall Survival (OS) with Uploaded IDs
observeEvent(input$overallSurvivalSubjectBtn, {
ids <- uploaded_subject_ids_val()
if (is.null(ids) || length(ids) == 0) {
showNotification("⚠️ Please upload Subject IDs before running Overall Survival.", type = "warning")
return()
}
tryCatch({
df <- survival_df_clean()
df_sub <- df %>% filter(dst_id %in% ids)
validate(need(nrow(df_sub) > 0, "No matching Subject IDs found in OS data."))
surv_obj <- survfit(Surv(time_years, event) ~ 1, data = df_sub)
rv$os_fit <- surv_obj
rv$os_done <- TRUE
}, error = function(e) {
output$errorMsg <- renderText(paste("ERROR preparing Overall Survival (IDs):", conditionMessage(e)))
})
})
# Event-Free Survival (EFS) with Uploaded IDs
observeEvent(input$eventFreeSurvivalSubjectBtn, {
ids <- uploaded_subject_ids_val()
if (is.null(ids) || length(ids) == 0) {
showNotification("⚠️ Please upload Subject IDs before running Event-Free Survival.", type = "warning")
return()
}
tryCatch({
df <- efs_df_clean()
df_sub <- df %>% filter(id %in% ids)
validate(need(nrow(df_sub) > 0, "No matching Subject IDs found in EFS data."))
surv_obj <- survfit(Surv(time_years, event) ~ 1, data = df_sub)
rv$efs_fit <- surv_obj
rv$efs_done <- TRUE
}, error = function(e) {
output$errorMsg <- renderText(paste("ERROR preparing EFS (IDs):", conditionMessage(e)))
})
})
# Clear Uploaded IDs (reset both uploaders)
observeEvent(input$clear_ids_btn, {
# Reset stored IDs + filename
uploaded_subject_ids_val(NULL)
uploaded_subject_file_name_val(NULL)
rv$uploaded_subject_ids_val <- list()
# Reset the Excel/CSV/TXT uploader
output$upload_excel_ui <- renderUI({
fileInput("upload_excel", "Choose Excel / CSV / TXT",
accept = c(".xlsx", ".xls", ".csv", ".txt", ".tsv"))
})
# Reset the Subject IDs uploader
output$upload_ids_ui <- renderUI({
fileInput(
"upload_subject_ids",
"Upload Subject IDs (CSV/TXT, one ID per line):",
accept = c(".csv", ".txt")
)
})
showNotification("Uploaded Subject IDs have been cleared.", type = "message")
})
# Download OS Plot as PNG
output$download_os_png <- downloadHandler(
filename = function() {
paste0("overall_survival_", Sys.Date(), ".png")
},
content = function(file) {
req(rv$os_fit)
gp <- ggsurvplot(
rv$os_fit,
data = survival_df_clean(),
conf.int = TRUE,
risk.table = TRUE,
risk.table.col = "black",
risk.table.height = 0.25,
surv.median.line = "hv",
xlab = "Time Since Diagnosis (Years)",
ylab = "Overall Survival Probability",
title = "Kaplan-Meier Overall Survival Curve",
palette = "Dark2",
ggtheme = theme_minimal(base_size = 14)
)
ggsave(file, plot = gp$plot, device = "png", width = 8, height = 6, dpi = 300)
}
)
# Download EFS Plot as PNG
output$download_efs_png <- downloadHandler(
filename = function() {
paste0("event_free_survival_", Sys.Date(), ".png")
},
content = function(file) {
req(rv$efs_fit)
gp <- ggsurvplot(
rv$efs_fit,
data = efs_df_clean(),
conf.int = TRUE,
risk.table = TRUE,
risk.table.col = "black",
risk.table.height = 0.25,
surv.median.line = "hv",
xlab = "Time Since Diagnosis (Years)",
ylab = "Event-Free Survival Probability",
title = "Kaplan-Meier Event-Free Survival Curve",
palette = "Dark2",
ggtheme = theme_minimal(base_size = 14)
)
ggsave(file, plot = gp$plot, device = "png", width = 8, height = 6, dpi = 300)
}
)
# TABLE 1 — UI cards & grouping handling
card_ids <- reactiveVal(character(0))
counter <- reactiveVal(0)
observeEvent(input$add_covariate, {
req(rv$final_df)
n <- counter() + 1
counter(n)
new_id <- paste0("filter", n)
try(suppressWarnings(removeUI(selector = "#no_cards_msg", immediate = TRUE)), silent = TRUE)
ignored_prefixes <- c("id", "dst_id", "dst_name")
init_choices <- names(rv$final_df)[!vapply(
names(rv$final_df),
function(col) any(startsWith(as.character(col), ignored_prefixes)),
logical(1)
)]
insertUI(
selector = "#filter_container",
where = "beforeEnd",
ui = covariateCardUI(new_id, choices = init_choices)
)
covariateCardServer(
new_id,
reactive({ rv$final_df }),
function(id) {
try(suppressWarnings(removeUI(selector = paste0("#", id), immediate = TRUE)), silent = TRUE)
card_ids(setdiff(card_ids(), id))
}
)
card_ids(c(card_ids(), new_id))
})
# Robust uploaded ID parsing: watch the upload input and populate reactiveVal
observeEvent(input$upload_excel, {
f <- input$upload_excel
uploaded_subject_ids_val(NULL)
uploaded_subject_file_name_val(NULL)
if (is.null(f)) return()
path <- f$datapath
ext <- tolower(tools::file_ext(f$name %||% path))
ids <- NULL
try({
if (ext %in% c("xlsx", "xls")) {
df <- suppressWarnings(readxl::read_excel(path, col_names = TRUE))
if (ncol(df) > 0) {
cn <- names(df)
pick <- which(tolower(cn) %in% c("id", "subject_id", "subjectid"))
col <- if (length(pick) >= 1) cn[pick[1]] else cn[1]
ids <- as.character(df[[col]])
}
} else if (ext %in% c("txt")) {
lines <- readLines(path, warn = FALSE)
ids <- trimws(lines)
ids <- ids[nzchar(ids)]
} else if (ext %in% c("csv")) {
tmp <- read.csv(path, stringsAsFactors = FALSE)
if (ncol(tmp) >= 1) ids <- as.character(tmp[[1]])
} else if (ext %in% c("tsv")) {
tmp <- read.delim(path, stringsAsFactors = FALSE)
if (ncol(tmp) >= 1) ids <- as.character(tmp[[1]])
}
}, silent = TRUE)
ids <- ids %||% character(0)
ids <- unique(trimws(as.character(ids)))
ids <- ids[nzchar(ids)]
if (length(ids) == 0) {
uploaded_subject_ids_val(NULL)
uploaded_subject_file_name_val(NULL)
showNotification("Uploaded ID file parsed but no IDs found.", type = "warning")
} else {
uploaded_subject_ids_val(ids)
# Store basename (sans extension) for column labeling
bn <- tools::file_path_sans_ext(basename(f$name))
uploaded_subject_file_name_val(bn)
showNotification(paste("Parsed", length(ids), "unique IDs from upload."), type = "message")
}
}, ignoreInit = TRUE)
uploaded_subject_ids <- reactive({ uploaded_subject_ids_val() })
observeEvent(uploaded_subject_ids(), {
if (!is.null(uploaded_subject_ids())) {
showNotification(
"Subject ID file detected — Table 1 will use two groups (Uploaded IDs vs Others).",
type = "message"
)
}
}, ignoreInit = TRUE)
# Build grouping info based on uploaded IDs; fallback to single-group
group_info <- reactive({
req(rv$final_df)
unique_ids <- rv$final_df %>% pull(id) %>% as.character() %>% unique()
up_ids <- uploaded_subject_ids()
fname <- uploaded_subject_file_name_val()
if (!is.null(up_ids) && length(up_ids) > 0) {
g1 <- intersect(unique_ids, up_ids)
g2 <- setdiff(unique_ids, g1)
if (length(g1) == 0 || length(g2) == 0) {
list(mode = "single", all = unique_ids, g1 = character(0), g2 = character(0),
g1_label = NULL, g2_label = "Everything else")
} else {
g1_label <- if (!is.null(fname) && nzchar(fname)) fname else "Group 1"
g2_label <- "Everything else"
list(mode = "two", all = unique_ids, g1 = g1, g2 = g2,
g1_label = g1_label, g2_label = g2_label)
}
} else {
list(mode = "single", all = unique_ids, g1 = character(0), g2 = character(0),
g1_label = NULL, g2_label = "Everything else")
}
})
# Helper formatters
fmt_p <- function(p) {
if (is.null(p) || is.na(p)) return("—")
if (p < 0.001) "<0.001" else sprintf("%.3f", p)
}
fmt_mean_sd <- function(x) {
x <- x[!is.na(x)]
if (!length(x)) return("—")
paste0(round(mean(x), 2), " (", round(stats::sd(x), 2), ")")
}
n_pct <- function(n, d) {
if (is.na(d) || d == 0) return("0 (0%)")
paste0(n, " (", round(100 * n / d, 2), "%)")
}
# Clear all cards + reset grouping only (do NOT delete flattened data)
observeEvent(input$clear_cards, {
try(suppressWarnings(removeUI(selector = "#filter_container .card", multiple = TRUE, immediate = TRUE)), silent = TRUE)
card_ids(character(0))
counter(0)
session$userData$lastChoices <- NULL
try(suppressWarnings(removeUI(selector = "#no_cards_msg", immediate = TRUE)), silent = TRUE)
insertUI(
selector = "#filter_container",
where = "afterBegin",
ui = tags$div(id = "no_cards_msg", class = "muted", "No covariates added. Click 'Add Covariate' to begin.")
)
# Clear stored uploaded subject IDs and filename
uploaded_subject_ids_val(NULL)
uploaded_subject_file_name_val(NULL)
rv$table1_done <- FALSE
output$status <- renderText({
paste0(
"Status: data loaded, but grouping cleared. Table 1 will now use single-group mode (",
nrow(rv$final_df %||% tibble())[1],
" rows)."
)
})
output$errorMsg <- renderText("")
showNotification(
"Grouping cleared. Operations will now default to single-group until a new Subject ID file is uploaded.",
type = "message"
)
})
# Core: build Table 1 according to grouping mode
table1_summary <- eventReactive(input$apply_filters, {
req(rv$final_df)
gi <- group_info()
df <- rv$final_df
active_cards <- card_ids()
active_cards <- purrr::keep(active_cards, function(idc) {
sel_col <- isolate(input[[paste0(idc, "-col")]])
!is.null(sel_col) && nzchar(sel_col) && (sel_col %in% names(df))
})
results <- list()
if (identical(gi$mode, "two")) {
col1_name <- gi$g1_label %||% "Group 1"
col2_name <- gi$g2_label %||% "Everything else"
# Header counts for two groups (dynamically named)
hdr <- tibble(Covariate = "Count")
hdr[[col1_name]] <- paste0(length(gi$g1), " (Count)")
hdr[[col2_name]] <- paste0(length(gi$g2), " (Count)")
hdr$P_value <- ""
results[[length(results) + 1]] <- hdr
for (idc in active_cards) {
col <- isolate(input[[paste0(idc, "-col")]])
vals <- isolate(input[[paste0(idc, "-vals")]])
if (is.null(col) || !nzchar(col) || !(col %in% names(df))) next
df_unique <- df %>%
select(id, all_of(col)) %>%
distinct() %>%
mutate(.grp = case_when(
id %in% gi$g1 ~ "G1",
id %in% gi$g2 ~ "G2",
TRUE ~ NA_character_
))
col_raw <- df_unique[[col]]
if (is.list(col_raw)) {
col_chr <- sapply(
col_raw,
function(x) {
if (is.null(x)) return(NA_character_)
if (length(x) > 1) paste0(x, collapse = ";")