-
Notifications
You must be signed in to change notification settings - Fork 2.1k
n.breaks
are supplied to function-breaks.
#5974
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
base: main
Are you sure you want to change the base?
Changes from all commits
5872bae
2799a86
78ecf62
151b4b1
4d0ed09
b4c5eb5
6fedc16
89316f6
1d44b9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -640,6 +640,12 @@ | |
if (is.null(breaks) || is.null(labels)) { | ||
return(invisible()) | ||
} | ||
if (identical(breaks, NA)) { | ||
cli::cli_abort( | ||
"Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.", | ||
call = call | ||
) | ||
} | ||
|
||
bad_labels <- is.atomic(breaks) && is.atomic(labels) && | ||
length(breaks) != length(labels) | ||
|
@@ -751,48 +757,38 @@ | |
return(numeric()) | ||
} | ||
transformation <- self$get_transformation() | ||
breaks <- self$breaks %|W|% transformation$breaks | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like to think of this line as the 'spirit of this PR'. We no longer need to treat function-breaks and default breaks any differently. |
||
|
||
if (is.null(breaks)) { | ||
return(NULL) | ||
} | ||
|
||
# Ensure limits don't exceed domain (#980) | ||
domain <- suppressWarnings(transformation$transform(transformation$domain)) | ||
domain <- sort(domain) | ||
# To avoid NaN causing issues. NaN are dropped by the sort() | ||
if (length(domain) == 2 && !zero_range(domain)) { | ||
limits <- oob_squish(limits, domain) | ||
} | ||
|
||
# Limits in transformed space need to be converted back to data space | ||
limits <- transformation$inverse(limits) | ||
|
||
if (is.null(self$breaks)) { | ||
return(NULL) | ||
} | ||
|
||
if (identical(self$breaks, NA)) { | ||
cli::cli_abort( | ||
"Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.", | ||
call = self$call | ||
) | ||
if (zero_range(as.numeric(limits))) { | ||
return(limits[1]) | ||
} | ||
|
||
# Compute `zero_range()` in transformed space in case `limits` in data space | ||
# don't support conversion to numeric (#5304) | ||
if (zero_range(as.numeric(transformation$transform(limits)))) { | ||
breaks <- limits[1] | ||
} else if (is.waiver(self$breaks)) { | ||
if (!is.null(self$n.breaks) && trans_support_nbreaks(transformation)) { | ||
breaks <- transformation$breaks(limits, self$n.breaks) | ||
if (is.function(breaks)) { | ||
# Limits in transformed space need to be converted back to data space | ||
limits <- transformation$inverse(limits) | ||
if (!is.null(self$n.breaks) && support_nbreaks(breaks)) { | ||
breaks <- breaks(limits, n = self$n.breaks) | ||
} else { | ||
breaks <- breaks(limits) | ||
if (!is.null(self$n.breaks)) { | ||
cli::cli_warn( | ||
"Ignoring {.arg n.breaks}. Use a {.cls transform} object that supports setting number of breaks.", | ||
"Ignoring {.arg n.breaks}. Use a {.cls transform} object or \\ | ||
{.arg breaks} function that supports setting number of breaks", | ||
call = self$call | ||
) | ||
} | ||
breaks <- transformation$breaks(limits) | ||
} | ||
} else if (is.function(self$breaks)) { | ||
breaks <- self$breaks(limits) | ||
} else { | ||
Comment on lines
+777
to
-794
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We no longer need separate logic for the default breaks (breaks function(!) from transformation) and function-breaks, as both now access the |
||
breaks <- self$breaks | ||
} | ||
|
||
# Breaks in data space need to be converted back to transformed space | ||
|
@@ -1046,13 +1042,6 @@ | |
return(NULL) | ||
} | ||
|
||
if (identical(self$breaks, NA)) { | ||
cli::cli_abort( | ||
"Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.", | ||
call = self$call | ||
) | ||
} | ||
|
||
Comment on lines
-1049
to
-1055
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also caught in constructor |
||
if (is.waiver(self$breaks)) { | ||
breaks <- limits | ||
} else if (is.function(self$breaks)) { | ||
|
@@ -1268,14 +1257,9 @@ | |
|
||
if (is.null(self$breaks)) { | ||
return(NULL) | ||
} else if (identical(self$breaks, NA)) { | ||
cli::cli_abort( | ||
"Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.", | ||
call = self$call | ||
) | ||
Comment on lines
-1271
to
-1275
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also caught in constructor |
||
} else if (is.waiver(self$breaks)) { | ||
if (self$nice.breaks) { | ||
if (!is.null(self$n.breaks) && trans_support_nbreaks(transformation)) { | ||
if (!is.null(self$n.breaks) && support_nbreaks(transformation$breaks)) { | ||
breaks <- transformation$breaks(limits, n = self$n.breaks) | ||
} else { | ||
if (!is.null(self$n.breaks)) { | ||
|
@@ -1332,9 +1316,16 @@ | |
} | ||
} | ||
} else if (is.function(self$breaks)) { | ||
if ("n.breaks" %in% names(formals(environment(self$breaks)$f))) { | ||
fmls <- names(formals(environment(self$breaks)$f)) | ||
if (any(c("n", "n.breaks") %in% fmls)) { | ||
n.breaks <- self$n.breaks %||% 5 # same default as trans objects | ||
breaks <- self$breaks(limits, n.breaks = n.breaks) | ||
# TODO: we should only allow `n` argument and not `n.breaks` to be | ||
# consistent with other scales. We should start deprecation at some point. | ||
if ("n.breaks" %in% fmls) { | ||
breaks <- self$breaks(limits, n.breaks = n.breaks) | ||
} else { | ||
breaks <- self$breaks(limits, n = n.breaks) | ||
} | ||
} else { | ||
if (!is.null(self$n.breaks)) { | ||
cli::cli_warn( | ||
|
@@ -1439,6 +1430,14 @@ | |
cli::cli_warn(msg, call = call) | ||
} | ||
|
||
|
||
support_nbreaks <- function(fun) { | ||
if (inherits(fun, "ggproto_method")) { | ||
fun <- environment(fun)$f | ||
} | ||
Comment on lines
+1435
to
+1437
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might need to deal with ggproto methods as |
||
"n" %in% fn_fmls_names(fun) | ||
} | ||
|
||
check_continuous_limits <- function(limits, ..., | ||
arg = caller_arg(limits), | ||
call = caller_env()) { | ||
|
@@ -1449,10 +1448,6 @@ | |
check_length(limits, 2L, arg = arg, call = call) | ||
} | ||
|
||
trans_support_nbreaks <- function(trans) { | ||
"n" %in% names(formals(trans$breaks)) | ||
} | ||
|
||
allow_lambda <- function(x) { | ||
if (is_formula(x)) as_function(x) else x | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -430,21 +430,18 @@ test_that("scales accept lambda notation for function input", { | |
|
||
test_that("breaks and labels are correctly checked", { | ||
expect_snapshot_error(check_breaks_labels(1:10, letters)) | ||
p <- ggplot(mtcars) + geom_point(aes(mpg, disp)) + scale_x_continuous(breaks = NA) | ||
expect_snapshot_error(ggplot_build(p)) | ||
expect_snapshot_error(scale_x_continuous(breaks = NA)) | ||
p <- ggplot(mtcars) + geom_point(aes(mpg, disp)) + scale_x_continuous(minor_breaks = NA) | ||
expect_snapshot_error(ggplot_build(p)) | ||
p <- ggplot(mtcars) + geom_point(aes(mpg, disp)) + scale_x_continuous(labels = NA) | ||
expect_snapshot_error(ggplotGrob(p)) | ||
p <- ggplot(mtcars) + geom_point(aes(mpg, disp)) + scale_x_continuous(labels = function(x) 1:2) | ||
expect_snapshot_error(ggplotGrob(p)) | ||
p <- ggplot(mtcars) + geom_bar(aes(factor(gear))) + scale_x_discrete(breaks = NA) | ||
expect_snapshot_error(ggplot_build(p)) | ||
expect_snapshot_error(scale_x_discrete(breaks = NA)) | ||
p <- ggplot(mtcars) + geom_bar(aes(factor(gear))) + scale_x_discrete(labels = NA) | ||
expect_snapshot_error(ggplotGrob(p)) | ||
|
||
p <- ggplot(mtcars) + geom_bar(aes(mpg)) + scale_x_binned(breaks = NA) | ||
expect_snapshot_error(ggplot_build(p)) | ||
expect_snapshot_error(scale_x_binned(breaks = NA)) | ||
Comment on lines
-434
to
+444
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again simplification due to |
||
p <- ggplot(mtcars) + geom_bar(aes(mpg)) + scale_x_binned(labels = NA) | ||
expect_snapshot_error(ggplotGrob(p)) | ||
p <- ggplot(mtcars) + geom_bar(aes(mpg)) + scale_x_binned(labels = function(x) 1:2) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an adjacent change that seemed like a good idea because we can now omit this check in the
get_breaks()
calculation. It is the reason why the unit tests had to be adapted.