Skip to contents

whence_rows() finds rows of the source data. With no step, it gives the rows that contribute to the value: for sales |> filter(...) |> summarise(total = sum(x)) these are the rows of sales that the filters keep. With step = k, it gives the rows that step k removed. The steps are numbered as in the step column of as.data.frame(trace), where the source is step 0.

Usage

whence_rows(trace, n = NULL, step = NULL)

Arguments

trace

A whence_trace from whence_trace() or whence_table(), or a group from whence_group().

n

The largest number of rows to give, or NULL for all of them.

step

NULL for the rows that make the value, or the number of a step for the rows that it removed.

Value

Rows of the source data, in their original order, with the columns of the source. NULL, with a warning of class whence_rows_unavailable, when the rows cannot be found.

Details

For a group from whence_group(), both are limited to the rows of that group.

The function replays the pipeline with a hidden id column on a copy of the source. whence_trace() never does this, so the waterfall costs nothing extra. The data that you give is never changed.

Some steps destroy the link between a row and the source: distinct(), pivot_longer(), pivot_wider(), and each step that is not a known verb. After such a step the function gives NULL with a warning that names the step. The rows that an earlier step removed can still be found. The waterfall of the trace is always complete, because it uses counts only.

Examples

library(dplyr, warn.conflicts = FALSE)

trace <- whence_trace(
  mtcars |>
    filter(cyl == 4) |>
    filter(mpg > 30) |>
    summarise(mean_hp = mean(hp))
)
whence_rows(trace)
#>                 mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1
#> Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
#> Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
#> Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2

# The cars that the second filter removed
whence_rows(trace, step = 2)
#>                mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Datsun 710    22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> Merc 240D     24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#> Merc 230      22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#> Toyota Corona 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
#> Fiat X1-9     27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
#> Porsche 914-2 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
#> Volvo 142E    21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2