|
| 1 | +#' @include layer.R |
| 2 | +#' @include scale-type.R |
| 3 | +NULL |
| 4 | + |
| 5 | +#' @export |
| 6 | +boilerplate <- function(x, ...) { |
| 7 | + UseMethod("boilerplate") |
| 8 | +} |
| 9 | + |
| 10 | +#' @export |
| 11 | +boilerplate.Geom <- function(x, ..., env = caller_env()) { |
| 12 | + |
| 13 | + # Check that we can independently find the geom |
| 14 | + geom <- gsub("^geom_", "", snake_class(x)) |
| 15 | + check_subclass(geom, "Geom", env = env) |
| 16 | + |
| 17 | + # Split additional arguments into required and extra ones |
| 18 | + args <- enexprs(...) |
| 19 | + fixed_fmls_names <- c("mapping", "data", "stat", "position", "...", |
| 20 | + "na.rm", "show.legend", "inherit.aes") |
| 21 | + extra_args <- setdiff(names(args), fixed_fmls_names) |
| 22 | + if ("geom" %in% extra_args) { |
| 23 | + cli::cli_abort("{.arg geom} is a reserved argument.") |
| 24 | + } |
| 25 | + |
| 26 | + # Build function formals |
| 27 | + fmls <- list2( |
| 28 | + mapping = args$mapping, |
| 29 | + data = args$data, |
| 30 | + stat = args$stat %||% "identity", |
| 31 | + position = args$position %||% "identity", |
| 32 | + `...` = quote(expr = ), |
| 33 | + !!!args[extra_args], |
| 34 | + na.rm = args$na.rm %||% FALSE, |
| 35 | + show.legend = args$show.legend %||% NA, |
| 36 | + inherit.aes = args$inherit.aes %||% TRUE |
| 37 | + ) |
| 38 | + |
| 39 | + if (length(extra_args) > 0) { |
| 40 | + extra_args <- paste0( |
| 41 | + "\n ", extra_args, " = ", extra_args, ",", collapse = "" |
| 42 | + ) |
| 43 | + } |
| 44 | + |
| 45 | + body <- paste0(" |
| 46 | + layer( |
| 47 | + data = data, |
| 48 | + mapping = mapping, |
| 49 | + stat = stat, |
| 50 | + geom = \"", geom, "\", |
| 51 | + position = position, |
| 52 | + show.legend = show.legend, |
| 53 | + inherit.aes = inherit.aes, |
| 54 | + params = list2( |
| 55 | + na.rm = na.rm,", |
| 56 | + extra_args, " |
| 57 | + ... |
| 58 | + ) |
| 59 | + ) |
| 60 | + ") |
| 61 | + body <- as.call(parse(text = body))[[1]] |
| 62 | + |
| 63 | + new_function(fmls, body) |
| 64 | +} |
0 commit comments