|
10 | 10 | #' @param x An object to setup a constructor for. |
11 | 11 | #' @param ... Name-value pairs to use as additional arguments in the |
12 | 12 | #' 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()]. |
15 | 15 | #' |
16 | 16 | #' @return A function |
17 | 17 | #' @export |
@@ -40,7 +40,7 @@ boilerplate <- function(x, ...) { |
40 | 40 | } |
41 | 41 |
|
42 | 42 | #' @export |
43 | | -boilerplate.Geom <- function(x, ..., checks, env = caller_env()) { |
| 43 | +boilerplate.Geom <- function(x, ..., checks = NULL, env = caller_env()) { |
44 | 44 |
|
45 | 45 | # Check that we can independently find the geom |
46 | 46 | geom <- gsub("^geom_", "", snake_class(x)) |
@@ -81,46 +81,36 @@ boilerplate.Geom <- function(x, ..., checks, env = caller_env()) { |
81 | 81 | } |
82 | 82 |
|
83 | 83 | # Build function formals |
84 | | - fmls <- rlang::pairlist2( |
| 84 | + fmls <- pairlist2( |
85 | 85 | mapping = args$mapping, |
86 | 86 | data = args$data, |
87 | 87 | stat = args$stat %||% "identity", |
88 | 88 | position = args$position %||% "identity", |
89 | | - `...` = rlang::missing_arg(), |
| 89 | + `...` = missing_arg(), |
90 | 90 | !!!args[extra_args], |
91 | 91 | na.rm = args$na.rm %||% FALSE, |
92 | 92 | show.legend = args$show.legend %||% NA, |
93 | 93 | inherit.aes = args$inherit.aes %||% TRUE |
94 | 94 | ) |
95 | 95 |
|
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(...)) |
101 | 99 |
|
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) |
119 | 105 |
|
120 | | - checks <- substitute(checks) |
| 106 | + # Prepend any checks |
121 | 107 | 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 | + ) |
124 | 114 | } |
125 | 115 | body <- inject(quote(`{`(!!!c(checks, body)))) |
126 | 116 | } |
|
0 commit comments