Skip to content

Debugger for ggproto methods #5723

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

Merged
merged 1 commit into from
May 20, 2024
Merged
Changes from all commits
Commits
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
35 changes: 35 additions & 0 deletions R/ggproto.R
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,38 @@ format.ggproto_method <- function(x, ...) {

# proto2 TODO: better way of getting formals for self$draw
ggproto_formals <- function(x) formals(environment(x)$f)

#' Debug wrapper for ggproto methods
#'
#' @param method A ggproto method or function to debug.
#' @param debug One of the following:
#' * `"once"` for invoking `debugonce()` (default).
#' * `"always"` for invoking `debug()`.
#' * `"never"` for invoking `undebug()`.
#' @param ... Arguments passed to the function invoked by the `debug` argument.
#'
#' @return `NULL`, this function is called for its side-effects.
#' @noRd
#'
#' @examples
#' p <- ggplot(mpg, aes(displ, hwy)) +
#' geom_point()
#'
#' if (interactive()) {
#' ggproto_debug(GeomPoint$draw_panel)
#' }
#'
#' p
ggproto_debug <- function(method, debug = c("once", "always", "never"), ...) {
if (inherits(method, "ggproto_method")) {
method <- environment(method)$f
}
check_function(method)
switch(
arg_match0(debug, c("once", "always", "never")),
once = debugonce(method, ...),
always = debug(method, ...),
never = undebug(method, ...)
)
}