Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate default values inside methods of as.hyperSpec() #298

Merged
merged 3 commits into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion hyperSpec/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
* Column names in spectra matrix (`$spc` column of `hyperSpec` object) are now returned correctly by functions `spc.bin()` (#237), and `spc.loess()` (#245).
* New function `hy_list_available_hySpc_packages()` lists packages, that are available in GitHub organization `r-hyperSpec`.
* New function `hy_browse_homepage()` opens the homepage of *R hyperSpec* in a web browser.
* New method `as.hyperSpec(<hyperSpec>)` (#282).
* Changes related to function `as.hyperSpec()`:
- New method `as.hyperSpec(<hyperSpec>)` was created (#282).
- The default value of argument `wl` is now set to `wl = NULL` (#297).
- `wl = NULL` now means that the default values of wavelengths should be calculated inside the methods of `as.hyperSpec()` (#297).
* Possibility to initialize `hyperSpec` object by providing wavelengths only (#288).
* Function `wl.eval()` is converted into S3 generic. Methods `wl.eval(<hyperSpec>)` and `wl.eval(<numeric>)` for numeric vectors were added (#287).
* New function `new_hyperSpec()` that initializes `hyperSpec` object in a similar way as `new("hyperSpec")` does but has autocompletion possibilities in RStudio (#283).
Expand Down
37 changes: 21 additions & 16 deletions hyperSpec/R/as_hyperSpec.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@

#' `as.hyperSpec`: Convenience Conversion Functions
#'
#' These functions are shortcuts to convert other objects into hypeSpec objects.
#' These functions are shortcuts to convert other objects into `hypeSpec`
#' objects.
#'
#' @param X the object to convert. If `X` is:
#'
#' - a `matrix`, it is assumed to contain the spectra matrix,
#' - a `data.frame`, it is assumed to contain extra data.
#'
#' @param X the object to convert.
#' A matrix is assumed to contain the spectra matrix,
#' a data.frame is assumed to contain extra data.
#' @param ... additional parameters that should be handed over to
#' `new("hyperSpec")` (initialize)
#' `new("hyperSpec")` (initialize).
#'
#' @return hyperSpec object
#' @seealso [hyperSpec::initialize()]
Expand All @@ -23,14 +26,15 @@ setGeneric(
)

#' @include guess_wavelength.R
.as.hyperSpec.matrix <- function(X, wl = guess.wavelength(colnames(X)), ...) {
.as.hyperSpec.matrix <- function(X, wl = NULL, ...) {
if (is.null(wl)) wl <- guess.wavelength(colnames(X))
new("hyperSpec", spc = X, wavelength = wl, ...)
}

#' @rdname as.hyperSpec
#' @param wl wavelength vector. Defaults to guessing from the column names in `X`
#' @param spc spectra matrix
#' @param labels list with labels
#' @param wl wavelength vector. Defaults to guessing from the column names in `X`.
#' @param spc spectra matrix.
#' @param labels list with labels.
#' @export
#'
#' @concept hyperSpec conversion
Expand All @@ -41,12 +45,14 @@ setGeneric(
#' guess.wavelength(wl)
setMethod("as.hyperSpec", "matrix", .as.hyperSpec.matrix)

.as.hyperSpec.data.frame <- function(X, spc = NULL, wl = guess.wavelength(spc),
.as.hyperSpec.data.frame <- function(X, spc = NULL, wl = NULL,
labels = attr(X, "labels"), ...) {

if (is.null(wl)) wl <- guess.wavelength(X)
# TODO: remove after 31.12.2020
if (!all(!is.na(guess.wavelength(colnames(X))))) {
warning(
"as.hyperSpec.data.frame has changed its behaviour. ",
"Method as.hyperSpec(<data.frame>) has changed its behaviour. ",
"Use as.hyperSpec(as.matrix(X)) instead."
)
}
Expand All @@ -60,11 +66,10 @@ setMethod("as.hyperSpec", "matrix", .as.hyperSpec.matrix)
}

#' @rdname as.hyperSpec
#' @note *Note that the behaviour of `as.hyperSpec(X)` was changed: it now
#' assumes `X` to be extra data, and returns a hyperSpec object with 0
#' wavelengths. To get the old behaviour*

# FIXME: it seems that the documentation sentence in incomplete.
#' @note Note that the behaviour of `as.hyperSpec(X)` was changed when `X` is a
#' `data.frame`: it now assumes `X` to be extra data, and returns a `hyperSpec`
#' object with 0 wavelengths. To get the old behaviour, use
#' `as.hyperSpec(as.matrix(X))`.

setMethod("as.hyperSpec", "data.frame", .as.hyperSpec.data.frame)

Expand Down