tests/shiny-flow.R

library(shiny)
library(iSTATS)

app_file <- function(name) {
  path <- system.file("app", name, package = "iSTATS")
  stopifnot(nzchar(path))
  path
}

.observer_for <- function(path, input_id) {
  expressions <- parse(path)
  matches <- Filter(function(expr) {
    is.call(expr) && identical(expr[[1L]], as.name("observeEvent")) &&
      is.call(expr[[2L]]) && identical(expr[[2L]][[1L]], as.name("$")) &&
      identical(as.character(expr[[2L]][[3L]]), input_id)
  }, expressions)
  stopifnot(length(matches) == 1L)
  matches[[1L]]
}

axis <- seq(1, -1, length.out = 64)
peak <- exp(-((seq_len(64) - 32) / 4)^2)
spectra_by_file <- list(
  sample10 = c(0, 10 * peak[-64]),
  sample2 = 2 * peak
)
fixture_dir <- tempfile("icoshift-flow-")
dir.create(fixture_dir)
fixture_paths <- file.path(fixture_dir, c("sample10.csv", "sample2.csv"))
for (i in seq_along(fixture_paths)) {
  write.table(cbind(axis, spectra_by_file[[i]]), fixture_paths[i], sep = ",",
              col.names = FALSE, row.names = FALSE)
}
on.exit(unlink(fixture_dir, recursive = TRUE), add = TRUE)

server_flow <- function(input, output, session) {
  source(app_file("functions.R"), local = TRUE)

  NMRData <- matrix(0, 2, length(axis))
  NMRData_plot <- NMRData
  CS_values_real <- rbind(axis, axis)
  file_names <- c("placeholder1", "placeholder2")
  old_sel <- 0
  refresh_count <- 0L
  refreshval <- function() refresh_count <<- refresh_count + 1L
  icoshift_status <- reactiveVal("No alignment has been applied.")
  icoshift_editable_intervals <- reactiveVal(matrix(c(-0.8, 0.8), nrow = 1L))
  icoshift_interval_references <- reactiveVal(NULL)
  icoshift_applied_intervals <- reactiveVal(NULL)
  output$icoshift_status <- renderText(icoshift_status())
  icoshift_detection <- reactiveVal(NULL)
  alignment_original <- reactiveVal(NULL)
  alignment_aligned <- reactiveVal(NULL)
  alignment_original_cache <- reactiveVal(NULL)
  alignment_cache_dir <- reactiveVal(NULL)
  alignment_view_range <- reactiveVal(NULL)
  alignment_view_y <- reactiveVal(NULL)
  alignment_highlight <- reactiveVal(NULL)
  alignment_data_version <- reactiveVal(0L)
  alignment_memory_point_limit <- 65536L

  # Use the app's own source/result helpers, so importers reset the immutable
  # source and alignment commits both the working and the aligned matrices.
  helper_expressions <- parse(app_file("Pretreatment_server.R"))
  for (name in c(".remove_alignment_cache", ".alignment_original_data",
                 ".reset_alignment_source", ".commit_icoshift_alignment"))
    eval(Filter(function(expr) {
      is.call(expr) && identical(expr[[1L]], as.name("<-")) &&
        identical(as.character(expr[[2L]]), name)
    }, helper_expressions)[[1L]], envir = environment())

  eval(.observer_for(app_file("uploader_server_CSV.R"), "file2"))
  eval(.observer_for(app_file("uploader_server_CSV.R"), "import"))
  eval(.observer_for(app_file("Pretreatment_server.R"), "icoshift_align"))
}

uploads <- data.frame(
  name = c("sample10.csv", "sample2.csv"),
  size = file.info(fixture_paths)$size,
  type = "text/csv",
  datapath = fixture_paths,
  stringsAsFactors = FALSE
)

testServer(server_flow, {
  eval(quote({
    session$setInputs(file2 = uploads, ls_lims = "-1", hs_lims = "1")
    stopifnot(identical(as.character(up2$name), c("sample2.csv", "sample10.csv")))

    session$setInputs(import = 1)
    stopifnot(identical(file_names, c("sample2", "sample10")),
              identical(dim(NMRData), c(2L, 64L)),
              identical(NMRData_plot, NMRData),
              max(NMRData[1, ]) < max(NMRData[2, ]),
              isTRUE(all.equal(CS_values_real[1, ], axis,
                               check.attributes = FALSE)),
              refresh_count == 1L,
              identical(alignment_original(), NMRData),
              is.null(alignment_aligned()))

    imported <- NMRData
    imported_names <- file_names
    imported_axis <- CS_values_real
    intervals <- .parse_icoshift_intervals("-0.8, 0.8", axis)
    expected <- suppressMessages(.run_icoshift_alignment(imported, axis, intervals))
    session$setInputs(icoshift_intervals = "-0.8, 0.8",
                      icoshift_reference = "average", icoshift_align = 1)
    stopifnot(identical(NMRData, expected$xCS),
              identical(NMRData_plot, expected$xCS),
              identical(file_names, imported_names),
              identical(CS_values_real, imported_axis),
              identical(icoshift_applied_intervals(), intervals),
              identical(alignment_aligned(), expected$xCS),
              identical(alignment_original(), imported),
              refresh_count == 2L)

    selected <- c(45L, 20L)
    export <- .selected_points_export(NMRData, selected,
                                      CS_values_real[1, ], file_names)
    csv <- tempfile(fileext = ".csv")
    write.table(export, csv, sep = ",", col.names = TRUE, row.names = FALSE)
    downloaded <- read.csv(csv, check.names = FALSE, stringsAsFactors = FALSE)
    unlink(csv)
    selected <- sort(selected)
    stopifnot(identical(downloaded$Sample, file_names),
              identical(names(downloaded)[-1L],
                        as.character(CS_values_real[1, selected])),
              isTRUE(all.equal(as.matrix(downloaded[-1L]),
                               NMRData[, selected, drop = FALSE],
                               check.attributes = FALSE)))
  }), envir = environment(.run_icoshift_alignment))
})

cat("PASS complete light flow: unordered CSV import, alignment, selection and named CSV export\n")

Try the iSTATS package in your browser

Any scripts or data that you put into this service are public.

iSTATS documentation built on Nov. 6, 2026, 1:07 a.m.