Skip to content

Commit

Permalink
Make it possible to deprecated argument 'center'; introduced option '…
Browse files Browse the repository at this point in the history
…matrixStats.center.onUse'/env var 'MATRIXSTATS_CENTER_ONUSE' for controlling this [#187]
  • Loading branch information
HenrikBengtsson committed Nov 25, 2020
1 parent 49d59a7 commit c7db362
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
27 changes: 27 additions & 0 deletions R/000.DEPRECATION.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
centerOnUse <- function(fcnname, calls = sys.calls(), msg = NULL) {
value <- getOption("matrixStats.center.onUse")
if (is.null(value) || identical(value, "ignore")) return()

value <- match.arg(value, c("deprecated", "defunct"))
fcn <- switch(value, deprecated = .Deprecated, defunct = .Defunct)

if (is.null(msg)) {
msg <- sprintf("Argument 'center' of %s::%s() is %s: %s",
.packageName, fcnname, value, deparse(calls[[1]]))
}

fcn(msg = msg, package = .packageName)
}


onLoadSetCenterOnUse <- function() {
## Option is already set?
if (!is.null(getOption("matrixStats.center.onUse", NULL))) return()

## Is environment variable set?
value <- Sys.getenv("MATRIXSTATS_CENTER_ONUSE", NA_character_)
if (is.na(value)) return()

value <- match.arg(value, c("ignore", "deprecated", "defunct"))
options(matrixStats.center.onUse = value)
}
8 changes: 7 additions & 1 deletion R/rowMads.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ rowMads <- function(x, rows = NULL, cols = NULL, center = NULL,
has_nas <- TRUE
x <- .Call(C_rowMads, x, dim., rows, cols, constant, na.rm, has_nas, TRUE)
} else {
## https://github.com/HenrikBengtsson/matrixStats/issues/187
centerOnUse("rowMads")

if (is.vector(x)) dim(x) <- dim.

# Apply subset on 'center'
if (length(center) != nrow(x)) {
## Scalar 'center'?
Expand Down Expand Up @@ -53,6 +56,9 @@ colMads <- function(x, rows = NULL, cols = NULL, center = NULL,
has_nas <- TRUE
x <- .Call(C_rowMads, x, dim., rows, cols, constant, na.rm, has_nas, FALSE)
} else {
## https://github.com/HenrikBengtsson/matrixStats/issues/187
centerOnUse("colMads")

if (is.vector(x)) dim(x) <- dim.

# Apply subset on 'center'
Expand Down
6 changes: 6 additions & 0 deletions R/rowVars.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ rowVars <- function(x, rows = NULL, cols = NULL, na.rm = FALSE, center = NULL,
return(sigma2)
}

## https://github.com/HenrikBengtsson/matrixStats/issues/187
centerOnUse("rowVars")

if (is.vector(x)) dim(x) <- dim.

# Apply subset on 'center'
Expand Down Expand Up @@ -117,6 +120,9 @@ colVars <- function(x, rows = NULL, cols = NULL, na.rm = FALSE, center = NULL,
return(sigma2)
}

## https://github.com/HenrikBengtsson/matrixStats/issues/187
centerOnUse("colVars")

if (is.vector(x)) dim(x) <- dim.

# Apply subset on 'center'
Expand Down
3 changes: 3 additions & 0 deletions R/weightedMad.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ weightedMad <- function(x, w = NULL, idxs = NULL, na.rm = FALSE,
# Estimate the mean?
if (is.null(center)) {
center <- weightedMedian(x, w = w, na.rm = NA)
} else {
## https://github.com/HenrikBengtsson/matrixStats/issues/187
centerOnUse("weightedMad")
}

# Estimate the standard deviation
Expand Down
3 changes: 3 additions & 0 deletions R/weightedVar.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ weightedVar <- function(x, w = NULL, idxs = NULL, na.rm = FALSE,
# Estimate the mean?
if (is.null(center)) {
center <- sum(w * x) / wsum
} else {
## https://github.com/HenrikBengtsson/matrixStats/issues/187
centerOnUse("weightedVar")
}

# Estimate the variance
Expand Down
5 changes: 5 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.onLoad <- function(libname, pkgname) {
## https://github.com/HenrikBengtsson/matrixStats/issues/187
onLoadSetCenterOnUse()
}

#' @useDynLib "matrixStats", .registration = TRUE, .fixes = "C_"
.onUnload <- function(libpath) {
library.dynam.unload("matrixStats", libpath)
Expand Down

0 comments on commit c7db362

Please sign in to comment.