Skip to content

Simplify scale training #6443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 11 additions & 31 deletions R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -684,19 +684,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
if (length(x) == 0) {
return()
}
# Intercept error here to give examples and mention scale in call
if (is.factor(x) || !typeof(x) %in% c("integer", "double")) {
# These assumptions only hold for standard ContinuousRange class, so
# we skip the error if another range class is used
if (inherits(self$range, "ContinuousRange")) {
cli::cli_abort(
c("Discrete values supplied to continuous scale.",
i = "Example values: {.and {.val {head(x, 5)}}}"),
call = self$call
)
}
}
self$range$train(x)
self$range$train(x, call = self$call)
},

is_empty = function(self) {
Expand Down Expand Up @@ -964,19 +952,12 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
if (length(x) == 0) {
return()
}
# Intercept error here to give examples and mention scale in call
if (!is.discrete(x)) {
# These assumptions only hold for standard DiscreteRange class, so
# we skip the error if another range class is used
if (inherits(self$range, "DiscreteRange")) {
cli::cli_abort(
c("Continuous values supplied to discrete scale.",
i = "Example values: {.and {.val {head(x, 5)}}}"),
call = self$call
)
}
}
self$range$train(x, drop = self$drop, na.rm = !self$na.translate)
self$range$train(
x,
drop = self$drop,
na.rm = !self$na.translate,
call = self$call
)
},

transform = identity,
Expand Down Expand Up @@ -1196,17 +1177,16 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
is_discrete = function() FALSE,

train = function(self, x) {
if (length(x) == 0) {
return()
}
Comment on lines +1180 to +1182
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency's sake early exit first

if (!is.numeric(x)) {
cli::cli_abort(
"Binned scales only support continuous data.",
call = self$call
)
}

if (length(x) == 0) {
return()
}
self$range$train(x)
self$range$train(x, call = self$call)
},

transform = default_transform,
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/_snaps/scales.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@

# training incorrectly appropriately communicates the offenders

Continuous values supplied to discrete scale.
i Example values: 1, 2, 3, 4, and 5
Continuous value supplied to a discrete scale.
i Example values: 1, 2, 3, 4, and 5.

---

Discrete values supplied to continuous scale.
i Example values: "A", "B", "C", "D", and "E"
Discrete value supplied to a continuous scale.
i Example values: "A" and "E".
Comment on lines +206 to +207
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably slightly worse, but if we want to improve this, this should happen in the scales package.


# Using `scale_name` prompts deprecation message

Expand Down