Skip to content

Commit 5f067c1

Browse files
committed
write boilerplate function
1 parent ddd207e commit 5f067c1

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ Collate:
108108
'backports.R'
109109
'bench.R'
110110
'bin.R'
111+
'scale-type.R'
112+
'layer.R'
113+
'boilerplates.R'
111114
'coord-.R'
112115
'coord-cartesian-.R'
113116
'coord-fixed.R'
@@ -186,7 +189,6 @@ Collate:
186189
'guide-colorbar.R'
187190
'guide-colorsteps.R'
188191
'guide-custom.R'
189-
'layer.R'
190192
'guide-none.R'
191193
'guide-old.R'
192194
'guides-.R'
@@ -236,7 +238,6 @@ Collate:
236238
'scale-shape.R'
237239
'scale-size.R'
238240
'scale-steps.R'
239-
'scale-type.R'
240241
'scale-view.R'
241242
'scale-viridis.R'
242243
'scales-.R'

R/boilerplates.R

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)