Skip to content

Commit 91431a1

Browse files
authored
Propagate call for error messages (#416)
1 parent 80d3d7f commit 91431a1

6 files changed

+36
-11
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ Config/testthat/edition: 3
3939
Encoding: UTF-8
4040
LazyLoad: yes
4141
Roxygen: list(markdown = TRUE, r6 = FALSE)
42-
RoxygenNote: 7.2.3
42+
RoxygenNote: 7.3.2

R/range.R

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ DiscreteRange <- R6::R6Class(
1818
inherit = Range,
1919
list(
2020
factor = NULL,
21-
train = function(x, drop = FALSE, na.rm = FALSE) {
21+
train = function(x, drop = FALSE, na.rm = FALSE, call = caller_env()) {
2222
self$factor <- self$factor %||% is.factor(x)
23-
self$range <- train_discrete(x, self$range, drop, na.rm, self$factor)
23+
self$range <- train_discrete(x, self$range, drop, na.rm,
24+
self$factor, call = call)
2425
},
2526
reset = function() {
2627
self$range <- NULL
@@ -35,7 +36,9 @@ ContinuousRange <- R6::R6Class(
3536
"ContinuousRange",
3637
inherit = Range,
3738
list(
38-
train = function(x) self$range <- train_continuous(x, self$range),
39+
train = function(x, call = caller_env()) {
40+
self$range <- train_continuous(x, self$range, call = call)
41+
},
3942
reset = function() self$range <- NULL
4043
)
4144
)

R/scale-continuous.R

+7-2
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ cscale <- function(x, palette, na.value = NA_real_, trans = transform_identity()
3737
#'
3838
#' @inheritParams train_discrete
3939
#' @export
40-
train_continuous <- function(new, existing = NULL) {
40+
train_continuous <- function(new, existing = NULL, call = caller_env()) {
4141
if (is.null(new)) {
4242
return(existing)
4343
}
4444

4545
if (is.factor(new) || !typeof(new) %in% c("integer", "double")) {
46-
cli::cli_abort("Discrete value supplied to a continuous scale")
46+
example <- unique(new)
47+
example <- example[seq_len(pmin(length(example), 5))]
48+
cli::cli_abort(c(
49+
"Discrete value supplied to a continuous scale.",
50+
i = "Example values: {.and {.val {example}}}."
51+
), call = call)
4752
}
4853

4954
# Needs casting to numeric because some `new` vectors can misbehave when

R/scale-discrete.R

+9-3
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ is.discrete <- function(x) {
2525
#' @param drop `TRUE`, will drop factor levels not associated with data
2626
#' @param na.rm If `TRUE`, will remove missing values
2727
#' @param fct Treat `existing` as if it came from a factor (ie. don't sort the range)
28+
#' @param call A call to display in error messages
2829
#' @export
29-
train_discrete <- function(new, existing = NULL, drop = FALSE, na.rm = FALSE, fct = NA) {
30+
train_discrete <- function(new, existing = NULL, drop = FALSE,
31+
na.rm = FALSE, fct = NA, call = caller_env()) {
3032
if (is.null(new)) {
3133
return(existing)
3234
}
33-
3435
if (!is.discrete(new)) {
35-
cli::cli_abort("Continuous value supplied to a discrete scale")
36+
example <- unique(new)
37+
example <- example[seq_len(pmin(length(example), 5))]
38+
cli::cli_abort(c(
39+
"Continuous value supplied to a discrete scale.",
40+
i = "Example values: {.and {.val {example}}}."
41+
), call = call)
3642
}
3743
discrete_range(existing, new, drop = drop, na.rm = na.rm, fct = fct)
3844
}

man/train_continuous.Rd

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/train_discrete.Rd

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)