Skip to content

Commit

Permalink
Document as.data.frame() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Dec 18, 2024
1 parent 78bdee8 commit 1cbb78c
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 16 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ exportMethods("totals<-")
exportMethods(aggregate)
exportMethods(all_assigned)
exportMethods(any_assigned)
exportMethods(as.data.frame)
exportMethods(as_amounts)
exportMethods(as_composition)
exportMethods(as_graph)
Expand Down
22 changes: 20 additions & 2 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ setGeneric(
)

# Extract ======================================================================


## Subset ----------------------------------------------------------------------
#' Extract or Replace Parts of an Object
#'
Expand Down Expand Up @@ -272,6 +270,26 @@ setGeneric(
def = function(object, value) standardGeneric("totals<-")
)

#' Coerce to a Data Frame
#'
#' @param x An \R object (typically, a [`CompositionMatrix-class`] object).
#' @param row.names A [`character`] vector giving the row names for the data
#' frame, or `NULL`.
#' @param optional A [`logical`] scalar: should the names of the variables in
#' the data frame be checked?
#' @param group_var A [`character`] string specifying the name of the column to
#' create for group attribution.
#' @param ... Currently not used.
#' @return
#' A [`data.frame`].
#' @example inst/examples/ex-coerce.R
#' @author N. Frerebeau
#' @docType methods
#' @family mutators
#' @name as.data.frame
#' @rdname as.data.frame
NULL

#' Matrix Transpose
#'
#' @param x A [`CompositionMatrix-class`] object.
Expand Down
54 changes: 44 additions & 10 deletions R/coerce.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,40 +93,74 @@ setMethod(
# To data.frame ================================================================
#' @method as.data.frame CompositionMatrix
#' @export
as.data.frame.CompositionMatrix <- function(x, ...) {
as.data.frame(methods::as(x, "matrix"), row.names = rownames(x))
as.data.frame.CompositionMatrix <- function(x, row.names = rownames(x),
optional = FALSE, ...) {
as.data.frame(methods::as(x, "matrix"), row.names = row.names, optional = optional)
}

#' @export
#' @rdname as.data.frame
#' @aliases as.data.frame,CompositionMatrix-method
setMethod("as.data.frame", "CompositionMatrix", as.data.frame.CompositionMatrix)

#' @method as.data.frame GroupedComposition
#' @export
as.data.frame.GroupedComposition <- function(x, ..., group_var = ".group") {
as.data.frame.GroupedComposition <- function(x, row.names = rownames(x),
optional = FALSE, ...,
group_var = ".group") {
z <- data.frame(
methods::as(x, "matrix"),
row.names = rownames(x)
row.names = row.names,
check.names = !optional
)
z[[group_var]] <- group_names(x)
z
}

#' @export
#' @rdname as.data.frame
#' @aliases as.data.frame,GroupedComposition-method
setMethod("as.data.frame", "GroupedComposition", as.data.frame.GroupedComposition)

#' @method as.data.frame LogRatio
#' @export
as.data.frame.LogRatio <- function(x, ...) {
as.data.frame(methods::as(x, "matrix"), row.names = rownames(x))
as.data.frame.LogRatio <- function(x, row.names = rownames(x),
optional = FALSE, ...) {
as.data.frame(methods::as(x, "matrix"), row.names = row.names, optional = optional)
}

#' @export
#' @rdname as.data.frame
#' @aliases as.data.frame,LogRatio-method
setMethod("as.data.frame", "LogRatio", as.data.frame.LogRatio)

#' @method as.data.frame GroupedLogRatio
#' @export
as.data.frame.GroupedLogRatio <- function(x, ..., group_var = ".group") {
as.data.frame.GroupedLogRatio <- function(x, row.names = rownames(x),
optional = FALSE, ...,
group_var = ".group") {
z <- data.frame(
methods::as(x, "matrix"),
row.names = rownames(x)
row.names = row.names,
check.names = !optional
)
z[[group_var]] <- group_names(x)
z
}

#' @export
#' @rdname as.data.frame
#' @aliases as.data.frame,GroupedLogRatio-method
setMethod("as.data.frame", "GroupedLogRatio", as.data.frame.GroupedLogRatio)

#' @method as.data.frame OutlierIndex
#' @export
as.data.frame.OutlierIndex <- function(x, ...) {
as.data.frame(x@standard, row.names = rownames(x))
as.data.frame.OutlierIndex <- function(x, row.names = rownames(x),
optional = FALSE, ...) {
as.data.frame(x@standard, row.names = row.names, optional = optional)
}

#' @export
#' @rdname as.data.frame
#' @aliases as.data.frame,OutlierIndex-method
setMethod("as.data.frame", "OutlierIndex", as.data.frame.OutlierIndex)
2 changes: 1 addition & 1 deletion inst/examples/ex-coerce.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ totals(B)
A2 <- as_amounts(B)

## Coerce to a data.frame
X <- data.frame(B)
X <- as.data.frame(B)
head(X)
80 changes: 80 additions & 0 deletions man/as.data.frame.Rd

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

2 changes: 1 addition & 1 deletion man/as_amounts.Rd

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

2 changes: 1 addition & 1 deletion man/as_composition.Rd

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

1 change: 1 addition & 0 deletions man/mutators.Rd

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

3 changes: 2 additions & 1 deletion man/totals.Rd

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

0 comments on commit 1cbb78c

Please sign in to comment.