Skip to content

Commit 101c3a6

Browse files
committed
make sure custom transform methods are not circumvented
1 parent 62a826d commit 101c3a6

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

R/scale-.r

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,17 @@ check_breaks_labels <- function(breaks, labels) {
560560
TRUE
561561
}
562562

563+
default_transform <- function(self, x) {
564+
new_x <- self$trans$transform(x)
565+
axis <- if ("x" %in% self$aesthetics) "x" else "y"
566+
check_transformation(x, new_x, self$scale_name, axis)
567+
new_x
568+
}
569+
570+
has_default_transform <- function(scale) {
571+
transform_method <- environment(scale$transform)$f
572+
identical(default_transform, transform_method) || identical(identity, transform_method)
573+
}
563574

564575
#' @rdname ggplot2-ggproto
565576
#' @format NULL
@@ -589,12 +600,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
589600
!has_data && !has_limits
590601
},
591602

592-
transform = function(self, x) {
593-
new_x <- self$trans$transform(x)
594-
axis <- if ("x" %in% self$aesthetics) "x" else "y"
595-
check_transformation(x, new_x, self$scale_name, axis)
596-
new_x
597-
},
603+
transform = default_transform,
598604

599605
map = function(self, x, limits = self$get_limits()) {
600606
x <- self$rescale(self$oob(x, range = limits), limits)
@@ -819,9 +825,7 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
819825
self$range$train(x, drop = self$drop, na.rm = !self$na.translate)
820826
},
821827

822-
transform = function(x) {
823-
x
824-
},
828+
transform = identity,
825829

826830
map = function(self, x, limits = self$get_limits()) {
827831
n <- sum(!is.na(limits))
@@ -1001,12 +1005,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
10011005
self$range$train(x)
10021006
},
10031007

1004-
transform = function(self, x) {
1005-
new_x <- self$trans$transform(x)
1006-
axis <- if ("x" %in% self$aesthetics) "x" else "y"
1007-
check_transformation(x, new_x, self$scale_name, axis)
1008-
new_x
1009-
},
1008+
transform = default_transform,
10101009

10111010
map = function(self, x, limits = self$get_limits()) {
10121011
if (self$after.stat) {

R/scales-.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ scales_transform_df <- function(scales, df) {
8585
# if the scale contains no trans or the trans is of identity, it doesn't need
8686
# to be transformed.
8787
idx_skip <- vapply(scales$scales, function(x) {
88-
is.null(x$trans) ||
89-
identical(x$trans$transform, identity)
88+
has_default_transform(x) &&
89+
(is.null(x$trans) || identical(x$trans$transform, identity))
9090
}, logical(1L))
9191
scale_list <- scales$scales[!idx_skip]
9292

0 commit comments

Comments
 (0)