tests/shiny-stocsy.R

library(shiny)
library(iSTATS)
cat(R.version.string, "\nshiny", as.character(packageVersion("shiny")), "\n")

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

server <- function(input, output, session) {
  source(app_file("functions.R"), local = TRUE)
  set.seed(17)
  NMRData <- matrix(rnorm(60), 10, 6)
  NMRData_plot <- rbind(positive = seq(2, 8, length.out = 6),
                        negative = seq(-8, -2, length.out = 6),
                        constant = rep(1e-12, 6))
  NMRData[, 3] <- -NMRData[, 2]
  col_select <- c(2L, 3L, 5L)
  CS_values_real <- matrix(6:1, 1)
  CS_selection <- reactiveValues(vranges = CS_values_real[1, col_select])
  facts <- reactiveValues(fac_stocsy_i = NULL)
  facts_is <- reactiveValues(fac_stocsy_is = NULL)
  facts_rt <- reactiveValues(fac_stocsy_rt = NULL)
  rr_n <- reactiveValues(csranges = NULL)
  rr_nn <- reactiveValues(csranges = NULL)
  spectrums_stocsy_is <- reactiveValues(dat = data.frame(Spectrum = NMRData[1, ]))
  sel_ind <- 1
  p_value <- 0.05
  file_names <- c("positive", "negative", "constant", letters[4:10])
  stop_menssager <- 1
  cor_cutoff_p <- 0.9
  cor_cutoff_n <- -0.9
  cor_cutoff <- cor_cutoff_rt <- 0.8
  rr <- rr_is <- rr_rt <- rep("B", 6)
  testy_stocsy_i <- testy_stocsy_is <- testy_stocsy_rt <-
    data.frame(Chemical_Shift = CS_values_real[1, ])
  ranges_stocsy_i <- reactiveValues(x = range(CS_values_real[1, ]),
                                    rt = range(CS_values_real[1, ]),
                                    y = .stocsy_ylim(NMRData_plot[1, ]))
  ranges_stocsy_is <- reactiveValues(x = range(CS_values_real[1, ]),
                                     y = .stocsy_ylim(NMRData_plot[1, ]))
  ranges_stocsy_rt <- reactiveValues(x = range(CS_values_real[1, ]),
                                     y = .stocsy_ylim(NMRData_plot[1, ]))
  spectrums_stocsy_i <- reactiveValues(dat = data.frame(
    Chemical_Shift = CS_values_real[1, ], Spectrum = NMRData_plot[1, ]))
  spectrums_stocsy_is <- reactiveValues(dat = data.frame(
    Chemical_Shift = CS_values_real[1, ], Spectrum = NMRData_plot[1, ]))
  spectrums_stocsy_rt <- reactiveValues(dat = data.frame(
    Chemical_Shift = CS_values_real[1, ], Spectrum = NMRData_plot[1, ]))
  chkzoom_stocsy_i <- chkzoom_stocsy_is <- chkzoom_stocsy_rt <- 1
  idb_stocsy_i <- idb_stocsy_is <- idb_stocsy_rt <- 0
  peran_stocsy_i <- peran_stocsy_is <- peran_stocsy_rt <- 0
  events <- c("s_stocsy", "sel_cor", "dblclick_stocsy_i", "dblclick_stocsy_is",
              "dblclick_stocsy_rt", "hover_stocsy_rt", "cutoff_stocsy_i",
              "cutoff_stocsy_is", "cutoff_stocsy_rt", "radio", "radio_is",
              "radio_rt", "stocsy_s", "spectrum_list_stocsy_i",
              "spectrum_list_stocsy_is", "spectrum_list_stocsy_rt",
              "plot_brush_stocsy_i", "plot_brush_stocsy_is",
              "plot_brush_stocsy_rt", "x2_stocsy_i", "x2_stocsy_is",
              "x2_stocsy_rt", "all_stocsy_i", "all_stocsy_is",
              "all_stocsy_rt")
  for (file in c("select_signals_server.R", "stocsy_i_server.R",
                 "stocsy_is_server.R", "stocsy_rt_server.R")) {
    for (expr in parse(app_file(file))) {
      if (is.call(expr) && identical(expr[[1]], as.name("observeEvent")) &&
          as.character(expr[[2]][[3]]) %in% events) eval(expr)
      if (is.call(expr) && identical(expr[[1]], as.name("<-")) &&
          is.name(expr[[2]]) && grepl("^\\.stocsy_.+_show_all$", as.character(expr[[2]]))) eval(expr)
    }
  }
}
testServer(server, {
  eval(quote({
  expect_margin <- function(values, limits) {
    values <- values[is.finite(values)]
    stopifnot(all(is.finite(limits)), limits[1L] < limits[2L],
              isTRUE(all.equal((min(values) - limits[1L]) / diff(limits),
                               0.10, tolerance = 1e-9)))
  }
  for (suffix in c("i", "is", "rt")) {
    selector <- paste0("spectrum_list_stocsy_", suffix)
    do.call(session$setInputs, stats::setNames(list("negative"), selector))
    spectrum <- get(paste0("spectrums_stocsy_", suffix))$dat$Spectrum
    limits <- get(paste0("ranges_stocsy_", suffix))$y
    expect_margin(spectrum, limits)

    brush_id <- paste0("plot_brush_stocsy_", suffix)
    manual_limits <- c(-20, 20)
    brush <- list(xmin = 2, xmax = 5, ymin = manual_limits[1L],
                  ymax = manual_limits[2L])
    do.call(session$setInputs, stats::setNames(list(brush), brush_id))
    do.call(session$setInputs, stats::setNames(list("constant"), selector))
    stopifnot(identical(get(paste0("ranges_stocsy_", suffix))$y,
                        manual_limits))

    scale_id <- paste0("x2_stocsy_", suffix)
    do.call(session$setInputs, stats::setNames(list(1), scale_id))
    stopifnot(identical(get(paste0("ranges_stocsy_", suffix))$y,
                        manual_limits))
    all_id <- paste0("all_stocsy_", suffix)
    do.call(session$setInputs, stats::setNames(list(1), all_id))
    spectrum <- get(paste0("spectrums_stocsy_", suffix))$dat$Spectrum
    expect_margin(spectrum, get(paste0("ranges_stocsy_", suffix))$y)
  }

  # Start STOCSY opens every view as "Show all", whatever zoom was left.
  for (suffix in c("i", "is", "rt")) {
    brush <- list(xmin = 2, xmax = 5, ymin = -20, ymax = 20)
    do.call(session$setInputs, stats::setNames(list(brush),
                                               paste0("plot_brush_stocsy_", suffix)))
    do.call(session$setInputs, stats::setNames(list(2), paste0("x2_stocsy_", suffix)))
  }
  session$setInputs(s_stocsy = 1)
  for (suffix in c("i", "is", "rt")) {
    stopifnot(identical(get(paste0("chkzoom_stocsy_", suffix)), 1),
              identical(get(paste0("idb_stocsy_", suffix)), 0),
              isTRUE(all.equal(sort(get(paste0("ranges_stocsy_", suffix))$x),
                               range(CS_values_real[1, ]))))
    expect_margin(get(paste0("spectrums_stocsy_", suffix))$dat$Spectrum,
                  get(paste0("ranges_stocsy_", suffix))$y)
  }
  expect_current <- function() {
    m <- .stocsy_data
    cat("check driver", drv_pk, "columns", ncol(m), "\n")
    stopifnot(isTRUE(all.equal(cor_pearson, unname(cor(m)[drv_pk, ]))),
              isTRUE(all.equal(cor_spearman, unname(cor(m, method = "spearman")[drv_pk, ]))))
  }
  expect_current()
  session$setInputs(radio = 1)
  stopifnot(identical(cor_cutoff_value_n, -0.9),
            identical(cor_cutoff_value_p, 0.9))
  session$setInputs(cutoff_stocsy_i = c(-0.9, 0.9), cutoff_stocsy_is = 0.8, cutoff_stocsy_rt = 0.8)
  session$setInputs(dblclick_stocsy_i = list(x = 5))
  stopifnot(drv_pk == 1L, facts$fac_stocsy_i[2] == "A", facts$fac_stocsy_i[3] == "C")
  expect_current()
  session$setInputs(dblclick_stocsy_is = list(x = 4))
  stopifnot(drv_pk == 2L)
  expect_current()
  session$setInputs(dblclick_stocsy_rt = list(x = 2))
  stopifnot(drv_pk == 3L)
  expect_current()
  session$setInputs(hover_stocsy_rt = list(x = 5))
  stopifnot(drv_pk == 1L)
  expect_current()
  session$setInputs(hover_stocsy_rt = list(x = 6))
  stopifnot(drv_pk == 1L)
  for (mode in 1:3) session$setInputs(radio = mode, radio_is = mode, radio_rt = mode)
  session$setInputs(stocsy_s = 1)
  .s_stocsy(NMRData[, col_select, drop = FALSE]^2)
  expect_current()
  session$setInputs(sel_cor = 1)
  session$setInputs(hover_stocsy_rt = list(x = 4))
  stopifnot(length(cor_pearson) == 0L)
  col_select <- 2L
  CS_selection$vranges <- 5
  session$setInputs(s_stocsy = 2)
  stopifnot(length(cor_pearson) == 1L)
  expect_current()
  NMRData[, 2] <- 1
  NMRData[1, 2] <- NA_real_
  suppressWarnings(session$setInputs(s_stocsy = 3))
  stopifnot(cor_pearson == 1, cor_spearman == 1, norm_var == "UNAVAILABLE")
  }), envir = environment(.start_stocsy))
})
cat(paste0("PASS actual Shiny STOCSY observers: vertical framing, preserved ",
           "brush/scaling, reset, correlations, clicks, hover and invalidation\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.