Skip to content

Commit 7cc5c72

Browse files
authored
Simplify scale training (#6443)
* pass on calls * accept snapshot
1 parent 40bdd82 commit 7cc5c72

File tree

2 files changed

+15
-35
lines changed

2 files changed

+15
-35
lines changed

R/scale-.R

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -690,19 +690,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
690690
if (length(x) == 0) {
691691
return()
692692
}
693-
# Intercept error here to give examples and mention scale in call
694-
if (is.factor(x) || !typeof(x) %in% c("integer", "double")) {
695-
# These assumptions only hold for standard ContinuousRange class, so
696-
# we skip the error if another range class is used
697-
if (inherits(self$range, "ContinuousRange")) {
698-
cli::cli_abort(
699-
c("Discrete values supplied to continuous scale.",
700-
i = "Example values: {.and {.val {head(x, 5)}}}"),
701-
call = self$call
702-
)
703-
}
704-
}
705-
self$range$train(x)
693+
self$range$train(x, call = self$call)
706694
},
707695

708696
is_empty = function(self) {
@@ -960,19 +948,12 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
960948
if (length(x) == 0) {
961949
return()
962950
}
963-
# Intercept error here to give examples and mention scale in call
964-
if (!is.discrete(x)) {
965-
# These assumptions only hold for standard DiscreteRange class, so
966-
# we skip the error if another range class is used
967-
if (inherits(self$range, "DiscreteRange")) {
968-
cli::cli_abort(
969-
c("Continuous values supplied to discrete scale.",
970-
i = "Example values: {.and {.val {head(x, 5)}}}"),
971-
call = self$call
972-
)
973-
}
974-
}
975-
self$range$train(x, drop = self$drop, na.rm = !self$na.translate)
951+
self$range$train(
952+
x,
953+
drop = self$drop,
954+
na.rm = !self$na.translate,
955+
call = self$call
956+
)
976957
},
977958

978959
transform = identity,
@@ -1185,17 +1166,16 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
11851166
is_discrete = function() FALSE,
11861167

11871168
train = function(self, x) {
1169+
if (length(x) == 0) {
1170+
return()
1171+
}
11881172
if (!is.numeric(x)) {
11891173
cli::cli_abort(
11901174
"Binned scales only support continuous data.",
11911175
call = self$call
11921176
)
11931177
}
1194-
1195-
if (length(x) == 0) {
1196-
return()
1197-
}
1198-
self$range$train(x)
1178+
self$range$train(x, call = self$call)
11991179
},
12001180

12011181
transform = default_transform,

tests/testthat/_snaps/scales.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,13 @@
198198

199199
# training incorrectly appropriately communicates the offenders
200200

201-
Continuous values supplied to discrete scale.
202-
i Example values: 1, 2, 3, 4, and 5
201+
Continuous value supplied to a discrete scale.
202+
i Example values: 1, 2, 3, 4, and 5.
203203

204204
---
205205

206-
Discrete values supplied to continuous scale.
207-
i Example values: "A", "B", "C", "D", and "E"
206+
Discrete value supplied to a continuous scale.
207+
i Example values: "A" and "E".
208208

209209
# Using `scale_name` prompts deprecation message
210210

0 commit comments

Comments
 (0)