R/DEPRECATED-wl_sort.R

Defines functions orderwl

#' @name DEPRECATED-orderwl
#' @concept deprecated
#'
#' @title (DEPRECATED)
#'        Sorting the wavelengths of a `hyperSpec` object
#'
#' @description
#' These \pkg{hyperSpec} functions are **deprecated** and not maintained any
#' more. You should not use these.
#' Currently they are present due to back-compatibility reasons and will be
#' removed in the next release of the package.
#' Please, use the suggested alternative functions instead.
#'
#' `_____________`
#'
#' Rearranges the `hyperSpec` object so that the wavelength vector is in
#' increasing (or decreasing) order.
#'
#' The wavelength vector is sorted and the columns of the spectra matrix are
#' rearranged accordingly.
#'
#' @param x The `hyperSpec` object.
#' @param na.last,decreasing Handed to [base::order()].
#' @return A `hyperSpec` object.
#' @author C. Beleites
#' @export
#'
#' @seealso [base::sort()]
#' @examples
#'
#' ## Example 1: different drawing order in plot_spc
#' spc <- new("hyperSpec", spc = matrix(rnorm(5) + 1:5, ncol = 5))
#' spc <- cbind(spc, spc + .5)
#'
#' plot_spc(spc)
#' text(wl(spc), spc[[]], as.character(1:10), col = "darkred")
#'
#' spc_sorted <- orderwl(spc)
#' plot_spc(spc_sorted)
#' text(wl(spc_sorted), spc_sorted[[]], as.character(1:10), col = "darkred")
#'
#' ## Example 2
#' spc <- new("hyperSpec", spc = matrix(rnorm(5) * 2 + 1:5, ncol = 5))
#' spc <- cbind(spc, spc)
#'
#' plot(seq_len(nwl(spc)), spc[[]], type = "b")
#' spc[[]]
#'
#' spc_sorted <- orderwl(spc)
#' lines(seq_len(nwl(spc_sorted)), spc_sorted[[]], type = "l", col = "red")
#' spc_sorted[[]]
orderwl <- function(x, na.last = TRUE, decreasing = FALSE) {
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  hySpc_deprecated("wl_sort")
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  assert_hyperSpec(x)
  validObject(x)

  ord <- order(x@wavelength, na.last = na.last, decreasing = decreasing)

  if (any(ord != seq_along(x@wavelength))) {
    x@data$spc <- x@data$spc[, ord, drop = FALSE]
    .wl(x) <- x@wavelength[ord]
  }

  x
}
r-hyperspec/hyperSpec documentation built on May 31, 2024, 5:53 p.m.