Skip to content

Commit

Permalink
Minor edits
Browse files Browse the repository at this point in the history
Handle edge case of idfactor = "" gracefully.
Edit docs and vignette.
  • Loading branch information
aphalo committed Dec 23, 2024
1 parent d75ccf1 commit b8eaae8
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 238 deletions.
17 changes: 9 additions & 8 deletions R/autoplot-source-spct.r
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,10 @@ q_plot <- function(spct,
#' @param chroma.type character one of \code{"CMF"} (color matching function) or
#' \code{"CC"} (color coordinates) or a
#' \code{\link[photobiology]{chroma_spct}} object.
#' @param idfactor character Name of an index column in data holding a
#' \code{factor} with each spectrum in a long-form multispectrum object
#' corresponding to a distinct level of the factor.
#' @param idfactor character Name of an index \code{factor} used to identify
#' each spectrum when multiple spectra are included in a plot. If different
#' from an existing one, renaming takes place. It is used as title for the
#' plot guide and may include embedded spaces and new lines.
#' @param facets logical or integer Indicating if facets are to be created for
#' the levels of \code{idfactor} when \code{spct} contain multiple spectra in
#' long form.
Expand All @@ -662,11 +663,11 @@ q_plot <- function(spct,
#' different classes for storage of spectral data defined in package
#' \code{\link[photobiology]{photobiology}}.
#'
#' @note If \code{idfactor = NULL}, the default for single spectra, the name of
#' the factor is retrieved from metadata or if no metadata found, the default
#' \code{"spct.idx"} is tried. The default for multiple spectra is to create a
#' factor named \code{"spct.idx"}, but if a different name is passed, it will
#' be used instead, possibly renaming a pre-existing one.
#' @note If \code{idfactor = NULL}, the default, the name of the factor is
#' retrieved from metadata or if no metadata found, the default
#' \code{"spct.idx"} is tried. The default for collections of spectra is to
#' create a factor named \code{"spct.idx"}, but if a different name is passed,
#' it will be used instead, possibly renaming a pre-existing one.
#'
#' @return A \code{ggplot} object with a number of layers that depends on the
#' data and annotations.
Expand Down
13 changes: 9 additions & 4 deletions R/utils-internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ check_idfactor_arg <- function(object, idfactor = NULL, default = FALSE) {
idfactor <- idfactor[1]
}
if (is.null(idfactor) || is.na(idfactor)) {
idfactor <- photobiology::getIdFactor(object)
idfactor <- photobiology::getIdFactor(object) # may return NA!
}
if (is.na(idfactor) || !is.character(idfactor)) {
idfactor <- default || photobiology::getMultipleWl(object) > 1L
Expand All @@ -24,21 +24,26 @@ check_idfactor_arg <- function(object, idfactor = NULL, default = FALSE) {

#' Update idfactor name in object
#'
#' Conditionally call \code{\link[photobiology]{setIdFactor}}.
#'
#' @param object generic_spct object.
#' @param idfactor character The name of the factor identifying spectra when
#' stored in long form.
#'
#' @note There is no check for \code{NA} or \code{NULL} as we assume that
#' \code{\link{check_idfactor_arg}} has already been called on \code{idfactor}
#' to check its sanity.
#'
#' @keywords internal
#'
rename_idfactor <- function(object, idfactor) {
if (is.character(idfactor) &&
if (is.character(idfactor) && nzchar(idfactor) &&
photobiology::getIdFactor(object) != idfactor) {
object <- photobiology::setIdFactor(object, idfactor = idfactor)
}
object
}


#' Convert lubridate duration objects to a string if possible
#'
#' @param time.unit lubridate::duration object or character
Expand All @@ -62,7 +67,7 @@ duration2character <- function(time.unit) {
#' @keywords internal
#'
warn_norm_arg <- function(norm) {
if (!is.na(norm)) {
if (is.null(norm) || !is.na(norm)) {
warning("On-the-fly normalization no longer supported. Use 'normalize()' instead.")
}
}
Expand Down
17 changes: 9 additions & 8 deletions man/autoplot.calibration_spct.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions man/autoplot.cps_spct.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/autoplot.filter_spct.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions man/autoplot.generic_spct.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/autoplot.object_spct.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions man/autoplot.raw_spct.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions man/autoplot.reflector_spct.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions man/autoplot.response_spct.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions man/autoplot.source_spct.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/rename_idfactor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions vignettes/userguide2-autoplot-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ autoplot(sun_evening.mspct, facets = 2)

## -----------------------------------------------------------------------------
p1 <- autoplot(sun.spct)
summary(p1)
summary(p1$data)

## -----------------------------------------------------------------------------
Expand Down Expand Up @@ -152,7 +151,7 @@ autoplot(sun_evening.spct, facets = 3) +
## -----------------------------------------------------------------------------
filter_no_yes.spct <-
rbindspct(list(sun = sun.spct, filtered = yellow_gel.spct * sun.spct),
idfactor = "Source")
idfactor = "Light source")
autoplot(filter_no_yes.spct)

## -----------------------------------------------------------------------------
Expand Down
7 changes: 3 additions & 4 deletions vignettes/userguide2-autoplot-methods.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,10 @@ Contrary to `"gg"` objects created with package 'ggplot2', the data embedded in

```{r}
p1 <- autoplot(sun.spct)
summary(p1)
summary(p1$data)
```

This makes it possible to trace the origin of the data, and also to apply especialised methods to them.
This makes it possible to trace the origin of the data, and also to apply specialised methods to them.

---

Expand Down Expand Up @@ -202,7 +201,7 @@ The effect of `range` is slightly different to the effect of `ggplot2::xlim()` a

---

The default argument to `parameter `w.band` can be changed by setting
The default argument to parameter `w.band` can be changed by setting
the R option `"photobiology.plot.bands"` to a single waveband object or to a list waveband
objects.

Expand Down Expand Up @@ -407,7 +406,7 @@ It is possible to construct and bind the spectra on-the-fly, and to use arbitrar
```{r}
filter_no_yes.spct <-
rbindspct(list(sun = sun.spct, filtered = yellow_gel.spct * sun.spct),
idfactor = "Source")
idfactor = "Light source")
autoplot(filter_no_yes.spct)
```

Expand Down
Loading

0 comments on commit b8eaae8

Please sign in to comment.