Skip to content

Commit f16a15b

Browse files
committed
tidyverse#4173 lambda functions in discrete scales & facets
1 parent 813d0bd commit f16a15b

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

R/labeller.r

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ resolve_labeller <- function(rows, cols, labels) {
297297
#' # your labeller to the right variable with labeller()
298298
#' p + facet_grid(cyl ~ am, labeller = labeller(am = to_string))
299299
as_labeller <- function(x, default = label_value, multi_line = TRUE) {
300+
if(is.formula(x)) x <- as_function(x)
300301
force(x)
301302
fun <- function(labels) {
302303
labels <- lapply(labels, as.character)
@@ -426,7 +427,15 @@ labeller <- function(..., .rows = NULL, .cols = NULL,
426427
if (!is.null(keep.as.numeric)) {
427428
.Deprecated(old = "keep.as.numeric")
428429
}
430+
429431
dots <- list(...)
432+
433+
first_dot <- dots[[1]]
434+
435+
if (is.formula(first_dot) || is.function(first_dot)) {
436+
return(as_labeller(first_dot))
437+
}
438+
430439
.default <- as_labeller(.default)
431440

432441
function(labels) {

R/scale-.r

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ discrete_scale <- function(aesthetics, scale_name, palette, name = waiver(),
168168

169169
check_breaks_labels(breaks, labels)
170170

171+
if (is.formula(labels)) labels <- as_function(labels)
172+
171173
if (!is.function(limits) && (length(limits) > 0) && !is.discrete(limits)) {
172174
warn(
173175
glue(

0 commit comments

Comments
 (0)