Running examples in ‘speaq-Ex.R’ failed
The error most likely occurred in:

> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: AddPlottingStuff
> ### Title: Add plotting variables
> ### Aliases: AddPlottingStuff
> 
> ### ** Examples
> 
> subset <- GetWinedata.subset()
> subset.spectra = as.matrix(subset$Spectra)
> subset.ppm = as.numeric(subset$PPM)
> 
> test.peaks <- getWaveletPeaks(Y.spec=subset.spectra, 
+                               X.ppm=subset.ppm,
+                               nCPU = 1) # nCPU set to 2 for the vignette build
 ----------- FAILURE REPORT -------------- 
 --- failure: the condition has length > 1 ---
 --- srcref --- 
: 
 --- package (from environment) --- 
speaq
 --- call from context --- 
getWaveletPeaks(Y.spec = subset.spectra, X.ppm = subset.ppm, 
    nCPU = 1)
 --- call from argument --- 
if (class(Y.spec) != "matrix") {
    print("the raw spectra, Y.spec, are not in matrix format, attempting conversion")
    warning("the raw spectra, Y.spec, are not in matrix format, conversion attempted")
    if ("numeric" %in% class(Y.spec)) {
        Y.spec <- matrix(Y.spec, nrow = 1, ncol = length(Y.spec))
    }
    else {
        Y.spec <- as.data.frame(Y.spec)
        Y.spec <- as.matrix(Y.spec)
    }
}
 --- R stacktrace ---
where 1: getWaveletPeaks(Y.spec = subset.spectra, X.ppm = subset.ppm, 
    nCPU = 1)

 --- value of length: 2 type: logical ---
[1] FALSE  TRUE
 --- function from context --- 
function (Y.spec, X.ppm, sample.labels = NULL, window.width = "small", 
    window.split = 4, scales = seq(1, 16, 1), baselineThresh = 1000, 
    SNR.Th = -1, nCPU = -1, include_nearbyPeaks = TRUE, raw_peakheight = FALSE, 
    duplicate_detection_multiplier = 1) 
{
    for (w in seq_along(window.split)) {
        if (!window.split[w] %in% c(2, 4, 16, 32, 64)) {
            warning(paste(" 'window.split value", as.character(window.split[w]), 
                "is not a power of 2 (max 64). It is set to the default: 4", 
                sep = " "))
        }
    }
    if (nCPU == -1) {
        nCPU <- parallel::detectCores(all.tests = FALSE, logical = TRUE) - 
            1
    }
    if ("small" %in% window.width & !"large" %in% window.width | 
        missing(window.width)) {
        FFTwindow <- 512
    }
    else if ("large" %in% window.width & !"small" %in% window.width) {
        FFTwindow <- 1024
    }
    else {
        warning("'window.width' is defined ambiguously or wrong, set to default small.")
        FFTwindow <- 512
    }
    if (class(Y.spec) != "matrix") {
        print("the raw spectra, Y.spec, are not in matrix format, attempting conversion")
        warning("the raw spectra, Y.spec, are not in matrix format, conversion attempted")
        if ("numeric" %in% class(Y.spec)) {
            Y.spec <- matrix(Y.spec, nrow = 1, ncol = length(Y.spec))
        }
        else {
            Y.spec <- as.data.frame(Y.spec)
            Y.spec <- as.matrix(Y.spec)
        }
    }
    nPPM <- ncol(Y.spec)
    nSamp <- nrow(Y.spec)
    if (nCPU > nSamp) {
        nCPU <- nSamp
    }
    if ("data.frame" %in% class(X.ppm)) {
        print("X.ppm is a data frame, attempting conversion.")
        warning("X.ppm was in data frame format. Conversion to numeric vector or matrix attempted")
        if (1 %in% dim(X.ppm)) {
            X.ppm = as.numeric(X.ppm)
        }
        else if (nPPM %in% dim(X.ppm) & nSamp %in% dim(X.ppm)) {
            X.ppm = as.matrix(X.ppm)
            if (ncol(X.ppm) == nSamp) {
                X.ppm = t(X.ppm)
                warning("PPM matrix was transposed to have the samples in the matrix rows.")
            }
        }
    }
    if (is.numeric(X.ppm)) {
        if (length(X.ppm) == nPPM) {
            X.ppm.matrix <- matrix(rep(X.ppm, nSamp), ncol = nPPM, 
                nrow = nSamp, byrow = TRUE)
        }
        else {
            stop("the length of the ppm vector, X.ppm, does not match with the amount of columns in the data matrix, Y.ppm.")
        }
    }
    else if (is.matrix(X.ppm)) {
        if (ncol(X.ppm) != nPPM | nrow(X.ppm) != nSamp) {
            stop("X.ppm is a matrix but the dimensions do not match with the Y.ppm matrix")
        }
        X.ppm.matrix <- X.ppm
    }
    else {
        stop("X.ppm is not a numeric nor a matrix. If X.ppm is a data frame: convert it to a matrix or a numeric vector")
    }
    if (any(is.na(X.ppm))) {
        warning("X.ppm contains NA's, trying to remove these.")
        Y.spec <- Y.spec[, !is.na(X.ppm)]
        X.ppm <- X.ppm[!is.na(X.ppm)]
    }
    if (any(is.na(Y.spec))) {
        stop("Y.spec contains NA's. Don't know how to deal with these.")
    }
    if (is.null(sample.labels)) {
        sample.labels <- as.numeric(seq(from = 1, to = nSamp))
    }
    else if (nSamp != length(sample.labels)) {
        warning("Sample labels do not match amount of rows in Y matrix, default row numbers will be used as sample labels")
        sample.labels <- seq(from = 1, to = nSamp)
    }
    if (!"numeric" %in% class(sample.labels)) {
        warning("sample.labels is not numeric. Attempting conversion to numeric for internal purposes.")
        if (!"factor" %in% class(sample.labels)) {
            sample.labels = as.factor(sample.labels)
        }
        original.levels = levels(sample.labels)
        levels(sample.labels) <- seq(1, length(levels(sample.labels)))
        new.levels = levels(sample.labels)
        sample.labels = as.numeric(as.character(sample.labels))
        SampleLabel_Reconversion = TRUE
    }
    else {
        SampleLabel_Reconversion = FALSE
    }
    noiseEsp <- 0.005
    if (SNR.Th < 0) 
        SNR.Th <- max(scales) * 0.05
    print("detecting peaks")
    cl <- parallel::makeCluster(nCPU)
    doSNOW::registerDoSNOW(cl)
    peakList <- list()
    Parcounter <- NULL
    pb <- txtProgressBar(max = nSamp, style = 3)
    progress <- function(n) setTxtProgressBar(pb, n)
    opts <- list(progress = progress)
    peakList <- foreach::foreach(Parcounter = 1:nSamp, .options.snow = opts, 
        .inorder = TRUE, .packages = c("data.table", "MassSpecWavelet")) %dopar% 
        {
            teller <- 0
            WavPeaks <- list()
            currentSpec <- as.numeric(Y.spec[Parcounter, ])
            ppm_vector <- X.ppm.matrix[Parcounter, ]
            for (kw in seq_along(window.split)) {
                window.increment <- FFTwindow/window.split[kw]
                nshifts <- ceiling(nPPM/window.increment) - window.split + 
                  1
                if (sign(nshifts) != -1) {
                  for (j in 1:nshifts) {
                    startR <- (j - 1) * window.increment + 1
                    if (startR >= nPPM) 
                      next
                    endR <- (j - 1) * window.increment + FFTwindow
                    if (endR > nPPM) {
                      endR <- nPPM
                      startR <- nPPM - FFTwindow + 1
                    }
                    subSpec <- currentSpec[startR:endR]
                    subMean <- mean(subSpec)
                    subMedian <- stats::median(subSpec)
                    subMax <- max(subSpec)
                    subLeft <- 0
                    if (length(subSpec) < FFTwindow) {
                      subSpec.extended <- rep(0, FFTwindow)
                      subLeft <- floor((FFTwindow - (endR - startR + 
                        1))/2)
                      subRight <- ceiling((FFTwindow - (endR - 
                        startR + 1))/2)
                      subSpec.extended[(subLeft + 1):(FFTwindow - 
                        subRight)] <- subSpec
                      subSpec <- subSpec.extended
                    }
                    if ((subMean == subMedian) || abs(subMean - 
                      subMedian)/((subMean + subMedian) * 2) < 
                      noiseEsp || subMax < baselineThresh) {
                      next
                    }
                    else {
                      Wavelet.Peaks <- -1
                      try({
                        peakInfo <- MassSpecWavelet::peakDetectionCWT(subSpec, 
                          scales = scales, SNR.Th = SNR.Th, nearbyPeak = include_nearbyPeaks)
                        majorPeakInfo <- peakInfo$majorPeakInfo
                        betterPeakInfo <- MassSpecWavelet::tuneInPeakInfo(subSpec, 
                          majorPeakInfo)
                        betterPeakInfo$sample <- sample.labels[Parcounter]
                        peakIndex <- as.matrix(betterPeakInfo$peakIndex + 
                          startR - 1 - subLeft, ncol = 1)
                        peakPPM <- as.matrix(ppm_vector[peakIndex], 
                          ncol = 1)
                        peakSNR <- as.matrix(betterPeakInfo$peakSNR, 
                          ncol = 1)
                        peakValue <- as.matrix(betterPeakInfo$peakValue, 
                          ncol = 1)
                        peakScale <- as.matrix(betterPeakInfo$peakScale, 
                          ncol = 1)
                        Sample <- betterPeakInfo$sample
                        Wavelet.Peaks <- data.frame(peakIndex, 
                          peakPPM, peakValue, peakSNR, peakScale, 
                          Sample)
                      }, silent = TRUE)
                      oldWarningLevel <- getOption("warn")
                      options(warn = -1)
                      if (head(Wavelet.Peaks, n = 1)[1] != -1) {
                        teller <- teller + 1
                        WavPeaks[[teller]] <- Wavelet.Peaks
                      }
                      options(warn = oldWarningLevel)
                    }
                  }
                }
                else {
                  startR = 1
                  endR = length(currentSpec)
                  subSpec <- currentSpec
                  subMean <- mean(subSpec)
                  subMedian <- stats::median(subSpec)
                  subMax <- max(subSpec)
                  subLeft <- 0
                  if (length(subSpec) < FFTwindow) {
                    subSpec.extended <- rep(0, FFTwindow)
                    subLeft <- floor((FFTwindow - (endR - startR + 
                      1))/2)
                    subRight <- ceiling((FFTwindow - (endR - 
                      startR + 1))/2)
                    subSpec.extended[(subLeft + 1):(FFTwindow - 
                      subRight)] <- subSpec
                    subSpec <- subSpec.extended
                  }
                  if ((subMean == subMedian) || abs(subMean - 
                    subMedian)/((subMean + subMedian) * 2) < 
                    noiseEsp || subMax < baselineThresh) {
                    next
                  }
                  else {
                    Wavelet.Peaks <- -1
                    try({
                      peakInfo <- MassSpecWavelet::peakDetectionCWT(subSpec, 
                        scales = scales, SNR.Th = SNR.Th, nearbyPeak = include_nearbyPeaks)
                      majorPeakInfo <- peakInfo$majorPeakInfo
                      betterPeakInfo <- MassSpecWavelet::tuneInPeakInfo(subSpec, 
                        majorPeakInfo)
                      betterPeakInfo$sample <- sample.labels[Parcounter]
                      peakIndex <- as.matrix(betterPeakInfo$peakIndex + 
                        startR - 1 - subLeft, ncol = 1)
                      peakPPM <- as.matrix(ppm_vector[peakIndex], 
                        ncol = 1)
                      peakSNR <- as.matrix(betterPeakInfo$peakSNR, 
                        ncol = 1)
                      peakValue <- as.matrix(betterPeakInfo$peakValue, 
                        ncol = 1)
                      peakScale <- as.matrix(betterPeakInfo$peakScale, 
                        ncol = 1)
                      Sample <- betterPeakInfo$sample
                      Wavelet.Peaks <- data.frame(peakIndex, 
                        peakPPM, peakValue, peakSNR, peakScale, 
                        Sample)
                    }, silent = TRUE)
                    oldWarningLevel <- getOption("warn")
                    options(warn = -1)
                    if (head(Wavelet.Peaks, n = 1)[1] != -1) {
                      teller <- teller + 1
                      WavPeaks[[teller]] <- Wavelet.Peaks
                    }
                    options(warn = oldWarningLevel)
                  }
                }
            }
            WavPeaks <- data.table::rbindlist(WavPeaks)
            WavPeaks <- WavPeaks[!duplicated(WavPeaks$peakIndex), 
                ]
            WavPeaks <- WavPeaks[WavPeaks$peakValue > baselineThresh, 
                ]
            return(WavPeaks)
        }
    close(pb)
    parallel::stopCluster(cl)
    WaveletPeaks <- data.table::rbindlist(peakList)
    if (nrow(WaveletPeaks) != 0) {
        print("fixing duplicate detections")
        window.increment <- FFTwindow/min(window.split)
        nshifts <- ceiling(nPPM/window.increment) - window.split + 
            1
        if (sign(nshifts) != -1) {
            startR <- rep(NA, nshifts)
            endR <- rep(NA, nshifts)
            for (j in 1:nshifts) {
                startR[j] <- (j - 1) * window.increment + 1
                endR[j] <- (j - 1) * window.increment + FFTwindow
                if (startR[j] >= nPPM) {
                  startR[j] <- NA
                  endR[j] <- NA
                }
            }
            startR <- startR[complete.cases(startR)]
            endR <- endR[complete.cases(startR)]
            cl <- parallel::makeCluster(nCPU)
            doSNOW::registerDoSNOW(cl)
            to.delete <- list()
            Parcounter <- NULL
            pb <- txtProgressBar(max = (nshifts - 1), style = 3)
            progress <- function(n) setTxtProgressBar(pb, n)
            opts <- list(progress = progress)
            to.delete <- foreach::foreach(Parcounter = 1:(nshifts - 
                1), .options.snow = opts, .inorder = TRUE, .packages = c("cluster")) %dopar% 
                {
                  check.dat <- WaveletPeaks[WaveletPeaks$peakIndex >= 
                    (startR[Parcounter]) & WaveletPeaks$peakIndex <= 
                    endR[Parcounter + 1], ]
                  if (nrow(check.dat) != 0) {
                    check.dist <- cluster::daisy(matrix(check.dat$peakPPM, 
                      ncol = 1), metric = "euclid", stand = FALSE)
                    check.distM <- as.matrix(check.dist)
                    check.distM[lower.tri(check.distM, diag = T)] <- 100
                    indices = which(check.distM <= 0.005 * duplicate_detection_multiplier, 
                      arr.ind = TRUE)
                    distance.data <- matrix(NA, nrow = nrow(indices), 
                      ncol = 9)
                    colnames(distance.data) <- c("distance", 
                      "sample1", "sample2", "peakIndex1", "peakIndex2", 
                      "ppm.diff", "peakValue.diff.abs", "peakValue1", 
                      "peakValue2")
                    distance.data[, 1] <- check.distM[indices]
                    distance.data[, 2] <- check.dat$Sample[indices[, 
                      1]]
                    distance.data[, 3] <- check.dat$Sample[indices[, 
                      2]]
                    distance.data[, 4] <- check.dat$peakIndex[indices[, 
                      1]]
                    distance.data[, 5] <- check.dat$peakIndex[indices[, 
                      2]]
                    distance.data[, 6] <- abs(check.dat$peakPPM[indices[, 
                      2]] - check.dat$peakPPM[indices[, 1]])
                    distance.data[, 7] <- check.dat$peakValue[indices[, 
                      2]] - check.dat$peakValue[indices[, 1]]
                    distance.data[, 8] <- check.dat$peakValue[indices[, 
                      1]]
                    distance.data[, 9] <- check.dat$peakValue[indices[, 
                      2]]
                    distance.data[, 7] <- distance.data[, 7, 
                      drop = FALSE]/apply(distance.data[, 8:9, 
                      drop = FALSE], MARGIN = 1, max)
                    distance.data <- distance.data[, 1:7, drop = FALSE]
                    colnames(distance.data) <- c("distance", 
                      "sample1", "sample2", "peakIndex1", "peakIndex2", 
                      "ppm.diff", "peakValue.diff.prop")
                    distance.data <- distance.data[order(distance.data[, 
                      1]), , drop = FALSE]
                    distance.data <- distance.data[distance.data[, 
                      2] == distance.data[, 3], , drop = FALSE]
                    left.largest <- which(sign(distance.data[, 
                      7]) == -1)
                    distance.data[left.largest, c(4, 5)] <- distance.data[left.largest, 
                      c(5, 4)]
                    distance.data[left.largest, 7] <- abs(distance.data[left.largest, 
                      7])
                    to.delete <- distance.data[, c(3, 4), drop = FALSE]
                  }
                  else {
                    distance.data <- matrix(NA, nrow = 0, ncol = 7)
                    colnames(distance.data) <- c("distance", 
                      "sample1", "sample2", "peakIndex1", "peakIndex2", 
                      "ppm.diff", "peakValue.diff.prop")
                    to.delete <- distance.data[, c(3, 4), drop = FALSE]
                  }
                  return(to.delete)
                }
            close(pb)
            parallel::stopCluster(cl)
            to.delete <- do.call(rbind, to.delete)
            to.delete <- unique(to.delete)
            for (sn in 1:nSamp) {
                if (nrow(to.delete[, 1, drop = FALSE]) != 0) {
                  curr.samp.to.delete <- to.delete[to.delete[, 
                    1, drop = FALSE] == sn, 2]
                  WaveletPeaks$peakIndex[WaveletPeaks$Sample == 
                    sn & WaveletPeaks$peakIndex %in% curr.samp.to.delete] <- NA
                }
            }
        }
        WaveletPeaks <- WaveletPeaks[!is.na(WaveletPeaks$peakIndex), 
            , drop = FALSE]
        WaveletPeaks <- data.frame(WaveletPeaks)
        if (raw_peakheight) {
            WaveletPeaks$peakValue = Y.spec[as.matrix(WaveletPeaks[, 
                c("Sample", "peakIndex")])]
            message("Note that when using raw peakheights a baseline removal procedure can/should be used.")
        }
    }
    else {
        print("No peaks detected")
    }
    if (SampleLabel_Reconversion) {
        WaveletPeaks$Sample <- as.factor(WaveletPeaks$Sample)
        if (all(levels(WaveletPeaks$Sample) == new.levels)) {
            levels(WaveletPeaks$Sample) <- original.levels
        }
        else {
            for (lv in seq_along(levels(WaveletPeaks$Sample))) {
                levelMatch <- which(new.levels == levels(WaveletPeaks$Sample)[lv])
                levels(WaveletPeaks$Sample)[lv] <- original.levels[levelMatch]
            }
        }
    }
    return(WaveletPeaks)
}
<bytecode: 0x55ac66343838>
<environment: namespace:speaq>
 --- function search by body ---
Function getWaveletPeaks in namespace speaq has this body.
 ----------- END OF FAILURE REPORT -------------- 
Fatal error: the condition has length > 1