thinkr::peep(): See intermediate stages of piped dfs

dataframes
piping
Author
Published

June 1, 2023

Setup
library(tidyverse)
library(thinkr)

Insert thinkr::peep(head) into a df pipeline to print a few rows at a particular intermediate stage in the pipeline. Here it prints a few lines of the df before it gets summarized:

palmerpenguins::penguins |> 
  peep(head) |>
  group_by(species) |>
  summarize(across(where(is.numeric), \(x) mean(x, na.rm = TRUE)))
1
How convenient that the base R pipe looks like a penguin.
# A tibble: 6 × 8
  species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
  <fct>   <fct>              <dbl>         <dbl>             <int>       <int>
1 Adelie  Torgersen           39.1          18.7               181        3750
2 Adelie  Torgersen           39.5          17.4               186        3800
3 Adelie  Torgersen           40.3          18                 195        3250
4 Adelie  Torgersen           NA            NA                  NA          NA
5 Adelie  Torgersen           36.7          19.3               193        3450
6 Adelie  Torgersen           39.3          20.6               190        3650
# ℹ 2 more variables: sex <fct>, year <int>
species bill_length_mm bill_depth_mm flipper_length_mm body_mass_g year
Adelie 38.79139 18.34636 189.9536 3700.662 2008.013
Chinstrap 48.83382 18.42059 195.8235 3733.088 2007.971
Gentoo 47.50488 14.98211 217.1870 5076.016 2008.081

You can use multiple functions, like head and tail to see two views of the intermediate step; verbose = TRUE prints the functions used for these peeps:

palmerpenguins::penguins |>
  peep(head, tail, verbose = TRUE) |> 
  group_by(species) |> 
  summarize(across(where(is.numeric), \(x) mean(x, na.rm = TRUE)))
head(.)
# A tibble: 6 × 8
  species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
  <fct>   <fct>              <dbl>         <dbl>             <int>       <int>
1 Adelie  Torgersen           39.1          18.7               181        3750
2 Adelie  Torgersen           39.5          17.4               186        3800
3 Adelie  Torgersen           40.3          18                 195        3250
4 Adelie  Torgersen           NA            NA                  NA          NA
5 Adelie  Torgersen           36.7          19.3               193        3450
6 Adelie  Torgersen           39.3          20.6               190        3650
# ℹ 2 more variables: sex <fct>, year <int>
tail(.)
# A tibble: 6 × 8
  species   island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
  <fct>     <fct>           <dbl>         <dbl>             <int>       <int>
1 Chinstrap Dream            45.7          17                 195        3650
2 Chinstrap Dream            55.8          19.8               207        4000
3 Chinstrap Dream            43.5          18.1               202        3400
4 Chinstrap Dream            49.6          18.2               193        3775
5 Chinstrap Dream            50.8          19                 210        4100
6 Chinstrap Dream            50.2          18.7               198        3775
# ℹ 2 more variables: sex <fct>, year <int>
species bill_length_mm bill_depth_mm flipper_length_mm body_mass_g year
Adelie 38.79139 18.34636 189.9536 3700.662 2008.013
Chinstrap 48.83382 18.42059 195.8235 3733.088 2007.971
Gentoo 47.50488 14.98211 217.1870 5076.016 2008.081

And you can include arguments to functions inside peep() with dot syntax if you’re using the magrittr pipe %>% (at least for the line directly above the peep):1

palmerpenguins::penguins %>% 
  peep(head(n = 7, x = .)) |> 
  group_by(species) |> 
  summarize(across(where(is.numeric), \(x) mean(x, na.rm = TRUE)))
# A tibble: 7 × 8
  species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
  <fct>   <fct>              <dbl>         <dbl>             <int>       <int>
1 Adelie  Torgersen           39.1          18.7               181        3750
2 Adelie  Torgersen           39.5          17.4               186        3800
3 Adelie  Torgersen           40.3          18                 195        3250
4 Adelie  Torgersen           NA            NA                  NA          NA
5 Adelie  Torgersen           36.7          19.3               193        3450
6 Adelie  Torgersen           39.3          20.6               190        3650
7 Adelie  Torgersen           38.9          17.8               181        3625
# ℹ 2 more variables: sex <fct>, year <int>
species bill_length_mm bill_depth_mm flipper_length_mm body_mass_g year
Adelie 38.79139 18.34636 189.9536 3700.662 2008.013
Chinstrap 48.83382 18.42059 195.8235 3733.088 2007.971
Gentoo 47.50488 14.98211 217.1870 5076.016 2008.081

Footnotes

  1. You can’t use the base pipe’s placeholder, _, here, since it does not work in nested functions.↩︎