-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.R
More file actions
1452 lines (1355 loc) · 63.2 KB
/
server.R
File metadata and controls
1452 lines (1355 loc) · 63.2 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
source("plot.floater.R")
source("plotlyCRSFunc.R")
shinyServer(function(session, input, output) {
# ── Reactive values ────────────────────────────────────────────────────────
# Central store for mutable app state. Using reactiveValues rather than
# reactive() for data that gets modified in place (e.g. edits to rwlRV$dated).
# datedVault holds the pre-edit snapshot so edits can be reverted.
rwlRV <- reactiveValues(
dated = NULL,
undated = NULL,
editLog = NULL,
editDF = data.frame(series = NULL, year = NULL,
value = NULL, action = NULL,
fixLast = NULL),
seriesDF = NULL,
datedNoSeries = NULL,
datedVault = NULL,
undatedVault = NULL,
undated2dated = NULL,
dateLog = NULL,
readError = NULL # set by tryCatch in getRWL(); displayed on Overview
)
# ── Helper: resolve n input ────────────────────────────────────────────────
# The Hanning filter length n can be NULL (no filter) or a numeric.
# Centralised here so all reactives use identical logic.
resolveN <- function(val) {
if (is.null(val) || val == "NULL") NULL else as.numeric(val)
}
# ── Helper: build summary card from rwl.report() return value ─────────────
# rwl.report() returns a named list (since dplR 1.7.7 it is no longer
# invisible). We read fields directly rather than parsing printed output.
# Returns a div of label/value rows suitable for rendering inside a card.
rwlSummaryCard <- function(dat) {
rpt <- rwl.report(dat)
nZerosPct <- if (rpt$n > 0) round(rpt$nZeros / rpt$n * 100, 2) else 0
missingStr <- paste0(rpt$nZeros, " (", nZerosPct, "%)")
unconnectedStr <- if (rpt$unconnected) {
paste0("YES \u2014 years: ", paste(rpt$unconnectedYrs, collapse = ", "))
} else {
"None"
}
items <- list(
list(label = "Dated series", value = rpt$nSeries),
list(label = "Total measurements", value = rpt$n),
list(label = "Missing rings (zeros)", value = missingStr),
list(label = "Mean series length", value = round(rpt$meanSegLength, 0)),
list(label = "Span", value = paste(rpt$firstYear,
"\u2013",
rpt$lastYear)),
list(label = "Mean interseries cor (SD)",
value = paste0(round(rpt$meanInterSeriesCor, 3),
" (", round(rpt$sdInterSeriesCor, 3), ")")),
list(label = "Mean AR1 (SD)",
value = paste0(round(rpt$meanAR1, 3),
" (", round(rpt$sdAR1, 3), ")"))
#list(label = "Unconnected floaters", value = unconnectedStr)
)
rows <- lapply(items, function(item) {
div(class = "row mb-1",
div(class = "col-7 text-muted small", item$label),
div(class = "col-5 fw-bold small text-end", as.character(item$value))
)
})
div(rows)
}
# ── Progressive sidebar disclosure ────────────────────────────────────────
# Sidebar controls reveal as the user advances through the workflow:
# Overview: Dated Series + About only
# Correlations: + Analysis Parameters
# Series/Edit: + Series selector
# Floater: + Undated Series (only if dated file is loaded)
observe({
shinyjs::hide("divSharedParams")
shinyjs::hide("divSeriesSelector")
shinyjs::hide("divUndated")
})
observeEvent(input$navbar, {
tab <- input$navbar
# Analysis Parameters — Correlations and beyond
if (tab %in% c("AllSeriesTab", "IndividualSeriesTab",
"EditSeriesTab", "UndatedSeriesTab")) {
shinyjs::show("divSharedParams")
} else {
shinyjs::hide("divSharedParams")
}
# Series selector — Series and Edit panels only
if (tab %in% c("IndividualSeriesTab", "EditSeriesTab")) {
shinyjs::show("divSeriesSelector")
} else {
shinyjs::hide("divSeriesSelector")
}
# Undated series div is handled by the observe() below so that it
# reacts to both tab changes AND dated file state changes (e.g.
# unchecking useDemoDated). Nothing to do here.
})
# Undated sidebar section — separate observer so it reacts to BOTH
# tab changes and dated file state (getRWL() going NULL/non-NULL).
# If this lived only inside observeEvent(input$navbar), unchecking
# useDemoDated would leave divUndated visible because the navbar
# tab didn't change.
observe({
if (!is.null(input$navbar) &&
input$navbar == "UndatedSeriesTab" &&
!is.null(getRWL())) {
shinyjs::show("divUndated")
} else {
shinyjs::hide("divUndated")
}
})
# ── File readers ───────────────────────────────────────────────────────────
getRWL <- reactive({
rwlRV$readError <- NULL # clear any previous error on each attempt
if (input$useDemoDated) {
return(read.rwl("data/xDateRtest.rwl"))
}
inFile <- input$file1
if (is.null(inFile)) return(NULL)
# Use try() rather than tryCatch() — more reliable for catching base R
# errors (e.g. from scan()) that fire deep inside dplR's read.rwl().
result <- try(read.rwl(inFile$datapath), silent = TRUE)
if (inherits(result, "try-error")) {
# Strip the "Error in ... :" prefix that try() prepends
rwlRV$readError <- trimws(gsub("^Error.*?:\n", "", as.character(result)))
return(NULL)
}
result
})
getRWLUndated <- reactive({
if (input$useDemoUndated) {
dat <- read.rwl("data/xDateRtestUndated.rwl")
rwlRV$undated <- dat
return(dat)
}
inFile <- input$file2
if (is.null(inFile)) return(NULL)
dat <- read.rwl(inFile$datapath)
rwlRV$undated <- dat
dat
})
# ── Populate sidebar controls once data loads ──────────────────────────────
observeEvent(getRWL(), {
updateAwesomeCheckboxGroup(
session = session,
inputId = "master",
inline = TRUE,
choices = colnames(getRWL()),
selected = colnames(getRWL()),
status = "primary"
)
})
observeEvent(filteredRWL(), {
updateSelectInput(
session = session,
inputId = "series",
choices = colnames(rwlRV$dated),
selected = colnames(rwlRV$dated)[1]
)
})
observeEvent(getRWLUndated(), {
updateSelectInput(
session = session,
inputId = "series2",
choices = colnames(rwlRV$undated),
selected = colnames(rwlRV$undated)[1]
)
})
# ── Update window sliders when series, data, or window controls change ───────
# winCenter and winWidth are mutually constrained:
#
# winWidth max = min(100, 2 * min(center - seriesStart, seriesEnd - center))
# capped at 100 (skeleton plot is unreadable beyond that) and can never
# push the window off either end of the series.
#
# winCenter min/max = seriesStart + winWidth/2 .. seriesEnd - winWidth/2
# the centre must be far enough from both ends to fit a half-width.
#
# Both sliders share one observer so they stay consistent. Current values are
# preserved where still within the new bounds, and clamped otherwise.
# lagCCF fallback guards against NULL on first fire.
observeEvent({
input$series
filteredRWL()
input$winCenter
input$winWidth
}, {
req(input$series, rwlRV$dated)
dat <- rwlRV$dated
tmp <- summary(dat)
lag <- if (!is.null(input$lagCCF) && !is.na(input$lagCCF)) input$lagCCF else 5
sBnds <- as.numeric(tmp[tmp$series == input$series, 2:3])
sStart <- sBnds[1]
sEnd <- sBnds[2]
# rangeCCF (Series panel) -- unaffected by winWidth
minWin <- round(sStart + lag, -1)
maxWin <- round(sEnd - lag, -1)
output$rangeCCF <- renderUI({
sliderInput("rangeCCF", "Adjust plotted years",
min = minWin, max = maxWin,
value = c(minWin, maxWin),
step = 5, sep = "", dragRange = TRUE,
ticks = FALSE)
})
# Current slider values with sensible defaults on first load
curCenter <- if (!is.null(input$winCenter) && !is.na(input$winCenter)) {
input$winCenter
} else {
round(mean(sBnds), -1)
}
curWidth <- if (!is.null(input$winWidth) && !is.na(input$winWidth)) {
input$winWidth
} else {
40
}
# winWidth bounds: largest window that fits around curCenter, capped at 100
halfRoom <- min(curCenter - sStart, sEnd - curCenter)
maxWidth <- max(10, min(100, floor(halfRoom / 10) * 10 * 2))
curWidth <- min(curWidth, maxWidth)
# winCenter bounds: centre must fit half-width from each end
halfWidth <- curWidth / 2
minCenter <- round(sStart + halfWidth, -1)
maxCenter <- round(sEnd - halfWidth, -1)
if (minCenter >= maxCenter) {
minCenter <- round(sStart, -1)
maxCenter <- round(sEnd, -1)
}
curCenter <- max(minCenter, min(maxCenter, curCenter))
output$winCenter <- renderUI({
sliderInput("winCenter", "Window Center",
min = minCenter, max = maxCenter,
value = curCenter,
step = 5, sep = "",
ticks = FALSE)
})
}, ignoreNULL = TRUE)
# ── winWidth.ui: dynamic window width slider ───────────────────────────────
# Registered as a top-level renderUI (not inside the observer) so that
# outputOptions(suspendWhenHidden = FALSE) can reference it at startup.
# Reads input$series, input$winCenter, and rwlRV$dated as reactive deps
# so it still updates whenever any of those change.
# max = min(100, largest even window that fits around curCenter)
# default = min(40, max)
output$winWidth.ui <- renderUI({
if (is.null(rwlRV$dated) || is.null(input$series) || input$series == "") {
sliderInput("winWidth", "Window width (years)",
min = 10, max = 100, value = 40, step = 10, ticks = FALSE)
} else {
dat <- rwlRV$dated
tmp <- summary(dat)
sBnds <- as.numeric(tmp[tmp$series == input$series, 2:3])
sStart <- sBnds[1]
sEnd <- sBnds[2]
curCenter <- if (!is.null(input$winCenter) && !is.na(input$winCenter)) {
input$winCenter
} else {
round(mean(sBnds), -1)
}
halfRoom <- min(curCenter - sStart, sEnd - curCenter)
maxWidth <- max(10, min(100, floor(halfRoom / 10) * 10 * 2))
curWidth <- if (!is.null(input$winWidth) && !is.na(input$winWidth)) {
min(input$winWidth, maxWidth)
} else {
min(40, maxWidth)
}
sliderInput("winWidth", "Window width (years)",
min = 10, max = maxWidth, value = curWidth,
step = 10, ticks = FALSE)
}
})
outputOptions(output, "winWidth.ui", suspendWhenHidden = FALSE)
# ── filteredRWL ────────────────────────────────────────────────────────────
# The master chronology used by all correlation functions. Recomputes when
# the user clicks "Update Master" or when new data is loaded.
# Series can be temporarily excluded via the checkbox group on the
# Correlations panel — useful when a bad series would corrupt the master.
filteredRWL <- eventReactive({
input$updateMasterButton
getRWL()
}, {
req(getRWL())
res <- if (is.null(input$master) || length(input$master) == 0 || identical(input$master, "")) {
getRWL()
} else {
getRWL()[, colnames(getRWL()) %in% input$master, drop = FALSE]
}
rwlRV$dated <- res
rwlRV$datedVault <- res # snapshot for revert
res
})
# ── rwlStats: derived series-length stats used by dynamic widgets ─────────
# Computed once from filteredRWL() and shared by seg.length.ui, winWidth.ui,
# and any future widgets that need to be bounded by the data dimensions.
# minLen: min series length rounded down to nearest 10 (minimum 10).
# Used as the seg.length max — any series shorter than the segment length
# will produce zero-length bins and crash corr.rwl.seg().
# seriesLengths: named integer vector of non-NA measurements per series.
rwlStats <- reactive({
req(filteredRWL())
dat <- rwlRV$dated
lengths <- colSums(!is.na(dat))
minLen <- max(10, floor(min(lengths) / 10) * 10)
list(minLen = minLen, seriesLengths = lengths)
})
# ── seg.length.ui: dynamic segment length slider ───────────────────────────
# max = min series length rounded down to nearest 10 — prevents any series
# being shorter than the segment, which crashes corr.rwl.seg().
# default = min(50, minLen)
# suspendWhenHidden = FALSE ensures the slider renders even when the
# Analysis Parameters accordion is closed, so input$seg.length is never NULL.
output$seg.length.ui <- renderUI({
if (is.null(rwlRV$dated)) {
sliderInput("seg.length", "Segment Length",
min = 10, max = 100, value = 50, step = 10, ticks = FALSE)
} else {
ml <- rwlStats()$minLen
maxSeg <- max(10, ml)
defSeg <- min(50, maxSeg)
sliderInput("seg.length", "Segment Length",
min = 10, max = maxSeg, value = defSeg, step = 10,
ticks = FALSE)
}
})
outputOptions(output, "seg.length.ui", suspendWhenHidden = FALSE)
# ── rwlQA: tiered data quality check ──────────────────────────────────────
# Depends on rwlRV$dated and input$seg.length. Returns a list:
# tier — 0 = ok, 1 = hard block, 2 = soft warning, 3 = advisory
# message — plain-language explanation for the user
# short — character vector of series names that are too short (tier 2)
#
# Tier 1 (hard block): fewer than 5 series OR all series shorter than seg.length
# Tier 2 (soft warn): some (not all) series shorter than 1.5 * seg.length
# Tier 3 (advisory): mean series length < 3 * seg.length
rwlQA <- reactive({
req(rwlRV$dated, input$seg.length)
dat <- rwlRV$dated
segLen <- input$seg.length
lengths <- colSums(!is.na(dat))
nSeries <- ncol(dat)
# Tier 1 checks
if (nSeries < 5) {
return(list(
tier = 1,
message = paste0(
"This file has only ", nSeries, " series. Statistical crossdating ",
"requires at least 5 series to build a meaningful master chronology. ",
"Please load a file with more series."
),
short = character(0)
))
}
if (all(lengths < segLen)) {
return(list(
tier = 1,
message = paste0(
"All series are shorter than the current segment length (", segLen,
" years). No correlations can be computed. Try reducing the segment ",
"length in the Analysis Parameters, or load a file with longer series."
),
short = character(0)
))
}
# Tier 2 check
shortNames <- names(lengths[lengths < 1.5 * segLen])
if (length(shortNames) > 0) {
return(list(
tier = 2,
message = paste0(
length(shortNames), " series ",
if (length(shortNames) == 1) "is" else "are",
" too short for reliable crossdating at the current segment length (",
segLen, " years): ",
paste(shortNames, collapse = ", "),
". Consider removing ",
if (length(shortNames) == 1) "it" else "them",
" using the filter on the Correlations panel, or reducing the segment length."
),
short = shortNames
))
}
# Tier 3 check
meanLen <- mean(lengths)
if (meanLen < 3 * segLen) {
return(list(
tier = 3,
message = paste0(
"Mean series length (", round(meanLen, 0), " years) is short relative ",
"to the segment length (", segLen, " years). Correlations may have ",
"low statistical power."
),
short = character(0)
))
}
list(tier = 0, message = "", short = character(0))
})
# ── getCRS ─────────────────────────────────────────────────────────────────
# Runs corr.rwl.seg() on the current master. All correlation panels read
# from this single reactive so parameter changes propagate everywhere.
getCRS <- reactive({
req(rwlRV$dated, input$seg.length)
# Hard block: do not attempt computation if data fail QA tier 1
qa <- rwlQA()
if (qa$tier == 1) return(NULL)
corr.rwl.seg(
rwlRV$dated,
seg.length = input$seg.length,
bin.floor = as.numeric(input$bin.floor),
n = resolveN(input$n),
prewhiten = input$prewhiten,
pcrit = input$pcrit,
biweight = input$biweight,
method = input$method,
make.plot = FALSE
)
})
# ── getFloater ─────────────────────────────────────────────────────────────
# Runs dplR::xdate.floater() for the selected undated series. The floater
# panel has its own parameter inputs (seg.lengthUndated etc.) because floater
# analysis is typically run with different settings than the main crossdating.
# make.plot = FALSE and verbose = FALSE because the app handles display itself.
getFloater <- reactive({
req(filteredRWL(), input$series2, rwlRV$undated,
input$minOverlapUndated)
series2date <- rwlRV$undated[, input$series2]
fo <- dplR::xdate.floater(
rwl = filteredRWL(),
series = series2date,
series.name = input$series2,
min.overlap = input$minOverlapUndated,
n = NULL,
prewhiten = TRUE,
biweight = TRUE,
method = "spearman",
make.plot = FALSE,
verbose = FALSE,
return.rwl = TRUE
)
if (is.null(names(fo))) {
names(fo) <- c("floaterCorStats", "rwlCombined")
}
fo$series.name <- input$series2
fo
})
# ── getSeries4Editing ──────────────────────────────────────────────────────
# Prepares the selected series for the edit table. Splits the dated rwl into
# the series being edited and everything else (datedNoSeries), so edits can
# be recombined via combine.rwl() after each insert/delete operation.
getSeries4Editing <- reactive({
req(filteredRWL(), input$series)
dat <- rwlRV$dated
datNoSeries <- dat[, names(dat) != input$series, drop = FALSE]
series <- dat[, input$series]
mask <- is.na(series)
seriesDF <- data.frame(Year = time(datNoSeries)[!mask],
Value = series[!mask])
rwlRV$datedNoSeries <- datNoSeries
rwlRV$seriesDF <- seriesDF
})
# ════════════════════════════════════════════════════════════════════════════
# OVERVIEW
# ════════════════════════════════════════════════════════════════════════════
# ── overviewUI: switches between welcome state and data state ─────────────
# When no data is loaded: SVG tree-ring art with onboarding instructions.
# When data is loaded: summary card + RWL plot + collapsible series table.
#
# IMPORTANT: uiOutput("rwlSummaryHeader") inside the data state is a
# placeholder — it requires its own output$rwlSummaryHeader renderUI below.
# Nested uiOutputs always need their own registered render calls.
output$overviewUI <- renderUI({
if (is.null(getRWL()) && is.null(rwlRV$readError)) {
# ── Welcome state ────────────────────────────────────────────────────
HTML('
<svg width="100%" viewBox="0 0 680 420" xmlns="http://www.w3.org/2000/svg">
<g opacity="0.18">
<circle cx="0" cy="210" r="390" fill="none" stroke="#2C5F2E" stroke-width="18"/>
<circle cx="0" cy="210" r="360" fill="none" stroke="#2C5F2E" stroke-width="10"/>
<circle cx="0" cy="210" r="336" fill="none" stroke="#2C5F2E" stroke-width="20"/>
<circle cx="0" cy="210" r="308" fill="none" stroke="#2C5F2E" stroke-width="8"/>
<circle cx="0" cy="210" r="288" fill="none" stroke="#2C5F2E" stroke-width="22"/>
<circle cx="0" cy="210" r="256" fill="none" stroke="#2C5F2E" stroke-width="12"/>
<circle cx="0" cy="210" r="232" fill="none" stroke="#2C5F2E" stroke-width="16"/>
<circle cx="0" cy="210" r="206" fill="none" stroke="#2C5F2E" stroke-width="8"/>
<circle cx="0" cy="210" r="188" fill="none" stroke="#2C5F2E" stroke-width="24"/>
<circle cx="0" cy="210" r="154" fill="none" stroke="#2C5F2E" stroke-width="10"/>
<circle cx="0" cy="210" r="134" fill="none" stroke="#2C5F2E" stroke-width="18"/>
<circle cx="0" cy="210" r="106" fill="none" stroke="#2C5F2E" stroke-width="8"/>
<circle cx="0" cy="210" r="88" fill="none" stroke="#2C5F2E" stroke-width="20"/>
<circle cx="0" cy="210" r="60" fill="none" stroke="#2C5F2E" stroke-width="14"/>
<circle cx="0" cy="210" r="38" fill="none" stroke="#2C5F2E" stroke-width="10"/>
<circle cx="0" cy="210" r="20" fill="#2C5F2E" opacity="0.5"/>
</g>
<line x1="0" y1="210" x2="420" y2="210" stroke="#2C5F2E" stroke-width="0.5"
opacity="0.5" stroke-dasharray="4 4"/>
<text x="340" y="110" text-anchor="middle" font-family="sans-serif"
font-size="32" font-weight="500" fill="#2C5F2E" opacity="0.9">xDateR</text>
<text x="340" y="148" text-anchor="middle" font-family="sans-serif"
font-size="14" fill="#666">Statistical crossdating for tree-ring data</text>
<line x1="280" y1="168" x2="400" y2="168" stroke="#ccc" stroke-width="0.5"/>
<circle cx="252" cy="210" r="14" fill="#2C5F2E" opacity="0.15"/>
<text x="252" y="215" text-anchor="middle" font-family="sans-serif"
font-size="13" font-weight="500" fill="#2C5F2E">1</text>
<text x="276" y="207" font-family="sans-serif" font-size="13"
font-weight="500" fill="#333">Upload a ring-width file</text>
<text x="276" y="222" font-family="sans-serif" font-size="12"
fill="#888">Tucson, Heidelberg, compact, or TRiDaS format</text>
<circle cx="252" cy="262" r="14" fill="#2C5F2E" opacity="0.15"/>
<text x="252" y="267" text-anchor="middle" font-family="sans-serif"
font-size="13" font-weight="500" fill="#2C5F2E">2</text>
<text x="276" y="259" font-family="sans-serif" font-size="13"
font-weight="500" fill="#333">Inspect and crossdate</text>
<text x="276" y="274" font-family="sans-serif" font-size="12"
fill="#888">Correlations, segment analysis, skeleton plots</text>
<circle cx="252" cy="314" r="14" fill="#2C5F2E" opacity="0.15"/>
<text x="252" y="319" text-anchor="middle" font-family="sans-serif"
font-size="13" font-weight="500" fill="#2C5F2E">3</text>
<text x="276" y="311" font-family="sans-serif" font-size="13"
font-weight="500" fill="#333">Edit and export</text>
<text x="276" y="326" font-family="sans-serif" font-size="12"
fill="#888">Insert or delete rings, download corrected .rwl</text>
<rect x="208" y="356" width="264" height="32" rx="6"
fill="#2C5F2E" opacity="0.08"/>
<text x="340" y="377" text-anchor="middle" font-family="sans-serif"
font-size="12" fill="#2C5F2E">Or check "Use example data" to explore</text>
</svg>
')
} else {
# ── Data loaded state ────────────────────────────────────────────────
# Build alert list — only non-NULL elements are included in tagList
readErrAlert <- if (!is.null(rwlRV$readError)) {
div(class = "alert alert-danger",
bs_icon("x-circle"), " ",
tags$strong("File could not be read."),
" dplR's ", tags$code("read.rwl()"), " returned the following error:",
tags$pre(class = "mt-2 mb-1", style = "font-size:0.85em;",
rwlRV$readError),
"Please open a plain R session, load dplR, and run ",
tags$code('read.rwl("your-file.rwl")'),
" to diagnose the problem. Fix the file and then reload it here."
)
}
qaAlert <- if (!is.null(rwlRV$dated) && !is.null(input$seg.length)) {
qa <- rwlQA()
if (qa$tier == 1) {
div(class = "alert alert-danger mb-2",
bs_icon("x-circle"), " ",
tags$strong("Data cannot be crossdated statistically. "),
qa$message)
} else if (qa$tier == 2) {
div(class = "alert alert-warning mb-2",
bs_icon("exclamation-triangle"), " ",
tags$strong("Some series are too short. "),
qa$message)
} else if (qa$tier == 3) {
div(class = "alert alert-info mb-2",
bs_icon("info-circle"), " ",
tags$strong("Note: "),
qa$message)
}
}
tagList(
readErrAlert,
qaAlert,
card(
fill = FALSE,
card_header(
"Data Summary",
tooltip(
bsicons::bs_icon("question-circle"),
paste("Key statistics from rwl.report(). Check these to confirm",
"your file was read correctly — number of series, span,",
"mean series length, and interseries correlation. Proceed",
"to the Correlations tab to begin crossdating.")
)
),
uiOutput("rwlSummaryHeader")
),
card(
fill = FALSE,
card_header(
layout_columns(
col_widths = c(8, 4),
span("RWL Plot",
tooltip(
bsicons::bs_icon("question-circle"),
paste("Segment view: each series shown as a horizontal bar spanning",
"its dated range. Spaghetti view: all ring-width series",
"plotted as lines — useful for spotting outlier series.",
"Uses plot.rwl() from dplR.")
)
),
div(
class = "d-flex justify-content-end",
selectInput(
inputId = "rwlPlotType",
label = NULL,
choices = c("Segment" = "seg", "Spaghetti" = "spag"),
selected = "seg",
width = "150px"
)
)
)
),
plotOutput("rwlPlot", height = "500px")
),
accordion(
open = FALSE,
accordion_panel(
title = "Series Summary Table",
icon = bsicons::bs_icon("table"),
helpText("Summary statistics for each series from summary.rwl(). Includes start year, end year, length, mean, and AR1."),
tableOutput("rwlSummary")
)
),
div(class = "mt-2", downloadButton("rwlSummaryReport", "Generate report"))
)
}
})
# ── rwlSummaryHeader: fills the nested uiOutput in the data loaded state ──
# Must be registered as its own output — a uiOutput inside a renderUI still
# needs its own render call in the server. Without this the card body is blank.
output$rwlSummaryHeader <- renderUI({
req(filteredRWL())
rwlSummaryCard(rwlRV$dated)
})
output$rwlPlot <- renderPlot({
req(filteredRWL())
plot.rwl(filteredRWL(), plot.type = input$rwlPlotType)
}, height = 400)
output$rwlSummary <- renderTable({
req(filteredRWL())
summary(filteredRWL())
})
output$rwlSummaryReport <- downloadHandler(
filename = "rwl_summary_report.html",
content = function(file) {
tempReport <- file.path(tempdir(), "report_rwl_describe.rmd")
file.copy("report_rwl_describe.rmd", tempReport, overwrite = TRUE)
params <- list(fileName = input$file1$name,
rwlObject = rwlRV$dated,
rwlPlotType = input$rwlPlotType)
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv()))
}
)
# ── qaAlertCorr: QA banner for the Correlations panel ────────────────────
# Shows tier 2 and tier 3 alerts at the top of the Correlations panel.
# Tier 1 is not shown here — getCRS() is already blocked so nothing renders.
output$qaAlertCorr <- renderUI({
req(rwlRV$dated, input$seg.length)
qa <- rwlQA()
if (qa$tier == 2) {
div(class = "alert alert-warning",
bs_icon("exclamation-triangle"), " ",
tags$strong("Some series are too short. "),
qa$message)
} else if (qa$tier == 3) {
div(class = "alert alert-info",
bs_icon("info-circle"), " ",
tags$strong("Note: "),
qa$message)
}
})
# ════════════════════════════════════════════════════════════════════════════
# CORRELATIONS
# ════════════════════════════════════════════════════════════════════════════
# ── crsPlotUI: dynamic height container for the correlation tile plot ────
# plotlyOutput height must be set on the widget div itself — setting height
# inside plotly layout() alone is not enough as Shiny clips the container.
# We compute height from the number of series so the plot is never cramped.
output$crsPlotUI <- renderUI({
req(filteredRWL())
nSeries <- ncol(rwlRV$dated)
# Each series gets two tile rows at ~56px each plus generous margins
# for the x-axis and top padding. Minimum 400px.
plotHeight <- paste0(max(300, nSeries * 22 + 200), "px")
plotlyOutput("crsFancyPlot", height = plotHeight)
})
# Interactive plotly tile plot — see plotlyCRSFunc.R for implementation.
# Uses pure plotly (no ggplotly) to avoid factor coercion errors.
output$crsFancyPlot <- renderPlotly({
req(filteredRWL())
crsPlotly(getCRS())
})
# Overall mean correlation per series across all bins
output$crsOverall <- renderDT({
req(filteredRWL())
crsObject <- getCRS()
overallCor <- round(crsObject$overall, 3)
res <- data.frame(Series = rownames(overallCor),
Correlation = overallCor[, 1])
datatable(res, rownames = FALSE,
caption = "Overall Series Correlation",
options = list(pageLength = nrow(res),
searching = FALSE,
lengthChange = FALSE)) %>%
formatStyle("Correlation",
fontWeight = styleInterval(crsObject$pcrit,
c("normal", "bold")))
})
# Average correlation within each time bin across all series
output$crsAvgCorrBin <- renderDT({
req(filteredRWL())
crsObject <- getCRS()
binNames <- paste(crsObject$bins[, 1], "-", crsObject$bins[, 2], sep = "")
res <- data.frame(Bin = binNames,
Correlation = round(crsObject$avg.seg.rho, 3))
datatable(res, rownames = FALSE,
caption = "Avg. Correlation by Bin",
options = list(pageLength = nrow(res),
searching = FALSE,
lengthChange = FALSE)) %>%
formatStyle("Correlation",
fontWeight = styleInterval(crsObject$pcrit,
c("normal", "bold")))
})
# Series/segments with p > pcrit — these are the ones to investigate
output$crsFlags <- renderDT({
req(filteredRWL())
crsObject <- getCRS()
flags <- crsObject$flags
res <- if (length(flags) == 0) {
data.frame(Series = character(0), Bins = character(0))
} else {
flags <- unlist(flags)
data.frame(Series = names(flags),
Bins = gsub("\\.", "-", flags))
}
datatable(res, rownames = FALSE,
caption = "Flagged Series / Segments",
options = list(pageLength = max(5, nrow(res)),
searching = FALSE,
lengthChange = FALSE))
})
# Full correlation matrix: series x bin
output$crsCorrBin <- renderDT({
req(filteredRWL())
crsObject <- getCRS()
binNames <- paste(crsObject$bins[, 1], "-", crsObject$bins[, 2], sep = "")
res <- round(crsObject$spearman.rho, 3)
res <- data.frame(Series = rownames(res), res)
colnames(res) <- c("Series", binNames)
datatable(res, rownames = FALSE,
caption = "Series Correlation by Bin",
options = list(pageLength = min(30, nrow(res)),
searching = TRUE,
lengthChange = FALSE)) %>%
formatStyle(columns = -1,
fontWeight = styleInterval(crsObject$pcrit,
c("normal", "bold")))
})
# Report: captures all parameters for reproducibility
output$crsReport <- downloadHandler(
filename = "rwl_correlation_report.html",
content = function(file) {
tempReport <- file.path(tempdir(), "report_rwl_corr.rmd")
file.copy("report_rwl_corr.rmd", tempReport, overwrite = TRUE)
crsParams <- list(seg.length = input$seg.length,
bin.floor = input$bin.floor,
n = input$n,
prewhiten = input$prewhiten,
pcrit = input$pcrit,
biweight = input$biweight,
method = input$method)
params <- list(fileName = input$file1$name,
crsObject = getCRS(),
crsParams = crsParams)
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv()))
}
)
# ════════════════════════════════════════════════════════════════════════════
# SERIES
# ════════════════════════════════════════════════════════════════════════════
# Alert banner listing any flagged series from the Correlations panel.
# Renders NULL (no banner) if nothing is flagged.
output$flaggedSeriesUI <- renderUI({
req(filteredRWL())
crsObject <- getCRS()
flags <- crsObject$flags
if (length(flags) == 0) return(NULL)
flags <- unlist(flags)
series2get <- names(flags)
msg <- if (length(series2get) == 1) {
paste("Series", series2get, "was flagged.")
} else if (length(series2get) == 2) {
paste("Series", paste(series2get, collapse = " and "), "were flagged.")
} else {
paste("These series were flagged:",
paste(series2get, collapse = ", "), ".")
}
div(class = "alert alert-warning", msg)
})
# Segment correlation plot: corr.series.seg() for the selected series
output$cssPlot <- renderPlot({
req(filteredRWL(), input$series)
corr.series.seg(
rwlRV$dated,
series = input$series,
seg.length = input$seg.length,
bin.floor = as.numeric(input$bin.floor),
n = resolveN(input$n),
prewhiten = input$prewhiten,
pcrit = input$pcrit,
biweight = input$biweight,
method = input$method,
make.plot = TRUE
)
}, height = 400)
# Cross-correlation plot: ccf.series.rwl() for the selected series and window
output$ccfPlot <- renderPlot({
req(filteredRWL(), input$series, input$rangeCCF)
dat <- rwlRV$dated
yrs <- time(dat)
win <- input$rangeCCF[1]:input$rangeCCF[2]
dat <- dat[yrs %in% win, , drop = FALSE]
ccf.series.rwl(
dat,
series = input$series,
seg.length = input$seg.length,
bin.floor = as.numeric(input$bin.floor),
n = resolveN(input$n),
prewhiten = input$prewhiten,
pcrit = input$pcrit,
biweight = input$biweight,
method = input$method,
lag.max = input$lagCCF,
make.plot = TRUE
)
}, height = 400)
# Report: includes dating notes for the analyst's reasoning
output$cssReport <- downloadHandler(
filename = "series_correlation_report.html",
content = function(file) {
tempReport <- file.path(tempdir(), "report_series.rmd")
file.copy("report_series.rmd", tempReport, overwrite = TRUE)
cssParams <- list(seg.length = input$seg.length,
bin.floor = input$bin.floor,
n = input$n,
prewhiten = input$prewhiten,
pcrit = input$pcrit,
biweight = input$biweight,
method = input$method,
series = input$series,
lagCCF = input$lagCCF,
datingNotes = input$datingNotes,
winCCF = input$rangeCCF[1]:input$rangeCCF[2])
params <- list(fileName = input$file1$name,
rwlObject = rwlRV$dated,
cssParams = cssParams)
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv()))
}
)
# ════════════════════════════════════════════════════════════════════════════
# EDIT
# ════════════════════════════════════════════════════════════════════════════
# Skeleton / CCF plot: xskel.ccf.plot() centred on the current window
output$xskelPlot <- renderPlot({
req(filteredRWL(), input$series, input$winCenter, input$winWidth)
wStart <- input$winCenter - (input$winWidth / 2)
xskel.ccf.plot(
rwlRV$dated,
series = input$series,
win.start = wStart,
win.width = input$winWidth,
n = resolveN(input$n),
prewhiten = input$prewhiten,
biweight = input$biweight
)
}, height = 400)
output$series2edit <- renderText({
paste("Series", input$series, "selected")
})
# Scrollable measurements table — automatically scrolls to window center.
# Click a row to select it, then use the edit controls to modify it.
output$table1 <- renderDataTable({
req(filteredRWL(), input$series, input$winCenter)
getSeries4Editing()
wStart <- input$winCenter - 10
wEnd <- input$winCenter + 10
nRows <- length(seq(wStart, wEnd)) * 33.33
row2start <- which(rwlRV$seriesDF[, 1] == wStart)
datatable(
rwlRV$seriesDF,
selection = list(mode = "single", target = "row"),
extensions = "Scroller",
rownames = FALSE,
options = list(
deferRender = TRUE,
autoWidth = TRUE,
scrollY = nRows,
scroller = TRUE,
searching = FALSE,
lengthChange = FALSE,
columnDefs = list(list(className = "dt-left", targets = "_all")),
initComplete = JS('function() {this.api().table().scroller.toPosition(',
row2start - 1, ');}')
)
)
})
output$editLog <- renderPrint({
req(filteredRWL())
if (!is.null(rwlRV$editLog)) rwlRV$editLog
})
# Controls the visibility of the Save/Revert card — only shown after edits
output$showSaveEdits <- reactive({
!all(sapply(rwlRV$editDF, is.null))
})
outputOptions(output, "showSaveEdits", suspendWhenHidden = FALSE)
# Show/hide the save card based on whether any edits exist.
# Using shinyjs rather than renderUI because verbatimTextOutput("editLog")
# inside a renderUI doesn't bind correctly in Shiny.
observe({
if (!is.null(rwlRV$editDF) && nrow(rwlRV$editDF) > 0) {
shinyjs::show("divSaveEdits")
} else {
shinyjs::hide("divSaveEdits")
}
})
# ── Delete ring ────────────────────────────────────────────────────────────
# Removes the selected row from seriesDF, renumbers years according to
# fix.last logic, recombines with the rest of the rwl, and updates the log.
observeEvent(input$deleteRows, {
req(filteredRWL())
if (!is.null(input$table1_rows_selected)) {
seriesDF <- rwlRV$seriesDF
row2delIndex <- as.numeric(input$table1_rows_selected)
yearDeleted <- seriesDF$Year[row2delIndex]