Skip to contents

Inspect the arguments passed into a method

Usage

ggtrace_inspect_args(x, method, cond = 1L, hoist_dots = TRUE, error = FALSE)

Arguments

x

A ggplot object

method

A function or a ggproto method. The ggproto method may be specified using any of the following forms:

  • ggproto$method

  • namespace::ggproto$method

  • namespace:::ggproto$method

cond

When the arguments should be inspected. Defaults to 1L.

hoist_dots

Whether treat arguments passed to ... like regular arguments. If FALSE, the ... is treated as an argument

error

If TRUE, continues inspecting the method until the ggplot errors. This is useful for debugging but note that it can sometimes return incomplete output.

Value

A list of argument-value pairs from the method when it is called.

Tracing context

When quoted expressions are passed to the cond or value argument of workflow functions they are evaluated in a special environment which we call the "tracing context".

The tracing context is "data-masked" (see rlang::eval_tidy()), and exposes an internal variable called ._counter_ which increments every time a function/method has been called by the ggplot object supplied to the x argument of workflow functions. For example, cond = quote(._counter_ == 1L) is evaluated as TRUE when the method is called for the first time. The cond argument also supports numeric shorthands like cond = 1L which evaluates to quote(._counter_ == 1L), and this is the default value of cond for all workflow functions that only return one value (e.g., ggtrace_capture_fn()). It is recommended to consult the output of ggtrace_inspect_n() and ggtrace_inspect_which() to construct expressions that condition on ._counter_.

For highjack functions like ggtrace_highjack_return(), the value about to be returned by the function/method can be accessed with returnValue() in the value argument. By default, value is set to quote(returnValue()) which simply evaluates to the return value, but directly computing on returnValue() to derive a different return value for the function/method is also possible.

Examples


library(ggplot2)

p1 <- ggplot(diamonds, aes(cut)) +
  geom_bar(aes(fill = cut)) +
  facet_wrap(~ clarity)

p1


# Argument value of `Stat$compute_panel` for the first panel
compute_panel_args_1 <- ggtrace_inspect_args(x = p1, method = Stat$compute_panel)
names(ggformals(Stat$compute_panel))
#> [1] "self"   "data"   "scales" "..."   
names(compute_panel_args_1)
#> [1] "self"        "data"        "scales"      "width"       "flipped_aes"
table(compute_panel_args_1$data$fill)
#> 
#>      Fair      Good Very Good   Premium     Ideal 
#>       210        96        84       205       146 

# `hoist_dots` preserves information about which arguments were passed to `...`
with_dots <- ggtrace_inspect_args(p1, Stat$compute_panel, hoist_dots = FALSE)
names(with_dots)
#> [1] "self"   "data"   "scales" "..."   
with_dots$`...`
#> $width
#> [1] 0.9
#> 
#> $flipped_aes
#> [1] FALSE
#>