Skip to contents

Inspect the return value of a method

Usage

ggtrace_inspect_return(x, method, cond = 1L, ..., error = FALSE)

inspect_return(x, method, cond = 1L, ..., 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 return value should be inspected. Defaults to 1L.

...

Unused.

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

The return value from 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


# Return value of `Stat$compute_panel` for the first panel
ggtrace_inspect_return(x = p1, method = Stat$compute_panel)
#>   count prop x width flipped_aes      fill PANEL group
#> 1   210    1 1   0.9       FALSE      Fair     1     1
#> 2    96    1 2   0.9       FALSE      Good     1     2
#> 3    84    1 3   0.9       FALSE Very Good     1     3
#> 4   205    1 4   0.9       FALSE   Premium     1     4
#> 5   146    1 5   0.9       FALSE     Ideal     1     5

# Return value for 4th panel
ggtrace_inspect_return(x = p1, method = Stat$compute_panel,
                       cond = 4L)
#>   count prop x width flipped_aes      fill PANEL group
#> 1   261    1 1   0.9       FALSE      Fair     4     1
#> 2   978    1 2   0.9       FALSE      Good     4     2
#> 3  2591    1 3   0.9       FALSE Very Good     4     3
#> 4  3357    1 4   0.9       FALSE   Premium     4     4
#> 5  5071    1 5   0.9       FALSE     Ideal     4     5

# Return value for 4th panel, 2nd group (bar)
ggtrace_inspect_return(
  x = p1, method = StatCount$compute_group,
  cond = quote(data$PANEL[1] == 4 && data$group[1] == 2)
)
#>   count prop x width flipped_aes
#> 1   978    1 2   0.9       FALSE