From 61e6457d14fa73274b6481871dddde98670600d1 Mon Sep 17 00:00:00 2001 From: Suryansh Dey Date: Mon, 6 Apr 2026 17:16:13 +0530 Subject: [PATCH] feat: Creating a hyperSpec object without spectra --- NEWS.md | 1 + R/initialize.R | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index ce22ec5..9b58433 100644 --- a/NEWS.md +++ b/NEWS.md @@ -47,6 +47,7 @@ ### Bugfixes * Possibility to initialize `hyperSpec` object by providing wavelengths only (cbeleites/hyperSpec#288). +* Possibility to initialize `hyperSpec` object providing only `data` without `spc` (cbeleites/hyperSpec#296). * Column names in spectra matrix (`$spc` column of `hyperSpec` object) are now returned correctly by functions `spc.bin()` (cbeleites/hyperSpec#237), and `spc.loess()` (cbeleites/hyperSpec#245 * `all.equal()` method for `hyperSpec` objects converted from S4 to S3 registration. The S4 `setMethod()` approach for this S3 generic caused roxygen2 (>= 7.3) to crash during documentation generation. * `rbind.fill()` (internal): strip the `AsIs` class from matrix columns after data frame assembly. R >= 4.4 introduced `all.equal.AsIs()` which caused spurious class-mismatch failures when comparing `hyperSpec` objects that had been through `rbind` or `collapse`. diff --git a/R/initialize.R b/R/initialize.R index 36b0521..a88bd51 100644 --- a/R/initialize.R +++ b/R/initialize.R @@ -96,6 +96,9 @@ #' spc = spc #' ) #' +#' # data only, no spectra +#' new("hyperSpec", data = data.frame(x = 1:5)) +#' #' colnames(spc) <- 600:603 #' new("hyperSpec", spc = spc) # wavelength taken from colnames (spc) #' @@ -203,7 +206,8 @@ NULL # Deal with spectra if (is.null(spc) && is.null(data$spc)) { - spc <- structure(numeric(0), .Dim = c(0L, 0L)) + nrows <- if (!is.null(data)) nrow(data) else 0L + spc <- structure(numeric(0), .Dim = c(nrows, 0L)) } if (!is.null(spc) && !is.matrix(spc)) { @@ -397,6 +401,12 @@ hySpc.testthat::test(.initialize) <- function() { expect_equal(hy_obj_1c, hy_obj_2c) }) + test_that("data only, no spc", { + h <- new("hyperSpec", data = data.frame(c = 1:5)) + expect_equal(dim(h), c(nrow = 5L, ncol = 2L, nwl = 0L)) + expect_equal(h@wavelength, numeric(0)) + expect_equal(h$c, 1:5) + }) test_that('hyperSpec() and new("hyperSpec") give identical results', { expect_equal(hyperSpec(), new("hyperSpec"))