Skip to content

Commit

Permalink
Redefine classes
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Jul 29, 2024
1 parent 92c7485 commit 574d8d1
Show file tree
Hide file tree
Showing 86 changed files with 274 additions and 785 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Depends:
R (>= 3.5),
dimensio (>= 0.8.0)
Imports:
arkhe (>= 1.6.0),
arkhe (>= 1.7.0),
graphics,
grDevices,
isopleuros (>= 1.2.0),
Expand Down
7 changes: 0 additions & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ exportMethods("%power%")
exportMethods("[")
exportMethods("[<-")
exportMethods("[[<-")
exportMethods("set_groups<-")
exportMethods("set_samples<-")
exportMethods("set_totals<-")
exportMethods(aggregate)
exportMethods(any_assigned)
exportMethods(any_replicated)
exportMethods(as_amounts)
exportMethods(as_composition)
exportMethods(as_features)
Expand All @@ -43,11 +39,8 @@ exportMethods(covariance)
exportMethods(describe)
exportMethods(dist)
exportMethods(get_groups)
exportMethods(get_samples)
exportMethods(get_totals)
exportMethods(hist)
exportMethods(is_assigned)
exportMethods(is_replicated)
exportMethods(mahalanobis)
exportMethods(margin)
exportMethods(mean)
Expand Down
14 changes: 4 additions & 10 deletions R/AllClasses.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ setClassUnion("index", members = c("logical", "numeric", "character"))
#' An S4 class to represent compositional data.
#' @slot totals A [`numeric`] vector to store the absolute row sums (before
#' the closure of the compositions).
#' @slot samples A [`character`] vector to store the sample identifiers
#' (allows duplicates in case of repeated measurements).
#' @slot groups A [`character`] vector to store the group names.
#' @slot extra A [`list`] of extra variables.
#' @section Coerce:
#' In the code snippets below, `x` is a `CompositionMatrix` object.
#' \describe{
Expand All @@ -72,8 +70,7 @@ setClassUnion("index", members = c("logical", "numeric", "character"))
Class = "CompositionMatrix",
slots = c(
totals = "numeric",
samples = "character",
groups = "character"
extra = "list"
),
contains = c("NumericMatrix")
)
Expand All @@ -84,9 +81,7 @@ setClassUnion("index", members = c("logical", "numeric", "character"))
#' S4 classes to represent log-ratio data transformations.
#' @slot totals A [`numeric`] vector to store the absolute row sums (before
#' the closure of the compositions).
#' @slot samples A [`character`] vector to store the sample identifiers
#' (allows duplicates in case of repeated measurements).
#' @slot groups A [`character`] vector to store the group names.
#' @slot extra A [`list`] of extra variables.
#' @slot parts A [`character`] vector to store the part names.
#' @slot ratio A [`character`] vector to store the ratio names.
#' @slot order An [`integer`] vector to store the original ordering of the
Expand All @@ -112,8 +107,7 @@ setClassUnion("index", members = c("logical", "numeric", "character"))
Class = "LogRatio",
slots = c(
totals = "numeric",
samples = "character",
groups = "character",
extra = "list",

parts = "character",
ratio = "character",
Expand Down
107 changes: 24 additions & 83 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,16 @@ NULL
#'
#' Coerces an object to a `CompositionMatrix` object.
#' @param from A [`matrix`] or [`data.frame`] to be coerced.
#' @param samples An [`integer`] giving the index of the column to be used for
#' sample identification: allows duplicates to identify replicated measurements.
#' If `NULL` (the default), row names will be used as sample IDs.
#' @param groups An [`integer`] giving the index of the column to be used to
#' group the samples. If `NULL` (the default), no grouping is stored.
#' @param auto A [`logical`] scalar: try to automatically detect `codes`,
#' `samples` and `groups` columns?
#' @param parts A `vector` giving the index of the column to be used a
#' compositional parts. If `NULL` (the default), all [`double`] columns will be
#' used.
#' @param verbose A [`logical`] scalar: should \R report extra information
#' on progress?
#' @param ... Currently not used.
#' @details
#' The [`CompositionMatrix-class`] class has special slots:
#'
#' * `samples` for [repeated measurements/observation][samples],
#' * `groups` to [group data by site/area][group].
#'
#' When coercing a `data.frame` to a [`CompositionMatrix-class`] object, an
#' attempt is made to automatically assign values to these slots by mapping
#' column names (case insensitive, plural insensitive). This behavior can be
#' disabled by setting `options(nexus.autodetect = FALSE)` or overridden by
#' explicitly specifying the columns to be used.
#'
#' See `vignette("nexus")`.
#' @note
#' All non-numeric variable will be removed.
#' All non-numeric variable will be internally stored.
#' @return A [`CompositionMatrix-class`] object.
#' @example inst/examples/ex-coerce.R
#' @author N. Frerebeau
Expand Down Expand Up @@ -112,7 +97,7 @@ NULL
#' \describe{
#' \item{`%perturbe%`}{[Perturbation operation][perturbation()].}
#' \item{`%power%`}{[Powering operation][powering()].}
#' }
#' }
#' @return
#' A [`CompositionMatrix-class`] object or a [`numeric`] vector (same as `x`).
#' @example inst/examples/ex-arith.R
Expand Down Expand Up @@ -267,62 +252,6 @@ setGeneric(
def = function(x, value) standardGeneric("set_groups<-")
)

#' Working With Samples
#'
#' Retrieves or defines the sample names.
#' @param x An object from which to get or set `samples`.
#' @param value A possible value for the `samples` of `x`.
#' @details
#' In some situations, measurements may have been repeated (e.g. multiple
#' chemical analyses on the same sample). The presence of repeated
#' measurements can be specified by giving several observations the same
#' sample name.
#'
#' See `vignette("nexus")`.
#' @return
#' * `set_samples()` returns an object of the same sort as `x` with the new
#' sample names assigned.
#' * `get_samples()` returns a [`character`] vector giving the sample names of `x`.
#' * `any_replicated()` returns a [`logical`] scalar specifying whether or not
#' `x` has replicated observations.
#' * `is_replicated()` returns a [`logical`] vector specifying whether or not
#' an observation is a replicate.
#' @example inst/examples/ex-mutators.R
#' @author N. Frerebeau
#' @docType methods
#' @family mutators
#' @name samples
#' @rdname samples
NULL

#' @rdname samples
#' @aliases any_replicated-method
setGeneric(
name = "any_replicated",
def = function(x) standardGeneric("any_replicated")
)

#' @rdname samples
#' @aliases is_replicated-method
setGeneric(
name = "is_replicated",
def = function(x) standardGeneric("is_replicated")
)

#' @rdname samples
#' @aliases get_samples-method
setGeneric(
name = "get_samples",
def = function(x) standardGeneric("get_samples")
)

#' @rdname samples
#' @aliases set_samples-method
setGeneric(
name = "set_samples<-",
def = function(x, value) standardGeneric("set_samples<-")
)

#' Row Sums
#'
#' Retrieves or defines the row sums (before closure).
Expand Down Expand Up @@ -662,7 +591,8 @@ setGeneric(
#' returns the result.
#' @param x A [`CompositionMatrix-class`] object.
#' @param by A `vector` or a list of grouping elements, each as long as the
#' variables in `x`. The elements are coerced to factors before use.
#' variables in `x`. The elements are coerced to factors before use
#' (in the sense that [`as.factor(by)`][as.factor()] defines the grouping).
#' @param FUN A [`function`] to compute the summary statistics.
#' @param simplify A [`logical`] scalar: should the results be simplified to a
#' matrix if possible?
Expand Down Expand Up @@ -721,7 +651,10 @@ NULL
#'
#' Splits the data into subsets and computes compositional mean for each.
#' @param x A [`CompositionMatrix-class`] object.
#' @param by A `vector` of grouping elements, as long as the variables in `x`.
#' @param by A `vector` of grouping elements, as long as the variables in `x`
#' (in the sense that [`as.factor(by)`][as.factor()] defines the grouping).
#' If a single `character` string is passed, it must be the name of a
#' categorical variable from the original dataset.
#' @param ... Further arguments to be passed to [mean()].
#' @return A [`CompositionMatrix-class`] object.
#' @seealso [mean()], [aggregate()]
Expand Down Expand Up @@ -979,12 +912,14 @@ NULL
#'
#' Displays a compositional bar chart.
#' @param height A [`CompositionMatrix-class`] object.
#' @param groups A `vector` of grouping elements, as long as the variables in
#' `height`. If a single `character` string is passed, it must be the name of a
#' categorical variable from the original dataset.
#' If set, a matrix of panels defined by `groups` will be drawn.
#' @param order An [`integer`] vector giving the index of the column to be used
#' for the ordering of the data.
#' @param decreasing A [`logical`] scalar: should the sort order be increasing
#' or decreasing?
#' @param groups A `vector` of grouping elements, as long as the variables in
#' `height`. If set, a matrix of panels defined by `groups` will be drawn.
#' @param horiz A [`logical`] scalar. If `FALSE`, the bars are drawn vertically
#' with the first bar to the left. If `TRUE` (the default), the bars are drawn
#' horizontally with the first at the bottom.
Expand Down Expand Up @@ -1051,7 +986,9 @@ NULL
#'
#' Displays a matrix of ternary plots.
#' @param x A [`CompositionMatrix-class`] object.
#' @param groups A `vector` of grouping elements, as long as the variables in `x`.
#' @param groups A `vector` of grouping elements, as long as the variables in
#' `x`. If a single `character` string is passed, it must be the name of a
#' categorical variable from the original dataset.
#' @inheritParams isopleuros::ternary_pairs
#' @return
#' `plot()` is called for its side-effects: is results in a graphic being
Expand All @@ -1070,7 +1007,9 @@ NULL
#'
#' Displays a density plot.
#' @param x A [`LogRatio-class`] object.
#' @param groups A `vector` of grouping elements, as long as the variables in `x`.
#' @param groups A `vector` of grouping elements, as long as the variables in
#' `x`. If a single `character` string is passed, it must be the name of a
#' categorical variable from the original dataset.
#' If set, a matrix of panels defined by `groups` will be drawn.
#' @param rug A [`logical`] scalar: should a *rug* representation (1-d plot) of
#' the data be added to the plot?
Expand Down Expand Up @@ -1245,7 +1184,9 @@ NULL
#' `quantile` is used as a cut-off value for outlier detection: observations
#' with larger (squared) Mahalanobis distance are considered as potential
#' outliers.
#' @param groups A `vector` of grouping elements, as long as the variables in `object`.
#' @param groups A `vector` of grouping elements, as long as the variables in
#' `object`. If a single `character` string is passed, it must be the name of a
#' categorical variable from the original dataset.
#' @details
#' An outlier can be defined as having a very large Mahalanobis distance from
#' all observations. In this way, a certain proportion of the observations can
Expand Down
8 changes: 4 additions & 4 deletions R/barplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ NULL
# CompositionMatrix ============================================================
#' @export
#' @method barplot CompositionMatrix
barplot.CompositionMatrix <- function(height, ...,
barplot.CompositionMatrix <- function(height, ..., groups = NULL,
order = NULL, decreasing = FALSE,
groups = get_groups(height), horiz = TRUE,
horiz = TRUE,
xlab = NULL, ylab = NULL,
main = NULL, sub = NULL,
ann = graphics::par("ann"), axes = TRUE,
Expand Down Expand Up @@ -36,8 +36,8 @@ barplot.CompositionMatrix <- function(height, ...,
y_side <- if (horiz) 2 else 1

## Grouping
n <- 0
if (!all(is.na(groups))) {
groups <- get_variable(height, which = groups)
if (!is.null(groups) && !all(is.na(groups))) {
arkhe::assert_length(groups, nrow(z))
groups <- groups[ordering]

Expand Down
Loading

0 comments on commit 574d8d1

Please sign in to comment.