Skip to content

Commit b6748c2

Browse files
committed
protect against invalid arguments
1 parent 47a6b57 commit b6748c2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

R/scale-.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ Scale <- ggproto("Scale", NULL,
522522
},
523523

524524
update = function(self, params) {
525+
check_update_params(self, params)
525526
inject(self$new(!!!params))
526527
}
527528
)
@@ -546,6 +547,29 @@ check_breaks_labels <- function(breaks, labels, call = NULL) {
546547
TRUE
547548
}
548549

550+
check_update_params <- function(scale, params) {
551+
if (inherits(scale, "ScaleContinuous")) {
552+
args <- fn_fmls_names(continuous_scale)
553+
} else if (inherits(scale, "ScaleDiscrete")) {
554+
args <- fn_fmls_names(discrete_scale)
555+
} else if (inherits(scale, "ScaleBinned")) {
556+
args <- fn_fmls_names(binned_scale)
557+
} else {
558+
# We don't know what valid parameters are of custom scale types
559+
return(invisible(NULL))
560+
}
561+
extra <- setdiff(names(params), args)
562+
if (length(extra) == 0) {
563+
return(invisible(NULL))
564+
}
565+
extra <- paste0("{.val ", extra, "}")
566+
names(extra) <- rep("*", length(extra))
567+
cli::cli_abort(
568+
c("Cannot update scale with the unknown {cli::qty(extra)} argument{?s}:", extra),
569+
call = scale$call
570+
)
571+
}
572+
549573
default_transform <- function(self, x) {
550574
transformation <- self$get_transformation()
551575
new_x <- transformation$transform(x)
@@ -865,6 +889,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
865889
},
866890

867891
update = function(self, params) {
892+
check_update_params(self, params)
868893
# We may need to update limits when previously transformed and
869894
# a new transformation is coming in
870895
if ("transform" %in% names(params) &&

0 commit comments

Comments
 (0)