tunnel: Tunnel a dataframe through a function for side effects

View source: R/dplyr_wrappers.R

tunnelR Documentation

Tunnel a dataframe through a function for side effects

Description

Within a dplyr-pipe, you might want to print a summary statistic or run some other function before continuing to process or save the original data. This function does that by taking a dataframe and function as arguments, printing the result of the function and returning the dataframe for further processing.

Usage

tunnel(df, fun, ..., note = NULL, return = TRUE)

Arguments

df

A dataframe

fun

A function that is applied to the dataframe and the output of which is printed. If the argument is not passed, df is merely printed and not returned, which only makes sense in combination with the 'note' argument

...

Arguments passed on to fun

note

A string that is printed above the fun-output

return

Logical - should df be returned?

Value

The original dataframe, unless return is set to FALSE. In that case, NULL is returned invisibly, so that the function can be used to just add a note above the output of another function.

Source

Inspired by the magrittr %T>% operator that promises similar functionality but often didn't quite fit my needs

Examples

library(magrittr)
x <- 1:5 %>%
  tunnel(mean, note = "Mean") %>%
  tunnel(sd, note = "SD")
x

LukasWallrich/rNuggets documentation built on Aug. 26, 2022, 11:03 a.m.