Nothing
library(shiny)
library(iSTATS)
app_file <- function(name) {
path <- system.file("app", name, package = "iSTATS")
stopifnot(nzchar(path))
path
}
declaration <- parse(app_file("server.R"))[[1L]]
declared <- eval(declaration[[3L]], envir = baseenv())
set.seed(11)
axis <- seq(10, 0, length.out = 101)
spectra <- matrix(runif(6 * 101), 6, 101)
samples <- paste0("s", 1:6)
# Same layout as the app: session-owned bindings, code evaluated in a child.
server <- function(input, output, session) {
for (name in declared) assign(name, NULL)
NMRData <- NMRData_plot <- spectra
CS_values_real <- rbind(axis, axis)
file_names <- samples
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)
stop_menssager <- 0
.stocsy_i_show_all <- .stocsy_is_show_all <- .stocsy_rt_show_all <-
function() invisible(NULL)
code <- new.env(parent = environment())
sys.source(app_file("functions.R"), envir = code)
sys.source(app_file("select_signals_server.R"), envir = code)
}
cor_pearson_of <- function(code) get("cor_pearson", code)
read_download <- function(path) read.csv(path, check.names = FALSE)
brush <- function(xmin, xmax) list(xmin = xmin, xmax = xmax, ymin = 0, ymax = 1)
cols_a <- 71:81 # 3.0 .. 2.0 ppm
cols_b <- 36:41 # 6.5 .. 6.0 ppm
testServer(server, {
# testServer evaluates this block in a copy; read the live session state.
state <- parent.env(code)
check <- function(...) eval(substitute(stopifnot(...)), state)
# A single region is enough for buckets and STOCSY.
session$setInputs(sel_brush = brush(2, 3), sel_cor = 1)
check(sel_ind == 1, identical(dim(matr_selec), c(6L, 1L)),
identical(col_select, cols_a))
one <- read_download(output$downloadareas)
stopifnot(identical(names(one), c("Sample", "2-3")),
isTRUE(all.equal(one[["2-3"]], rowSums(spectra[, cols_a]))))
session$setInputs(s_stocsy = 1)
check(length(cor_pearson_of(code)) == length(cols_a),
length(facts$fac_stocsy_i) == ncol(spectra))
# A second region; markers are not duplicated for STOCSY/Normalize.
session$setInputs(sel_brush = brush(6, 6.5), sel_cor = 2)
check(sel_ind == 2, ncol(matr_selec) == 2L,
length(CS_selection$vranges) == length(col_select))
two <- read_download(output$downloadareas)
stopifnot(identical(names(two), c("Sample", "2-3", "6-6.5")))
# An overlapping region is refused without touching the state.
session$setInputs(sel_brush = brush(2.5, 2.8), sel_cor = 3)
check(sel_ind == 2, ncol(matr_selec) == 2L)
# Deleting after STOCSY sorted the points removes the right region.
session$setInputs(s_stocsy = 2)
session$setInputs(exc_reg = 1)
session$setInputs(sel_click = list(x = 6.2))
check(sel_ind == 1, alr_click == 0, identical(col_select, cols_a),
isTRUE(all.equal(matr_selec[, 1], rowSums(spectra[, cols_a]))),
length(cor_pearson_of(code)) == 0L)
left <- read_download(output$downloadareas)
stopifnot(identical(names(left), c("Sample", "2-3")))
# A click outside every region leaves the selection intact.
session$setInputs(exc_reg = 2)
session$setInputs(sel_click = list(x = 8))
check(sel_ind == 1, alr_click == 0)
# Deleting the last region resets the selection; a new one starts over.
session$setInputs(exc_reg = 3)
session$setInputs(sel_click = list(x = 2.5))
check(sel_ind == 0, length(col_select) == 0L)
session$setInputs(sel_brush = brush(6, 6.5), sel_cor = 4)
check(sel_ind == 1, identical(col_select, cols_b),
identical(dim(matr_selec), c(6L, 1L)))
})
cat("PASS Select Signals: single region download/STOCSY, add, overlap, delete\n")
# Suggested regions: the Alignment detector proposes regions on the current
# matrix; edited text drives the blue outlines and loading adds them like the
# save button, skipping overlaps. Both panels draw in both display modes.
peak_axis <- seq(10, 0, length.out = 4001)
peak <- function(ppm, width = 0.01) exp(-((peak_axis - ppm) / width)^2)
set.seed(5)
peak_spectra <- t(sapply(1:6, function(i)
1000 * (peak(2 + i * 1e-4) + 0.6 * peak(5) + 0.8 * peak(7.5)) +
rnorm(length(peak_axis), sd = 1)))
spectra <- peak_spectra
axis <- peak_axis
testServer(server, {
state <- parent.env(code)
check <- function(...) eval(substitute(stopifnot(...)), state)
selection_suggested <- function() code$selection_suggested()
session$setInputs(radio_s = 1)
stopifnot(nzchar(output$plot1$src), nzchar(output$plot2$src))
session$setInputs(select_suggest = 1)
suggested <- selection_suggested()
stopifnot(is.matrix(suggested), nrow(suggested) >= 3L,
nzchar(output$plot1$src))
inside <- function(ppm) any(pmin(suggested[, 1], suggested[, 2]) <= ppm &
pmax(suggested[, 1], suggested[, 2]) >= ppm)
stopifnot(inside(2), inside(5), inside(7.5))
# Editing the text replaces the suggestion; invalid text keeps the last one.
session$setInputs(select_suggested_regions = "7.6 7.4\n5.1, 4.9")
stopifnot(identical(selection_suggested(), rbind(c(7.6, 7.4), c(5.1, 4.9))))
session$setInputs(select_suggested_regions = "7.6")
stopifnot(nrow(selection_suggested()) == 2L,
grepl("Invalid ppm interval", output$select_suggest_status))
session$setInputs(select_suggested_regions = "7.6 7.4\n5.1, 4.9")
# A region already loaded makes the overlapping suggestion skip.
session$setInputs(sel_brush = list(xmin = 4.95, xmax = 5.2, ymin = 0, ymax = 1),
sel_cor = 1)
session$setInputs(select_suggest_load = 1)
state$first <- code$.region_columns(peak_axis, 4.95, 5.2)
state$second <- code$.region_columns(peak_axis, 7.4, 7.6)
check(sel_ind == 2, nrow(reg_selec) == 2L, is.null(rownames(reg_selec)),
identical(reg_selec[2, ], c(7.4, 7.6)), ncol(matr_selec) == 2L,
identical(col_select, c(first, second)),
length(CS_selection$vranges) == length(col_select))
stopifnot(is.null(selection_suggested()))
# Overlap mode draws every sample; discard clears a new suggestion.
session$setInputs(radio_s = 2)
stopifnot(nzchar(output$plot1$src), nzchar(output$plot2$src))
# The intensity buttons also scale the Overlap drawing; Show all undoes it.
before <- output$plot1$src
session$setInputs(x2_sel1 = 1)
session$setInputs(x8_sel1 = 1)
session$setInputs(q2_sel1 = 1)
stopifnot(identical(code$.selection_overlap_data(), peak_spectra*8),
!identical(output$plot1$src, before))
session$setInputs(all_h = 1)
stopifnot(identical(code$.selection_overlap_data(), peak_spectra))
session$setInputs(select_suggest = 2)
stopifnot(!is.null(selection_suggested()))
session$setInputs(select_suggest_discard = 1)
stopifnot(is.null(selection_suggested()))
})
cat("PASS Select Signals: base-graphics panels, Overlap scale buttons, suggested regions edit/load/discard\n")
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.