Skip to content

Commit 99b7c60

Browse files
committed
FEAT/PERF: ScaleContinuous accepts palette capability may_return_na
We can extend palettes with attributes to improve the mapping efficiency. With this commit a palette may define an attribute `may_return_na` to `FALSE`. If it does, `ScaleContinuous` will assume the palette may not return missing values, and it will skip checking for those and replacing them.
1 parent 549fae9 commit 99b7c60

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

R/scale-.r

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,14 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
609609
pal <- self$palette(uniq)
610610
scaled <- pal[match(x, uniq)]
611611

612-
ifelse(!is.na(scaled), scaled, self$na.value)
612+
# A specific palette can have as attribute "may_return_NA = FALSE"
613+
# If it has such attribute, we will skip the ifelse(!is.na(scaled), ...)
614+
pal_may_return_na <- ggproto_attr(self$palette, "may_return_NA", default = TRUE)
615+
if (pal_may_return_na) {
616+
scaled <- ifelse(!is.na(scaled), scaled, self$na.value)
617+
}
618+
619+
scaled
613620
},
614621

615622
rescale = function(self, x, limits = self$get_limits(), range = limits) {

0 commit comments

Comments
 (0)