From 636b1ca1ac494c368dd22ed21fad181f0b969017 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 1 Nov 2023 11:10:58 +0100 Subject: [PATCH] `train_continuous()` coerces `new` to `numeric` (#374) --- NEWS.md | 3 +++ R/scale-continuous.R | 5 ++++- tests/testthat/test-scale-continuous.R | 9 +++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 71228d4b..1e3b17eb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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`. diff --git a/R/scale-continuous.R b/R/scale-continuous.R index 8ad1cfb1..a26334ae 100644 --- a/R/scale-continuous.R +++ b/R/scale-continuous.R @@ -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. diff --git a/tests/testthat/test-scale-continuous.R b/tests/testthat/test-scale-continuous.R index fd81e4f2..8c8e3074 100644 --- a/tests/testthat/test-scale-continuous.R +++ b/tests/testthat/test-scale-continuous.R @@ -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) + ) +})