Skip to content

Commit e86c02a

Browse files
committed
FEAT: Let the scale mapping accept parameters
1 parent d9e577d commit e86c02a

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

R/scale-.r

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,25 +458,38 @@ Scale <- ggproto("Scale", NULL,
458458
cli::cli_abort("Not implemented")
459459
},
460460

461-
map_df = function(self, df, i = NULL) {
461+
map_df = function(self, df, i = NULL, scale_params = NULL) {
462462
if (empty(df)) {
463463
return()
464464
}
465+
if (is.null(scale_params)) {
466+
scale_params <- list()
467+
}
465468

466469
aesthetics <- intersect(self$aesthetics, names(df))
467470
names(aesthetics) <- aesthetics
468471
if (length(aesthetics) == 0) {
469472
return()
470473
}
471474

472-
if (is.null(i)) {
473-
lapply(aesthetics, function(j) self$map(df[[j]]))
475+
if ("scale_params" %in% names(ggproto_formals(self$map))) {
476+
if (is.null(i)) {
477+
lapply(aesthetics, function(j) self$map(df[[j]], scale_params = scale_params[[j]]))
478+
} else {
479+
lapply(aesthetics, function(j) self$map(df[[j]][i], scale_params = scale_params[[j]]))
480+
}
474481
} else {
475-
lapply(aesthetics, function(j) self$map(df[[j]][i]))
482+
# Eventually warn if self$map() does not accept scale_params
483+
if (is.null(i)) {
484+
lapply(aesthetics, function(j) self$map(df[[j]]))
485+
} else {
486+
lapply(aesthetics, function(j) self$map(df[[j]][i]))
487+
}
488+
476489
}
477490
},
478491

479-
map = function(self, x, limits = self$get_limits()) {
492+
map = function(self, x, limits = self$get_limits(), scale_params = NULL) {
480493
cli::cli_abort("Not implemented")
481494
},
482495

R/scales-.r

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,23 @@ scales_train_df <- function(scales, df, drop = FALSE) {
7070
}
7171

7272
# Map values from a data.frame. Returns data.frame
73-
scales_map_df <- function(scales, df) {
73+
scales_map_df <- function(scales, df, scale_params = NULL) {
7474
if (empty(df) || length(scales$scales) == 0) return(df)
7575

76-
mapped <- unlist(lapply(scales$scales, function(scale) scale$map_df(df = df)), recursive = FALSE)
76+
mapped <- unlist(
77+
lapply(
78+
scales$scales,
79+
function(scale) {
80+
if ("scale_params" %in% names(ggproto_formals(self$map_df))) {
81+
scale$map_df(df = df, scale_params = scale_params)
82+
} else {
83+
# Eventually warn if scale$map_df() does not accept scale_params
84+
scale$map_df(df = df)
85+
}
86+
}
87+
),
88+
recursive = FALSE
89+
)
7790

7891
data_frame0(!!!mapped, df[setdiff(names(df), names(mapped))])
7992
}

0 commit comments

Comments
 (0)