Skip to content

Commit f3a6fde

Browse files
committed
update mechanism
1 parent 8a5b03c commit f3a6fde

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

R/scale-.R

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -463,40 +463,40 @@ Scale <- ggproto("Scale", NULL,
463463
title
464464
},
465465

466-
new = function(self, aesthetics, palette, name = waiver(), breaks = waiver(),
466+
new = function(self, aesthetics = NULL, breaks = waiver(),
467467
minor_breaks = waiver(), labels = waiver(), limits = NULL,
468-
expand = waiver(), guide = "legend", position = "left",
468+
guide = NULL, position = NULL,
469469
call = caller_call(), ..., super = NULL) {
470470

471-
call <- call %||% current_call()
472-
aesthetics <- standardise_aes_names(aesthetics)
471+
super <- super %||% self
472+
call <- call %||% super$call() %||% current_call()
473+
aesthetics <- standardise_aes_names(aesthetics %||% super$aesthetics)
474+
limits <- allow_lambda(limits %||% super$limits)
475+
breaks <- allow_lambda(breaks %|W|% super$breaks)
476+
labels <- allow_lambda(labels %|W|% super$labels)
477+
minor_breaks <- allow_lambda(minor_breaks %|W|% super$minor_breaks)
473478
check_breaks_labels(breaks, labels, call = call)
474-
limits <- allow_lambda(limits)
475-
breaks <- allow_lambda(breaks)
476-
labels <- allow_lambda(labels)
477-
minor_breaks <- allow_lambda(minor_breaks)
478-
position <- arg_match0(position, .trbl)
479+
position <- arg_match0(position %||% super$position, .trbl)
479480
if (is.null(breaks) & all(!is_position_aes(aesthetics))) {
480481
guide <- "none"
481482
}
482483

483-
super <- super %||% self
484484
ggproto(
485485
NULL, super,
486486
call = call,
487487
aesthetics = aesthetics,
488-
palette = palette,
489488
limits = limits,
490-
expand = expand,
491-
name = name,
492489
breaks = breaks,
493490
minor_breaks = minor_breaks,
494491
labels = labels,
495-
guide = guide,
492+
guide = guide %||% super$guide,
496493
position = position,
497494
...
498495
)
496+
},
499497

498+
update = function(self, params) {
499+
inject(self$new(!!!params))
500500
}
501501
)
502502

@@ -811,18 +811,19 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
811811
}
812812
},
813813

814-
new = function(self, rescaler = rescale, oob = censor,
814+
new = function(self, rescaler = NULL, oob = NULL,
815815
range = ContinuousRange$new(),
816-
transform = "identity", limits = NULL, ...,
816+
transform = NULL, limits = NULL, ...,
817817
super = NULL) {
818-
819-
transform <- as.transform(transform)
820-
if (!is.null(limits) && !is.function(limits) && !is.formula(limits)) {
821-
limits = transform$transform(limits)
818+
super <- super %||% self
819+
transform <- as.transform(transform %||% super$trans)
820+
limits <- allow_lambda(limits %||% super$limits)
821+
if (!is.null(limits) && !is.function(limits)) {
822+
limits <- transform$transform(limits)
822823
}
823824

824-
rescaler <- allow_lambda(rescaler)
825-
oob <- allow_lambda(oob)
825+
rescaler <- allow_lambda(rescaler %||% super$rescaler)
826+
oob <- allow_lambda(oob %||% super$oob)
826827

827828
ggproto_parent(Scale, self)$new(
828829
rescaler = rescaler,
@@ -831,8 +832,20 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
831832
trans = transform,
832833
limits = limits,
833834
...,
834-
super = super %||% self
835+
super = super
835836
)
837+
},
838+
839+
update = function(self, params) {
840+
# We may need to update limits when previously transformed and
841+
# a new transformation is coming in
842+
if ("transform" %in% names(params) &&
843+
self$trans$name != "identity" &&
844+
(!"limits" %in% names(params)) &&
845+
!is.null(self$limits) && !is.function(self$limits)) {
846+
params$limits <- self$trans$inverse(self$limits)
847+
}
848+
inject(self$new(!!!params))
836849
}
837850
)
838851

R/scales-.R

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ scales_list <- function() {
88

99
ScalesList <- ggproto("ScalesList", NULL,
1010
scales = NULL,
11+
params = list(),
1112

1213
find = function(self, aesthetic) {
1314
vapply(self$scales, function(x) any(aesthetic %in% x$aesthetics), logical(1))
@@ -21,7 +22,10 @@ ScalesList <- ggproto("ScalesList", NULL,
2122
if (is.null(scale)) {
2223
return()
2324
}
24-
25+
aes <- intersect(scale$aesthetics, names(self$params))
26+
for (i in aes) {
27+
scale <- scale$update(self$params[[aes]])
28+
}
2529
prev_aes <- self$find(scale$aesthetics)
2630
if (any(prev_aes)) {
2731
# Get only the first aesthetic name in the returned vector -- it can
@@ -168,6 +172,20 @@ ScalesList <- ggproto("ScalesList", NULL,
168172
scale_name <- paste("scale", aes, "continuous", sep = "_")
169173
self$add(find_global(scale_name, env, mode = "function")())
170174
}
175+
},
176+
177+
add_params = function(self, aesthetic, params = NULL) {
178+
if (is.null(params) || is.null(aesthetic)) {
179+
return()
180+
}
181+
index <- which(self$find(aesthetic))
182+
if (length(index) > 0) {
183+
for (i in index) {
184+
self$scales[[i]] <- self$scales[[i]]$update(params)
185+
}
186+
} else {
187+
self$params[[aesthetic]] <- defaults(params, self$params[[aesthetic]])
188+
}
171189
}
172190
)
173191

0 commit comments

Comments
 (0)