Skip to content

Commit

Permalink
Propagate call for error messages (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand authored Oct 21, 2024
1 parent 80d3d7f commit 91431a1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ Config/testthat/edition: 3
Encoding: UTF-8
LazyLoad: yes
Roxygen: list(markdown = TRUE, r6 = FALSE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
9 changes: 6 additions & 3 deletions R/range.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ DiscreteRange <- R6::R6Class(
inherit = Range,
list(
factor = NULL,
train = function(x, drop = FALSE, na.rm = FALSE) {
train = function(x, drop = FALSE, na.rm = FALSE, call = caller_env()) {
self$factor <- self$factor %||% is.factor(x)
self$range <- train_discrete(x, self$range, drop, na.rm, self$factor)
self$range <- train_discrete(x, self$range, drop, na.rm,
self$factor, call = call)
},
reset = function() {
self$range <- NULL
Expand All @@ -35,7 +36,9 @@ ContinuousRange <- R6::R6Class(
"ContinuousRange",
inherit = Range,
list(
train = function(x) self$range <- train_continuous(x, self$range),
train = function(x, call = caller_env()) {
self$range <- train_continuous(x, self$range, call = call)
},
reset = function() self$range <- NULL
)
)
9 changes: 7 additions & 2 deletions R/scale-continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ cscale <- function(x, palette, na.value = NA_real_, trans = transform_identity()
#'
#' @inheritParams train_discrete
#' @export
train_continuous <- function(new, existing = NULL) {
train_continuous <- function(new, existing = NULL, call = caller_env()) {
if (is.null(new)) {
return(existing)
}

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

# Needs casting to numeric because some `new` vectors can misbehave when
Expand Down
12 changes: 9 additions & 3 deletions R/scale-discrete.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@ is.discrete <- function(x) {
#' @param drop `TRUE`, will drop factor levels not associated with data
#' @param na.rm If `TRUE`, will remove missing values
#' @param fct Treat `existing` as if it came from a factor (ie. don't sort the range)
#' @param call A call to display in error messages
#' @export
train_discrete <- function(new, existing = NULL, drop = FALSE, na.rm = FALSE, fct = NA) {
train_discrete <- function(new, existing = NULL, drop = FALSE,
na.rm = FALSE, fct = NA, call = caller_env()) {
if (is.null(new)) {
return(existing)
}

if (!is.discrete(new)) {
cli::cli_abort("Continuous value supplied to a discrete scale")
example <- unique(new)
example <- example[seq_len(pmin(length(example), 5))]
cli::cli_abort(c(
"Continuous value supplied to a discrete scale.",
i = "Example values: {.and {.val {example}}}."
), call = call)
}
discrete_range(existing, new, drop = drop, na.rm = na.rm, fct = fct)
}
Expand Down
4 changes: 3 additions & 1 deletion man/train_continuous.Rd

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

11 changes: 10 additions & 1 deletion man/train_discrete.Rd

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

0 comments on commit 91431a1

Please sign in to comment.