Skip to content

Commit

Permalink
<AsIs> methods for rescale() (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand authored Nov 2, 2023
1 parent cfe1ce7 commit 16cb02b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ S3method(offset_by,numeric)
S3method(plot,trans)
S3method(print,trans)
S3method(rescale,"NULL")
S3method(rescale,AsIs)
S3method(rescale,Date)
S3method(rescale,POSIXt)
S3method(rescale,difftime)
Expand All @@ -20,6 +21,7 @@ S3method(rescale,integer64)
S3method(rescale,logical)
S3method(rescale,numeric)
S3method(rescale_mid,"NULL")
S3method(rescale_mid,AsIs)
S3method(rescale_mid,Date)
S3method(rescale_mid,POSIXt)
S3method(rescale_mid,dist)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
vectors during training. Mixing of character and factor data will still lead
to different results depending on the training order.
* Add a rescale method for `difftime` objects (#382)
* `rescale(I(x), ...)` and `rescale_mid(I(x), ...)` return `I(x)` unaltered
(@teunbrand, #403).
* The `scale_cut` argument in `number()` now works as advertised for values
below the lowest cut value (#346)
* `label_dollar()` has been superseeded by `label_currency()` for clarity (#344)
Expand Down
12 changes: 12 additions & 0 deletions R/bounds.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#' @param from input range (vector of length two). If not given, is
#' calculated from the range of `x`
#' @param ... other arguments passed on to methods
#' @details
#' Objects of class `<AsIs>` are returned unaltered.
#'
#' @keywords manip
#' @export
#' @examples
Expand Down Expand Up @@ -62,6 +65,9 @@ rescale.integer64 <- function(x, to = c(0, 1), from = range(x, na.rm = TRUE), ..
#' @export
rescale.difftime <- rescale.numeric

#' @rdname rescale
#' @export
rescale.AsIs <- function(x, to, from, ...) x

#' Rescale vector to have specified minimum, midpoint, and maximum
#'
Expand All @@ -72,6 +78,8 @@ rescale.difftime <- rescale.numeric
#' calculated from the range of `x`
#' @param mid mid-point of input range
#' @param ... other arguments passed on to methods
#' @details
#' Objects of class `<AsIs>` are returned unaltered.
#' @examples
#' rescale_mid(1:100, mid = 50.5)
#' rescale_mid(runif(50), mid = 0.5)
Expand Down Expand Up @@ -133,6 +141,10 @@ rescale_mid.integer64 <- function(x, to = c(0, 1), from = range(x, na.rm = TRUE)
}


#' @rdname rescale_mid
#' @export
rescale_mid.AsIs <- function(x, to, from, ...) x

#' Rescale numeric vector to have specified maximum
#'
#' @export
Expand Down
6 changes: 6 additions & 0 deletions man/rescale.Rd

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

6 changes: 6 additions & 0 deletions man/rescale_mid.Rd

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

5 changes: 5 additions & 0 deletions tests/testthat/test-bounds.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ test_that("scaling is possible with NULL values", {
expect_null(rescale_mid(NULL))
})

test_that("rescaling does not alter AsIs objects", {
expect_identical(I(1:3), rescale(I(1:3), from = c(0, 4)))
expect_identical(I(1:3), rescale_mid(I(1:3), from = c(0, 4), mid = 1))
})

test_that("scaling is possible with logical values", {
expect_equal(rescale(c(FALSE, TRUE)), c(0, 1))
expect_equal(rescale_mid(c(FALSE, TRUE), mid = 0.5), c(0, 1))
Expand Down

0 comments on commit 16cb02b

Please sign in to comment.