Skip to content

Commit

Permalink
make sure custom transform methods are not circumvented
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Oct 7, 2022
1 parent 62a826d commit 101c3a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
29 changes: 14 additions & 15 deletions R/scale-.r
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,17 @@ check_breaks_labels <- function(breaks, labels) {
TRUE
}

default_transform <- function(self, x) {
new_x <- self$trans$transform(x)
axis <- if ("x" %in% self$aesthetics) "x" else "y"
check_transformation(x, new_x, self$scale_name, axis)
new_x
}

has_default_transform <- function(scale) {
transform_method <- environment(scale$transform)$f
identical(default_transform, transform_method) || identical(identity, transform_method)
}

#' @rdname ggplot2-ggproto
#' @format NULL
Expand Down Expand Up @@ -589,12 +600,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
!has_data && !has_limits
},

transform = function(self, x) {
new_x <- self$trans$transform(x)
axis <- if ("x" %in% self$aesthetics) "x" else "y"
check_transformation(x, new_x, self$scale_name, axis)
new_x
},
transform = default_transform,

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

transform = function(x) {
x
},
transform = identity,

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

transform = function(self, x) {
new_x <- self$trans$transform(x)
axis <- if ("x" %in% self$aesthetics) "x" else "y"
check_transformation(x, new_x, self$scale_name, axis)
new_x
},
transform = default_transform,

map = function(self, x, limits = self$get_limits()) {
if (self$after.stat) {
Expand Down
4 changes: 2 additions & 2 deletions R/scales-.r
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ scales_transform_df <- function(scales, df) {
# if the scale contains no trans or the trans is of identity, it doesn't need
# to be transformed.
idx_skip <- vapply(scales$scales, function(x) {
is.null(x$trans) ||
identical(x$trans$transform, identity)
has_default_transform(x) &&
(is.null(x$trans) || identical(x$trans$transform, identity))
}, logical(1L))
scale_list <- scales$scales[!idx_skip]

Expand Down

0 comments on commit 101c3a6

Please sign in to comment.