knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

loud

Easily add logs to your functions.

Installation

You can install the development version of loud from GitHub with:

# install.packages("devtools")
devtools::install_github("b-rodrigues/loud@release_0.1.1")

Example

This is a basic example which shows you how to solve a common problem:

library(loud)

loud_sqrt <- loudly(sqrt)

loud_sqrt(1:5)

You can also use the native R pipe:

loud_sqrt <- loudly(sqrt)
loud_exp <- loudly(exp)
loud_mean <- loudly(mean)

1:10 |>
  loud_sqrt() |>
  bind_loudly(loud_exp) |>
  bind_loudly(loud_mean)

bind_loudly() is used to pass the output from one decorated function to the next.

loudly() works with any function:

library(dplyr)

loud_group_by <- loudly(group_by)
loud_select <- loudly(select)
loud_summarise <- loudly(summarise)
loud_filter <- loudly(filter)

starwars %>%
  loud_select(height, mass, species, sex) %>%
  bind_loudly(loud_group_by, species, sex) %>%
  bind_loudly(loud_filter, sex != "male") %>%
  bind_loudly(loud_summarise,
              mass = mean(mass, na.rm = TRUE)
              )

You could also use the %>=% pipe instead of bind_loudly():

starwars %>%
  loud_value() %>=%
  loud_select(height, mass, species, sex) %>=%
  loud_group_by(species, sex) %>=%
  loud_filter(sex != "male") %>=%
  loud_summarise(mass = mean(mass, na.rm = TRUE))

Errors, warnings, and messages also get caught and composed in the log:

starwars %>%
  loud_value() %>=%
  loud_select(height, mass, species, sex) %>=% 
  loud_group_by(species, sx) %>=% # type, "sx" instead of "sex"
  loud_filter(sex != "male") %>=%
  loud_summarise(mass = mean(mass, na.rm = TRUE))

Caution

This packages is in early development and basically is held together with string and j*zz. Use at your own peril, but it has some tests now that pass, so it shouldn't be too bad. That being said, if you used it for serious work and it turns out that you house caught on fire, that's on you.

Thanks

I’d like to thank armcn, Kupac for their blog posts (here) and packages (maybe) which inspired me to build this package.



b-rodrigues/loud documentation built on April 8, 2022, 12:32 p.m.