Skip to content

Commit

Permalink
train_continuous() coerces new to numeric (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand authored Nov 1, 2023
1 parent 1a98998 commit 636b1ca
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# scales (development version)

* `train_continuous()` coerces `new` to numeric before calculating range
(@teunbrand, #369).

# scales 1.2.1

* Re-document to fix HTML issues in `.Rd`.
Expand Down
5 changes: 4 additions & 1 deletion R/scale-continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ train_continuous <- function(new, existing = NULL) {
cli::cli_abort("Discrete value supplied to a continuous scale")
}

suppressWarnings(range(existing, new, na.rm = TRUE, finite = TRUE))
# Needs casting to numeric because some `new` vectors can misbehave when
# combined with a NULL `existing` (#369)
suppressWarnings(range(existing, as.numeric(new),
na.rm = TRUE, finite = TRUE))
}

# Map values for a continuous palette.
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-scale-continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@ test_that("train_continuous with new=NULL maintains existing range.", {
c(1, 5)
)
})

test_that("train_continuous works with integer64", {
skip_if_not_installed("bit64")
new <- bit64::as.integer64(1:10)
expect_identical(
train_continuous(new),
c(1, 10)
)
})

0 comments on commit 636b1ca

Please sign in to comment.