Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boilerplate functions #6143

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5f067c1
write boilerplate function
teunbrand Oct 16, 2024
32a4a76
adopt boilerplate where possible
teunbrand Oct 16, 2024
3eee99f
write out all non-standard arguments
teunbrand Oct 17, 2024
26f9f8f
automatically fill in parameters
teunbrand Oct 17, 2024
8c8f795
incorporate small checks
teunbrand Oct 17, 2024
fa9f6d2
document
teunbrand Oct 17, 2024
075ea86
accept visual snapshots
teunbrand Oct 17, 2024
ea57200
Update R/boilerplates.R
teunbrand Oct 18, 2024
6b44f33
Adopt advice from June
teunbrand Oct 18, 2024
6e71e1a
docfix
teunbrand Oct 18, 2024
d488de8
Apply suggestions from code review
teunbrand Oct 18, 2024
e6db72d
ensure `list2()` can be found
teunbrand Oct 21, 2024
8c33474
rename `boilerplate()` to `make_constructor()`
teunbrand Oct 22, 2024
c11c5ed
give body pretty braces
teunbrand Oct 22, 2024
44cf2dc
purge unused parameters in `stat_bin()`
teunbrand Oct 22, 2024
cd54bfb
add Stat method for `make_constructor()`
teunbrand Nov 28, 2024
4dd78b5
use the `make_constructor()` for Stats
teunbrand Nov 28, 2024
c7abdd7
revert 4dd78b5c for `layer_sf()` stats
teunbrand Nov 28, 2024
d1043b0
mechanism to hide internal variables
teunbrand Nov 28, 2024
a68e4bb
document
teunbrand Nov 28, 2024
6f452f4
resolve merge conflict
teunbrand Nov 28, 2024
6c95f24
make GeomCol parent of `geom_col()` again for error message purposes
teunbrand Nov 28, 2024
52e66f2
rebalance omissions
teunbrand Nov 28, 2024
295e36c
resolve merge conflict
teunbrand Feb 4, 2025
c7aa7fd
include example of `checks` argument
teunbrand Feb 4, 2025
23f4c71
Hide origin/right arguments that are deprecated
teunbrand Feb 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ensure list2() can be found
  • Loading branch information
teunbrand committed Oct 21, 2024
commit e6db72d6426b02b869622eb4ba4839c31391d0c4
5 changes: 4 additions & 1 deletion R/boilerplates.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#' p + geom_test()
#' p + geom_test(grob = grid::circleGrob())
boilerplate <- function(x, ...) {
UseMethod("boilerplate")

Check warning on line 41 in R/boilerplates.R

View check run for this annotation

Codecov / codecov/patch

R/boilerplates.R#L41

Added line #L41 was not covered by tests
}

#' @export
Expand All @@ -46,77 +46,80 @@
boilerplate.Geom <- function(x, ..., checks = NULL, env = caller_env()) {

# Check that we can independently find the geom
geom <- gsub("^geom_", "", snake_class(x))
check_subclass(geom, "Geom", env = env)

Check warning on line 50 in R/boilerplates.R

View check run for this annotation

Codecov / codecov/patch

R/boilerplates.R#L49-L50

Added lines #L49 - L50 were not covered by tests

# Split additional arguments into required and extra ones
args <- enexprs(...)
fixed_fmls_names <- c("mapping", "data", "stat", "position", "...",
"na.rm", "show.legend", "inherit.aes")
extra_args <- setdiff(names(args), fixed_fmls_names)
if ("geom" %in% extra_args) {
cli::cli_abort("{.arg geom} is a reserved argument.")

Check warning on line 58 in R/boilerplates.R

View check run for this annotation

Codecov / codecov/patch

R/boilerplates.R#L53-L58

Added lines #L53 - L58 were not covered by tests
}

# Fill in values for parameters from draw functions
known_params <-
unique(c(names(args), fixed_fmls_names, "flipped_aes", x$aesthetics()))
missing_params <- setdiff(x$parameters(), known_params)
if (length(missing_params) > 0) {
draw_args <- ggproto_formals(x$draw_panel)
if ("..." %in% names(draw_args)) {
draw_args <- ggproto_formals(x$draw_group)

Check warning on line 68 in R/boilerplates.R

View check run for this annotation

Codecov / codecov/patch

R/boilerplates.R#L62-L68

Added lines #L62 - L68 were not covered by tests
}
params <- intersect(missing_params, names(draw_args))
extra_args <- c(extra_args, params)
for (param in params) {
if (!identical(draw_args[[param]], quote(expr = ))) {
args[param] <- draw_args[param]

Check warning on line 74 in R/boilerplates.R

View check run for this annotation

Codecov / codecov/patch

R/boilerplates.R#L70-L74

Added lines #L70 - L74 were not covered by tests
}
}
missing_params <- setdiff(missing_params, names(args))
if (length(missing_params) > 0) {
cli::cli_warn(
"In {.fn geom_{geom}}: please consider providing default values for: \\
{missing_params}."
)

Check warning on line 82 in R/boilerplates.R

View check run for this annotation

Codecov / codecov/patch

R/boilerplates.R#L77-L82

Added lines #L77 - L82 were not covered by tests
}
}

# Build function formals
fmls <- pairlist2(
mapping = args$mapping,
data = args$data,
stat = args$stat %||% "identity",
position = args$position %||% "identity",
`...` = missing_arg(),
!!!args[extra_args],
na.rm = args$na.rm %||% FALSE,
show.legend = args$show.legend %||% NA,
inherit.aes = args$inherit.aes %||% TRUE
)

Check warning on line 97 in R/boilerplates.R

View check run for this annotation

Codecov / codecov/patch

R/boilerplates.R#L87-L97

Added lines #L87 - L97 were not covered by tests

# Construct call for the 'layer(params)' argument
params <- exprs(!!!syms(c("na.rm", extra_args)), .named = TRUE)
params <- call2("list2", !!!params, quote(...))

Check warning on line 101 in R/boilerplates.R

View check run for this annotation

Codecov / codecov/patch

R/boilerplates.R#L100-L101

Added lines #L100 - L101 were not covered by tests

# Construct rest of 'layer()' call
layer_args <- syms(setdiff(fixed_fmls_names, c("...", "na.rm")))
layer_args <- append(layer_args, list(geom = geom), after = 2)
layer_args <- exprs(!!!layer_args, params = !!params, .named = TRUE)
body <- call2("layer", !!!layer_args)

Check warning on line 107 in R/boilerplates.R

View check run for this annotation

Codecov / codecov/patch

R/boilerplates.R#L104-L107

Added lines #L104 - L107 were not covered by tests

# Prepend any checks
if (!missing(checks)) {
lang <- vapply(checks, is_call, logical(1))
if (!all(lang)) {
cli::cli_abort(
"{.arg checks} must be a list of calls, such as one constructed \\
with {.fn rlang::exprs}."
)

Check warning on line 116 in R/boilerplates.R

View check run for this annotation

Codecov / codecov/patch

R/boilerplates.R#L110-L116

Added lines #L110 - L116 were not covered by tests
}
body <- call2("{", !!!checks, body)

Check warning on line 118 in R/boilerplates.R

View check run for this annotation

Codecov / codecov/patch

R/boilerplates.R#L118

Added line #L118 was not covered by tests
}

new_function(fmls, body, env = caller_env())
# We encapsulate rlang::list2
new_env <- new_environment(list(list2 = list2), env)

Check warning on line 122 in R/boilerplates.R

View check run for this annotation

Codecov / codecov/patch

R/boilerplates.R#L122

Added line #L122 was not covered by tests

new_function(fmls, body, env = new_env)

Check warning on line 124 in R/boilerplates.R

View check run for this annotation

Codecov / codecov/patch

R/boilerplates.R#L124

Added line #L124 was not covered by tests
}
Loading