diff --git a/DESCRIPTION b/DESCRIPTION index 616e7a5..0d52fcc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: quantities Type: Package Title: Quantity Calculus for R Vectors -Version: 0.2.1 +Version: 0.2.1.1 Authors@R: c( person("IƱaki", "Ucar", email="iucar@fedoraproject.org", role=c("aut", "cph", "cre"), comment=c(ORCID="0000-0001-6403-5550"))) @@ -22,6 +22,6 @@ Suggests: dplyr (>= 1.0.0), vctrs (>= 0.5.0), tidyr, pillar, ggplot2 (> 3.2.1), testthat, vdiffr, knitr, rmarkdown LinkingTo: Rcpp (>= 0.12.10) ByteCompile: yes -RoxygenNote: 7.2.2 +RoxygenNote: 7.2.3 Roxygen: list(old_usage = TRUE) VignetteBuilder: knitr diff --git a/NEWS.md b/NEWS.md index 0f80deb..c73fd78 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# quantities devel + +- Performance improvements for `data.frame` methods. + # quantities 0.2.1 - Fix a couple of tests (r-quantities/units#324). diff --git a/R/drop.R b/R/drop.R index ee31ae9..5b2cdbf 100644 --- a/R/drop.R +++ b/R/drop.R @@ -30,9 +30,5 @@ drop_errors.quantities <- drop_units.quantities #' @name drop_quantities #' @export drop_quantities.data.frame <- function(x) { - for (i in seq_along(x)) { - if (inherits(x[[i]], "quantities")) - x[[i]] <- drop_quantities(x[[i]]) - } - x + dfapply(x, function(i) if (inherits(i, "quantities")) drop_quantities(i) else i) } diff --git a/R/misc.R b/R/misc.R index f86d5aa..bfd3742 100644 --- a/R/misc.R +++ b/R/misc.R @@ -86,18 +86,8 @@ diff.quantities <- function(x, lag = 1L, differences = 1L, ...) reclass(NextMeth #' as.data.frame(z) #' #' @export -as.data.frame.quantities <- function(x, row.names = NULL, optional = FALSE, ...) { - e <- errors(x) - dim(e) <- dim(x) - e <- as.data.frame(e) - value <- NextMethod() - if (!optional && ncol(value) == 1) - colnames(value) <- deparse(substitute(x)) - for (i in seq_len(ncol(value))) { - value[[i]] <- reclass(value[[i]]) - errors(value[[i]]) <- e[[i]] - } - value +as.data.frame.quantities <- function(x, ...) { + dfapply(as.data.frame(drop_units(x), ...), as_units, units(x)) } #' Coerce to a List diff --git a/R/utils.R b/R/utils.R index ca51a84..1c43f44 100644 --- a/R/utils.R +++ b/R/utils.R @@ -3,3 +3,10 @@ reclass <- function(x) { class(x) <- c("quantities", "units", "errors") x } + +dfapply <- function(X, FUN, ...) { + attrs <- attributes(X) + X <- lapply(X, FUN, ...) + attributes(X) <- attrs + X +} diff --git a/man/as.data.frame.quantities.Rd b/man/as.data.frame.quantities.Rd index dc33c8d..db9ab52 100644 --- a/man/as.data.frame.quantities.Rd +++ b/man/as.data.frame.quantities.Rd @@ -4,23 +4,11 @@ \alias{as.data.frame.quantities} \title{Coerce to a Data Frame} \usage{ -\method{as.data.frame}{quantities}(x, row.names = NULL, optional = FALSE, - ...) +\method{as.data.frame}{quantities}(x, ...) } \arguments{ \item{x}{any \R object.} -\item{row.names}{\code{NULL} or a character vector giving the row - names for the data frame. Missing values are not allowed.} - -\item{optional}{logical. If \code{TRUE}, setting row names and - converting column names (to syntactic names: see - \code{\link[base]{make.names}}) is optional. Note that all of \R's - \pkg{base} package \code{as.data.frame()} methods use - \code{optional} only for column names treatment, basically with the - meaning of \code{\link[base]{data.frame}(*, check.names = !optional)}. - See also the \code{make.names} argument of the \code{matrix} method.} - \item{...}{additional arguments to be passed to or from methods.} } \description{ diff --git a/man/cbind.quantities.Rd b/man/cbind.quantities.Rd index e39a9a6..9f0652e 100644 --- a/man/cbind.quantities.Rd +++ b/man/cbind.quantities.Rd @@ -19,8 +19,9 @@ \item{deparse.level}{integer controlling the construction of labels in the case of non-matrix-like arguments (for the default method):\cr - \code{deparse.level = 0} constructs no labels; the default,\cr - \code{deparse.level = 1 or 2} constructs labels from the argument + \code{deparse.level = 0} constructs no labels;\cr + the default \code{deparse.level = 1} typically and + \code{deparse.level = 2} always construct labels from the argument names, see the \sQuote{Value} section below.} } \description{