Skip to content

Commit 6b44f33

Browse files
committed
Adopt advice from June
1 parent ea57200 commit 6b44f33

File tree

6 files changed

+33
-42
lines changed

6 files changed

+33
-42
lines changed

R/boilerplates.R

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ NULL
1010
#' @param x An object to setup a constructor for.
1111
#' @param ... Name-value pairs to use as additional arguments in the
1212
#' constructor. For layers, these are passed on to [`layer(params)`][layer()].
13-
#' @param checks Expressions evaluated before construction of the object.
14-
#' Can be a `{}` block to include multiple expressions.
13+
#' @param checks A list of calls to be evaluated before construction of the
14+
#' object, such as one constructed with [`exprs()`][rlang::exprs()].
1515
#'
1616
#' @return A function
1717
#' @export
@@ -40,7 +40,7 @@ boilerplate <- function(x, ...) {
4040
}
4141

4242
#' @export
43-
boilerplate.Geom <- function(x, ..., checks, env = caller_env()) {
43+
boilerplate.Geom <- function(x, ..., checks = NULL, env = caller_env()) {
4444

4545
# Check that we can independently find the geom
4646
geom <- gsub("^geom_", "", snake_class(x))
@@ -81,46 +81,36 @@ boilerplate.Geom <- function(x, ..., checks, env = caller_env()) {
8181
}
8282

8383
# Build function formals
84-
fmls <- rlang::pairlist2(
84+
fmls <- pairlist2(
8585
mapping = args$mapping,
8686
data = args$data,
8787
stat = args$stat %||% "identity",
8888
position = args$position %||% "identity",
89-
`...` = rlang::missing_arg(),
89+
`...` = missing_arg(),
9090
!!!args[extra_args],
9191
na.rm = args$na.rm %||% FALSE,
9292
show.legend = args$show.legend %||% NA,
9393
inherit.aes = args$inherit.aes %||% TRUE
9494
)
9595

96-
if (length(extra_args) > 0) {
97-
extra_args <- paste0(
98-
"\n ", extra_args, " = ", extra_args, ",", collapse = ""
99-
)
100-
}
96+
# Construct call for the 'layer(params)' argument
97+
params <- exprs(!!!syms(c("na.rm", extra_args)), .named = TRUE)
98+
params <- call2("list2", !!!params, quote(...))
10199

102-
body <- paste0("
103-
layer(
104-
data = data,
105-
mapping = mapping,
106-
stat = stat,
107-
geom = \"", geom, "\",
108-
position = position,
109-
show.legend = show.legend,
110-
inherit.aes = inherit.aes,
111-
params = list2(
112-
na.rm = na.rm,",
113-
extra_args, "
114-
...
115-
)
116-
)
117-
")
118-
body <- str2lang(body)
100+
# Construct rest of 'layer()' call
101+
layer_args <- syms(setdiff(fixed_fmls_names, c("...", "na.rm")))
102+
layer_args <- append(layer_args, list(geom = geom), after = 2)
103+
layer_args <- exprs(!!!layer_args, params = !!params, .named = TRUE)
104+
body <- call2("layer", !!!layer_args)
119105

120-
checks <- substitute(checks)
106+
# Prepend any checks
121107
if (!missing(checks)) {
122-
if (is_call(checks, "{")) {
123-
checks[[1]] <- NULL
108+
lang <- vapply(checks, is_call, logical(1))
109+
if (!all(lang)) {
110+
cli::cli_abort(
111+
"{.arg checks} must be a list of calls, such as one constructed \\
112+
with {.fn rlang::exprs}."
113+
)
124114
}
125115
body <- inject(quote(`{`(!!!c(checks, body))))
126116
}

R/geom-density.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ GeomDensity <- ggproto(
7474
#' }
7575
geom_density <- boilerplate(
7676
GeomDensity, stat = "density",
77-
checks = {
77+
checks = exprs(
7878
outline.type <- arg_match0(outline.type, c("both", "upper", "lower", "full"))
79-
}
79+
)
8080
)
81+

R/geom-function.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,5 @@ GeomFunction <- ggproto("GeomFunction", GeomPath,
8787
#' @export
8888
geom_function <- boilerplate(
8989
GeomFunction, stat = "function",
90-
checks = {data <- data %||% ensure_nonempty_data}
90+
checks = exprs(data <- data %||% ensure_nonempty_data)
9191
)

R/geom-raster.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ GeomRaster <- ggproto("GeomRaster", Geom,
9898
#' (the default) don't interpolate.
9999
geom_raster <- boilerplate(
100100
GeomRaster,
101-
checks = {
102-
check_number_decimal(hjust)
101+
checks = exprs(
102+
check_number_decimal(hjust),
103103
check_number_decimal(vjust)
104-
}
104+
)
105105
)

R/geom-ribbon.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,17 +272,17 @@ GeomArea <- ggproto("GeomArea", GeomRibbon,
272272
#'
273273
geom_ribbon <- boilerplate(
274274
GeomRibbon, orientation = NA,
275-
checks = {
275+
checks = exprs(
276276
outline.type <- arg_match0(outline.type, c("both", "upper", "lower", "full"))
277-
}
277+
)
278278
)
279279

280280
#' @rdname geom_ribbon
281281
#' @export
282282
geom_area <- boilerplate(
283283
GeomArea, stat = "align", position = "stack",
284284
orientation = NA, outline.type = "upper",
285-
checks = {
285+
checks = exprs(
286286
outline.type <- arg_match0(outline.type, c("both", "upper", "lower", "full"))
287-
}
287+
)
288288
)

man/boilerplate.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)